diff --git a/lib/referator.rb b/lib/referator.rb index 6ff1a36..8fe8661 100644 --- a/lib/referator.rb +++ b/lib/referator.rb @@ -8,23 +8,16 @@ require 'pathname' require_relative 'referator/command' require_relative 'referator/config' require_relative 'referator/config/categories' -require_relative 'referator/config/formats' require_relative 'referator/config/repo' -require_relative 'referator/config/scripts' require_relative 'referator/footnotes' require_relative 'referator/machine' require_relative 'referator/note' require_relative 'referator/reference' -require_relative 'referator/script' module Referator NAME_RE = /\A\w+(_\w+)*\z/ SLUG_RE = /\A\w+(-\w+)*\z/ - SCRIPT_ENUMS = %i[format template].sort.freeze - SCRIPT_OBJS = %i[note notes].sort.freeze - SCRIPT_VARS = [*SCRIPT_ENUMS, *SCRIPT_OBJS].sort.freeze - def self.validate_name!(name) name.freeze unless name.instance_of? Symbol @@ -44,14 +37,4 @@ module Referator slug end - - def self.validate_script_var!(var) - var.freeze - unless var.instance_of? Symbol - raise TypeError, "Expected #{String}, got #{slug.class}" - end - raise 'Invalid var' unless SCRIPT_VARS.include? var - - var - end end diff --git a/lib/referator/config.rb b/lib/referator/config.rb index 8106c54..81aa133 100644 --- a/lib/referator/config.rb +++ b/lib/referator/config.rb @@ -9,25 +9,15 @@ module Referator end def freeze - scripts.freeze categories.freeze - formats.freeze repo.freeze super end - def scripts - @scripts ||= Scripts.new self - end - def categories @categories ||= Categories.new self end - def formats - @formats ||= Formats.new self - end - def repo @repo ||= Repo.new self end diff --git a/lib/referator/config/formats.rb b/lib/referator/config/formats.rb deleted file mode 100644 index d82de04..0000000 --- a/lib/referator/config/formats.rb +++ /dev/null @@ -1,68 +0,0 @@ -# frozen_string_literal: true - -module Referator - class Config - class Formats - attr_reader :config - - def initialize(config) - self.config = config - @scripts = {} - end - - def freeze - @scripts.freeze - super - end - - def register(name:, script:) - name = String(name).to_sym - script = String(script).to_sym - Referator.validate_name! name - Referator.validate_name! script - config.scripts.vars! script, script_vars.keys - raise 'Already exists' if @scripts.key? name - - @scripts[name] = script - nil - end - - def exists!(name) - Referator.validate_name! name - @scripts[name] or raise 'Unknown format' - nil - end - - def render_footnotes(name, notes) - name = String(name).to_sym - exists! name - - config.scripts.run( - @scripts[name], - **script_vars( - template: :footnotes, - format: name, - notes: notes.to_json, - ), - ) - end - - private - - def config=(config) - unless config.instance_of? Config - raise TypeError, "Expected #{Config}, got #{config.class}" - end - - @config = config - end - - def script_vars(template: 'null', - format: 'null', - note: 'null', - notes: 'null') - { template:, format:, notes:, note: }.freeze - end - end - end -end diff --git a/lib/referator/config/scripts.rb b/lib/referator/config/scripts.rb deleted file mode 100644 index 6b42ab7..0000000 --- a/lib/referator/config/scripts.rb +++ /dev/null @@ -1,62 +0,0 @@ -# frozen_string_literal: true - -module Referator - class Config - class Scripts - attr_reader :config - - def initialize(config) - self.config = config - @scripts = {} - end - - def freeze - @scripts.freeze - super - end - - def register(name:, workdir: nil, vars: [], **kwargs) - name = String(name).to_sym - Referator.validate_name! name - raise 'Already exists' if @scripts.key? name - - script = Script.new( - **kwargs, - workdir: workdir || config.workdir, - vars: vars.map { |var| var.instance_of?(String) ? var.to_sym : var }, - ) - - @scripts[name] = script - nil - end - - def vars!(name, vars) - unless vars.instance_of? Array - raise TypeError, "Expected #{Array}, got #{vars.class}" - end - - Referator.validate_name! name - vars.each { |var| Referator.validate_script_var! var } - - script = @scripts[name] or raise 'Invalid script' - - raise 'Invalid vars' unless script.vars.sort == vars.sort - end - - def run(name, **vars) - vars! name, vars.keys - @scripts[name].call(**vars) - end - - private - - def config=(config) - unless config.instance_of? Config - raise TypeError, "Expected #{Config}, got #{config.class}" - end - - @config = config - end - end - end -end diff --git a/lib/referator/footnotes.rb b/lib/referator/footnotes.rb index 563bebe..47a8723 100644 --- a/lib/referator/footnotes.rb +++ b/lib/referator/footnotes.rb @@ -39,11 +39,6 @@ module Referator raise 'Unused note' end - def render_footnotes(format) - format = format.to_sym if format.instance_of? String - config.formats.render_footnotes format, notes - end - private def config=(config) diff --git a/lib/referator/machine.rb b/lib/referator/machine.rb index 885b920..0930264 100644 --- a/lib/referator/machine.rb +++ b/lib/referator/machine.rb @@ -49,44 +49,15 @@ module Referator :pong end - def do_exec(command) - File.open File.expand_path command.data, workdir do |file| - code = '' - while running? - line = file.gets&.strip - break if line.nil? - next if line.empty? || line.start_with?('#') - - if line.end_with? '\\' - code += line[...-1] - else - call Command.parse "#{code}#{line}" - code = '' - end - end - end - nil - end - ########## # Config # ########## - def do_register_script(command) - @config.scripts.register(**command.data_kwargs!) - nil - end - def do_register_category(command) @config.categories.register command.data nil end - def do_register_format(command) - @config.formats.register(**command.data_kwargs!) - nil - end - def do_register_ref(command) @config.repo.register_ref(**command.data_kwargs!) nil diff --git a/lib/referator/script.rb b/lib/referator/script.rb deleted file mode 100644 index 747be7b..0000000 --- a/lib/referator/script.rb +++ /dev/null @@ -1,128 +0,0 @@ -# frozen_string_literal: true - -module Referator - class Script - attr_reader :workdir, :vars, :args, :env, :stdin - - def initialize(workdir:, vars:, args: [], env: {}, stdin: []) - self.workdir = workdir - self.vars = vars - - self.args = args - self.env = env - self.stdin = stdin - - raise 'Invalid script' if self.args.empty? - end - - def call(**kwargs) - raise 'Invalid vars' unless kwargs.keys.sort == vars - - stdout, status = Open3.capture2( - sub_env(**kwargs), - *sub_args(**kwargs), - chdir: workdir.to_s, - stdin_data: sub_stdin(**kwargs), - ) - raise 'Exit code' unless status.success? - - String(stdout).freeze - end - - private - - def workdir=(workdir) - workdir = Pathname.new(workdir).expand_path.freeze - raise 'Expected absolute path' unless workdir.absolute? - raise 'Expected existing directory' unless workdir.directory? - - @workdir = workdir - end - - def vars=(vars) - @vars = [*vars].uniq.sort.freeze.each do |var| - unless var.instance_of? Symbol - raise TypeError, "Expected #{Symbol}, got #{var.class}" - end - end - end - - def args=(args) - @args = [*args].map { |arg| Sub.new vars, arg }.freeze - end - - def env=(env) - @env = Hash(env).to_h do |key, value| - [String(key).freeze, Sub.new(vars, value)] - end.freeze - end - - def stdin=(stdin) - @stdin = Sub.new vars, stdin - end - - def sub_args(**kwargs) - args.map { |sub| sub.call(**kwargs) }.freeze - end - - def sub_env(**kwargs) - env.transform_values { |sub| sub.call(**kwargs) }.freeze - end - - def sub_stdin(**kwargs) - stdin.call(**kwargs) - end - - class Sub - attr_reader :vars, :template - - def initialize(vars, template) - self.vars = vars - - self.template = - if template.instance_of?(String) || - template.instance_of?(Symbol) || - template.instance_of?(Hash) - [template] - else - template - end - end - - def call(**kwargs) - raise 'Invalid vars' unless kwargs.keys.sort == vars - - template.map do |part| - if part.instance_of? String - part - else - kwargs.fetch part - end - end.join.freeze - end - - private - - def vars=(vars) - @vars = [*vars].uniq.sort.freeze.each do |var| - Referator.validate_script_var! var - end - end - - def template=(template) - raise 'Invalid format' unless template.instance_of? Array - - @template = template.map do |part| - part = String(part.keys.first).to_sym if part.instance_of? Hash - - unless part.instance_of?(String) || - part.instance_of?(Symbol) && vars.include?(part) - raise 'Invalid format' - end - - part.freeze - end.freeze - end - end - end -end diff --git a/test.rb b/test.rb index 3553a51..ebfe947 100755 --- a/test.rb +++ b/test.rb @@ -41,9 +41,7 @@ REFS = { }.freeze, }.freeze -workdir = File.expand_path('test', __dir__).freeze - -@stdin, @stdout, @wait_thr = Open3.popen2 EXE, workdir, chdir: __dir__ +@stdin, @stdout, @wait_thr = Open3.popen2 EXE, __dir__, chdir: __dir__ @stdin.sync = true @stdout.sync = true @@ -75,24 +73,6 @@ end cmd('') { |result| raise unless result.nil? } cmd(:NULL) { |result| raise unless result.nil? } -################### -# REGISTER_SCRIPT # -################### - -cmd :REGISTER_SCRIPT, - { name: :render, - vars: %i[template format notes note], - args: ['./render.rb', - { template: nil }, - { format: nil }], - stdin: ['{', - '"notes":', - { notes: nil }, - ',', - '"note":', - { note: nil }, - '}'] } - ##################### # REGISTER_CATEGORY # ##################### @@ -100,13 +80,6 @@ cmd :REGISTER_SCRIPT, cmd :REGISTER_CATEGORY, :self cmd :REGISTER_CATEGORY, :link -################### -# REGISTER_FORMAT # -################### - -cmd :REGISTER_FORMAT, { name: :html, script: :render } -cmd :REGISTER_FORMAT, { name: :json, script: :render } - ################ # REGISTER_REF # ################ @@ -114,7 +87,8 @@ cmd :REGISTER_FORMAT, { name: :json, script: :render } cmd :REGISTER_REF, REFS[:self][:foo] cmd :REGISTER_REF, REFS[:link][:example_com] -cmd(:EXEC, 'conf_register_ref') { |result| raise unless result.nil? } +cmd :REGISTER_REF, REFS[:self][:bar] +cmd :REGISTER_REF, REFS[:link][:causa_arcana_com] ####### # REF # @@ -193,83 +167,6 @@ cmd :FETCH_NOTE, { category: :link, id: :causa_arcana_com } do |result| raise unless result == REFS[:link][:causa_arcana_com].merge('index' => 4) end -#################### -# RENDER_FOOTNOTES # -#################### - -cmd :RENDER_FOOTNOTES, :html do |result| - expected = <<~HTML -

