Start working on cite templates

This commit is contained in:
Alex Kotov 2023-10-01 01:32:24 +04:00
parent 9c60167337
commit 663ace82a2
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
8 changed files with 121 additions and 36 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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|

View file

@ -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

60
test.rb
View file

@ -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 == "<sup><a href=\"#self-blog-foo\">[1]</a></sup>\n"
end
cmd(
:RENDER_CITE_SUP,
{ format: :html, category: :self, id: '/blog/bar' },
) do |result|
raise unless result == "<sup><a href=\"#self-blog-bar\">[2]</a></sup>\n"
end
cmd(
:RENDER_CITE_SUP,
{ format: :html, category: :link, id: :example_com },
) do |result|
raise unless result == "<sup><a href=\"#link-example-com\">[3]</a></sup>\n"
end
cmd(
:RENDER_CITE_SUP,
{ format: :html, category: :link, id: :causa_arcana_com },
) do |result|
raise unless result ==
"<sup><a href=\"#link-causa-arcana-com\">[4]</a></sup>\n"
end

1
test/cite_sup.html.erb Normal file
View file

@ -0,0 +1 @@
<sup><a href="<%= note['fragment'] %>">[<%= note['index'] %>]</a></sup>