diff --git a/exe/referator b/exe/referator index e36e739..229491c 100755 --- a/exe/referator +++ b/exe/referator @@ -12,9 +12,12 @@ $stdout.sync = true def parse_command(line) line = String(line).chomp - command = line[0...line.index(' ')].upcase.freeze - rest = line[command.length..].strip.freeze - [command, rest].freeze + index = line.index(' ') || line.length + command = line[0...index].upcase.freeze + rest = line[index..].strip + rest = 'null' if rest.empty? + data = JSON.parse(rest).freeze + [command, data].freeze end def success(data = nil) @@ -35,7 +38,7 @@ stacktrace = nil while (line = $stdin.gets) begin - command, rest = parse_command line + command, data = parse_command line case command ################## # Administrative # @@ -52,19 +55,15 @@ while (line = $stdin.gets) # Config # ########## when 'REGISTER_SCRIPT' - data = JSON.parse rest config.scripts.register(**data.transform_keys(&:to_sym)) success when 'REGISTER_CATEGORY' - data = JSON.parse rest - config.categories.register String(data).to_sym + config.categories.register data success when 'REGISTER_FORMAT' - data = JSON.parse rest config.formats.register(**data.transform_keys(&:to_sym)) success when 'REGISTER_REF' - data = JSON.parse rest config.repo.register_ref(**data.transform_keys(&:to_sym)) success ######## @@ -72,35 +71,28 @@ while (line = $stdin.gets) ######## when 'REF' config.freeze - data = JSON.parse rest - success config.repo[ - String(data['category']).to_sym, - String(data['id']), - ].to_h + success config.repo[data['category'], data['id']].to_h ############ # Creation # ############ when 'MAKE_REF' config.freeze - data = JSON.parse rest - success footnotes.make_ref( - String(data['category']).to_sym, - String(data['id']), - ).to_h + success footnotes.make_ref(data['category'], data['id']).to_h ############# # Rendering # ############# + when 'FETCH_FOOTNOTES' + config.freeze + success footnotes.fetch_footnotes when 'FETCH_NOTE' config.freeze - data = JSON.parse rest - success footnotes.fetch_note( - String(data['category']).to_sym, - String(data['id']), - ).to_h + success footnotes.fetch_note(data['category'], data['id']).to_h when 'RENDER_FOOTNOTES' config.freeze - data = JSON.parse rest - success String footnotes.render String(data).to_sym + success String footnotes.render_footnotes data + when 'RENDER_CITE_SUP' + config.freeze + success String footnotes.render_cite_sup(**data.transform_keys(&:to_sym)) else raise 'Invalid command' end diff --git a/lib/referator.rb b/lib/referator.rb index 360e937..eaa283d 100644 --- a/lib/referator.rb +++ b/lib/referator.rb @@ -20,7 +20,7 @@ module Referator SLUG_RE = /\A\w+(-\w+)*\z/ SCRIPT_ENUMS = %i[format template].sort.freeze - SCRIPT_OBJS = %i[notes].sort.freeze + SCRIPT_OBJS = %i[note notes].sort.freeze SCRIPT_VARS = [*SCRIPT_ENUMS, *SCRIPT_OBJS].sort.freeze def self.validate_name!(name) diff --git a/lib/referator/config/categories.rb b/lib/referator/config/categories.rb index d8b979d..a521c88 100644 --- a/lib/referator/config/categories.rb +++ b/lib/referator/config/categories.rb @@ -16,6 +16,7 @@ module Referator end def register(name) + name = name.to_sym if name.instance_of? String Referator.validate_name! name raise 'Already exists' if names.index name diff --git a/lib/referator/config/formats.rb b/lib/referator/config/formats.rb index 900c838..cb295f6 100644 --- a/lib/referator/config/formats.rb +++ b/lib/referator/config/formats.rb @@ -33,18 +33,34 @@ module Referator nil end - def render(template, name, notes) + def render_footnotes(name, notes) + name = String(name).to_sym exists! name + config.scripts.run( @scripts[name], **script_vars( - template:, + template: :footnotes, format: name, notes: notes.to_json, ), ) end + def render_cite_sup(name, note) + name = String(name).to_sym + exists! name + + config.scripts.run( + @scripts[name], + **script_vars( + template: :cite_sup, + format: name, + note: note.to_json, + ), + ) + end + private def config=(config) @@ -55,8 +71,11 @@ module Referator @config = config end - def script_vars(template: nil, format: nil, notes: nil) - { template:, format:, notes: }.freeze + def script_vars(template: 'null', + format: 'null', + note: 'null', + notes: 'null') + { template:, format:, notes:, note: }.freeze end end end diff --git a/lib/referator/config/repo.rb b/lib/referator/config/repo.rb index f29e640..68835fc 100644 --- a/lib/referator/config/repo.rb +++ b/lib/referator/config/repo.rb @@ -25,6 +25,7 @@ module Referator end def [](category, id) + category = category.to_sym if category.instance_of? String config.categories.exists! category key = "#{category}-#{id}".freeze @references[key].tap do |reference| diff --git a/lib/referator/footnotes.rb b/lib/referator/footnotes.rb index 4023317..a39a3ff 100644 --- a/lib/referator/footnotes.rb +++ b/lib/referator/footnotes.rb @@ -11,6 +11,7 @@ module Referator end def make_ref(category, id) + category = category.to_sym if category.instance_of? String config.categories.exists! category @references[category] ||= [] @references[category].each do |reference| @@ -21,7 +22,10 @@ module Referator reference end + def fetch_footnotes = notes + def fetch_note(category, id) + category = category.to_sym if category.instance_of? String config.categories.exists! category index = 0 config.categories.names.each do |cur_category| @@ -35,8 +39,21 @@ module Referator raise 'Unused note' end - def render(format) - config.formats.render :footnotes, format, notes + def render_footnotes(format) + format = format.to_sym if format.instance_of? String + config.formats.render_footnotes format, notes + end + + def render_cite_sup(format:, category:, id:) + format = format.to_sym if format.instance_of? String + category = category.to_sym if category.instance_of? String + note = notes.flat_map(&:last).find do |curr_note| + curr_note[:category] == String(category).to_sym && + curr_note[:id] == String(id) + end + raise 'Unknown note' if note.nil? + + config.formats.render_cite_sup format, note end private diff --git a/test.rb b/test.rb index c349de3..12e7301 100755 --- a/test.rb +++ b/test.rb @@ -73,7 +73,7 @@ at_exit do raise 'Script error' unless @wait_thr.value.success? end -def cmd(name, data) +def cmd(name, data = nil) @stdin.puts "#{name} #{data.to_json}" result = @stdout.gets.chomp if result.start_with? 'OK ' @@ -93,12 +93,16 @@ end cmd :REGISTER_SCRIPT, { name: :render, - vars: %i[template format notes], + vars: %i[template format notes note], args: ['test/render.rb', { template: nil }, { format: nil }], - stdin: ['{"notes":', + stdin: ['{', + '"notes":', { notes: nil }, + ',', + '"note":', + { note: nil }, '}'] } ##################### @@ -164,6 +168,23 @@ cmd :MAKE_REF, { category: :link, id: :causa_arcana_com } do |result| raise unless result == REFS[:link][:causa_arcana_com] end +################### +# FETCH_FOOTNOTES # +################### + +cmd :FETCH_FOOTNOTES do |result| + raise unless result == { + 'self' => [ + REFS[:self][:foo].merge('index' => 1), + REFS[:self][:bar].merge('index' => 2), + ], + 'link' => [ + REFS[:link][:example_com].merge('index' => 3), + REFS[:link][:causa_arcana_com].merge('index' => 4), + ], + } +end + ############## # FETCH_NOTE # ############## @@ -268,3 +289,36 @@ cmd :RENDER_FOOTNOTES, :json do |result| raise unless result == expected end + +################### +# RENDER_CITE_SUP # +################### + +cmd( + :RENDER_CITE_SUP, + { format: :html, category: :self, id: '/blog/foo' }, +) do |result| + raise unless result == "[1]\n" +end + +cmd( + :RENDER_CITE_SUP, + { format: :html, category: :self, id: '/blog/bar' }, +) do |result| + raise unless result == "[2]\n" +end + +cmd( + :RENDER_CITE_SUP, + { format: :html, category: :link, id: :example_com }, +) do |result| + raise unless result == "[3]\n" +end + +cmd( + :RENDER_CITE_SUP, + { format: :html, category: :link, id: :causa_arcana_com }, +) do |result| + raise unless result == + "[4]\n" +end diff --git a/test/cite_sup.html.erb b/test/cite_sup.html.erb new file mode 100644 index 0000000..e996a69 --- /dev/null +++ b/test/cite_sup.html.erb @@ -0,0 +1 @@ +[<%= note['index'] %>]