Self references

- -
    -
  1. - Foo -
  2. -
  3. - Bar -
  4. -
- -

Link references

- -
    - - -
- HTML - - raise unless result == expected -end - -cmd :RENDER_FOOTNOTES, :json do |result| - expected = <<~JSON - { - "self": [ - { - "index": 1, - "category": "self", - "id": "/blog/foo", - "slug": "blog-foo", - "url": "/blog/foo.html", - "text": "Foo" - }, - { - "index": 2, - "category": "self", - "id": "/blog/bar", - "slug": "blog-bar", - "url": "/blog/bar.html", - "text": "Bar" - } - ], - "link": [ - { - "index": 3, - "category": "link", - "id": "example_com", - "slug": "example-com", - "url": "https://example.com", - "text": "Example Domain" - }, - { - "index": 4, - "category": "link", - "id": "causa_arcana_com", - "slug": "causa-arcana-com", - "url": "https://causa-arcana.com", - "text": "Causa Arcana" - } - ] - } - JSON - - raise unless result == expected -end - ######## # EXIT # ######## diff --git a/test/conf_register_ref b/test/conf_register_ref deleted file mode 100644 index d5419a4..0000000 --- a/test/conf_register_ref +++ /dev/null @@ -1,16 +0,0 @@ -# self references -register_ref { \ - "category": "self", \ - "id": "/blog/bar", \ - "slug": "blog-bar", \ - "text": "Bar" \ -} - -# link references -register_ref { \ - "category": "link", \ - "id": "causa_arcana_com", \ - "slug": "causa-arcana-com", \ - "url": "https://causa-arcana.com", \ - "text": "Causa Arcana" \ -} diff --git a/test/footnotes.html.erb b/test/footnotes.html.erb deleted file mode 100644 index a7c9d60..0000000 --- a/test/footnotes.html.erb +++ /dev/null @@ -1,25 +0,0 @@ -<% unless notes['self'].empty? -%> - -

Self references

- -
    -<% notes['self'].each do |note| -%> -
  1. " value="<%= note['index'] %>"> - <%= note['text'] %> -
  2. -<% end -%> -
-<% end -%> -<%# -%> -<% unless notes['link'].empty? -%> - -

Link references

- -
    -<% notes['link'].each do |note| -%> -
  1. " value="<%= note['index'] %>"> - <%= note['text'] %> -
  2. -<% end -%> -
-<% end -%> diff --git a/test/footnotes.json.erb b/test/footnotes.json.erb deleted file mode 100644 index 4084e53..0000000 --- a/test/footnotes.json.erb +++ /dev/null @@ -1,26 +0,0 @@ -{ - "self": [ - <%- notes['self'].each_with_index do |note, index| -%> - { - "index": <%= Integer(note['index']).to_json %>, - "category": "self", - "id": <%= String(note['id']).to_json %>, - "slug": <%= String(note['slug']).to_json %>, - "url": <%= "#{note['id']}.html".to_json %>, - "text": <%= String(note['text']).to_json %> - }<%= ',' unless index == notes['self'].count - 1 %> - <%- end -%> - ], - "link": [ - <%- notes['link'].each_with_index do |note, index| -%> - { - "index": <%= Integer(note['index']).to_json %>, - "category": "link", - "id": <%= String(note['id']).to_json %>, - "slug": <%= String(note['slug']).to_json %>, - "url": <%= String(note['url']).to_json %>, - "text": <%= String(note['text']).to_json %> - }<%= ',' unless index == notes['link'].count - 1 %> - <%- end -%> - ] -} diff --git a/test/render.rb b/test/render.rb deleted file mode 100755 index dd03544..0000000 --- a/test/render.rb +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -require 'erb' -require 'json' -require 'pathname' - -def template(filename) - pathname = Pathname.new(__dir__).join(filename).expand_path.freeze - ERB.new(pathname.read, trim_mode: '-').tap do |erb| - erb.filename = pathname.to_s.freeze - end.freeze -end - -name = "#{ARGV[0]}.#{ARGV[1]}.erb".freeze -vars = JSON.parse($stdin.read).freeze - -puts template(name).result_with_hash(vars).strip