Start working on cite templates
This commit is contained in:
parent
9c60167337
commit
663ace82a2
8 changed files with 121 additions and 36 deletions
|
@ -12,9 +12,12 @@ $stdout.sync = true
|
||||||
|
|
||||||
def parse_command(line)
|
def parse_command(line)
|
||||||
line = String(line).chomp
|
line = String(line).chomp
|
||||||
command = line[0...line.index(' ')].upcase.freeze
|
index = line.index(' ') || line.length
|
||||||
rest = line[command.length..].strip.freeze
|
command = line[0...index].upcase.freeze
|
||||||
[command, rest].freeze
|
rest = line[index..].strip
|
||||||
|
rest = 'null' if rest.empty?
|
||||||
|
data = JSON.parse(rest).freeze
|
||||||
|
[command, data].freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
def success(data = nil)
|
def success(data = nil)
|
||||||
|
@ -35,7 +38,7 @@ stacktrace = nil
|
||||||
|
|
||||||
while (line = $stdin.gets)
|
while (line = $stdin.gets)
|
||||||
begin
|
begin
|
||||||
command, rest = parse_command line
|
command, data = parse_command line
|
||||||
case command
|
case command
|
||||||
##################
|
##################
|
||||||
# Administrative #
|
# Administrative #
|
||||||
|
@ -52,19 +55,15 @@ while (line = $stdin.gets)
|
||||||
# Config #
|
# Config #
|
||||||
##########
|
##########
|
||||||
when 'REGISTER_SCRIPT'
|
when 'REGISTER_SCRIPT'
|
||||||
data = JSON.parse rest
|
|
||||||
config.scripts.register(**data.transform_keys(&:to_sym))
|
config.scripts.register(**data.transform_keys(&:to_sym))
|
||||||
success
|
success
|
||||||
when 'REGISTER_CATEGORY'
|
when 'REGISTER_CATEGORY'
|
||||||
data = JSON.parse rest
|
config.categories.register data
|
||||||
config.categories.register String(data).to_sym
|
|
||||||
success
|
success
|
||||||
when 'REGISTER_FORMAT'
|
when 'REGISTER_FORMAT'
|
||||||
data = JSON.parse rest
|
|
||||||
config.formats.register(**data.transform_keys(&:to_sym))
|
config.formats.register(**data.transform_keys(&:to_sym))
|
||||||
success
|
success
|
||||||
when 'REGISTER_REF'
|
when 'REGISTER_REF'
|
||||||
data = JSON.parse rest
|
|
||||||
config.repo.register_ref(**data.transform_keys(&:to_sym))
|
config.repo.register_ref(**data.transform_keys(&:to_sym))
|
||||||
success
|
success
|
||||||
########
|
########
|
||||||
|
@ -72,35 +71,28 @@ while (line = $stdin.gets)
|
||||||
########
|
########
|
||||||
when 'REF'
|
when 'REF'
|
||||||
config.freeze
|
config.freeze
|
||||||
data = JSON.parse rest
|
success config.repo[data['category'], data['id']].to_h
|
||||||
success config.repo[
|
|
||||||
String(data['category']).to_sym,
|
|
||||||
String(data['id']),
|
|
||||||
].to_h
|
|
||||||
############
|
############
|
||||||
# Creation #
|
# Creation #
|
||||||
############
|
############
|
||||||
when 'MAKE_REF'
|
when 'MAKE_REF'
|
||||||
config.freeze
|
config.freeze
|
||||||
data = JSON.parse rest
|
success footnotes.make_ref(data['category'], data['id']).to_h
|
||||||
success footnotes.make_ref(
|
|
||||||
String(data['category']).to_sym,
|
|
||||||
String(data['id']),
|
|
||||||
).to_h
|
|
||||||
#############
|
#############
|
||||||
# Rendering #
|
# Rendering #
|
||||||
#############
|
#############
|
||||||
|
when 'FETCH_FOOTNOTES'
|
||||||
|
config.freeze
|
||||||
|
success footnotes.fetch_footnotes
|
||||||
when 'FETCH_NOTE'
|
when 'FETCH_NOTE'
|
||||||
config.freeze
|
config.freeze
|
||||||
data = JSON.parse rest
|
success footnotes.fetch_note(data['category'], data['id']).to_h
|
||||||
success footnotes.fetch_note(
|
|
||||||
String(data['category']).to_sym,
|
|
||||||
String(data['id']),
|
|
||||||
).to_h
|
|
||||||
when 'RENDER_FOOTNOTES'
|
when 'RENDER_FOOTNOTES'
|
||||||
config.freeze
|
config.freeze
|
||||||
data = JSON.parse rest
|
success String footnotes.render_footnotes data
|
||||||
success String footnotes.render String(data).to_sym
|
when 'RENDER_CITE_SUP'
|
||||||
|
config.freeze
|
||||||
|
success String footnotes.render_cite_sup(**data.transform_keys(&:to_sym))
|
||||||
else
|
else
|
||||||
raise 'Invalid command'
|
raise 'Invalid command'
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,7 +20,7 @@ module Referator
|
||||||
SLUG_RE = /\A\w+(-\w+)*\z/
|
SLUG_RE = /\A\w+(-\w+)*\z/
|
||||||
|
|
||||||
SCRIPT_ENUMS = %i[format template].sort.freeze
|
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
|
SCRIPT_VARS = [*SCRIPT_ENUMS, *SCRIPT_OBJS].sort.freeze
|
||||||
|
|
||||||
def self.validate_name!(name)
|
def self.validate_name!(name)
|
||||||
|
|
|
@ -16,6 +16,7 @@ module Referator
|
||||||
end
|
end
|
||||||
|
|
||||||
def register(name)
|
def register(name)
|
||||||
|
name = name.to_sym if name.instance_of? String
|
||||||
Referator.validate_name! name
|
Referator.validate_name! name
|
||||||
raise 'Already exists' if names.index name
|
raise 'Already exists' if names.index name
|
||||||
|
|
||||||
|
|
|
@ -33,18 +33,34 @@ module Referator
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(template, name, notes)
|
def render_footnotes(name, notes)
|
||||||
|
name = String(name).to_sym
|
||||||
exists! name
|
exists! name
|
||||||
|
|
||||||
config.scripts.run(
|
config.scripts.run(
|
||||||
@scripts[name],
|
@scripts[name],
|
||||||
**script_vars(
|
**script_vars(
|
||||||
template:,
|
template: :footnotes,
|
||||||
format: name,
|
format: name,
|
||||||
notes: notes.to_json,
|
notes: notes.to_json,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def config=(config)
|
def config=(config)
|
||||||
|
@ -55,8 +71,11 @@ module Referator
|
||||||
@config = config
|
@config = config
|
||||||
end
|
end
|
||||||
|
|
||||||
def script_vars(template: nil, format: nil, notes: nil)
|
def script_vars(template: 'null',
|
||||||
{ template:, format:, notes: }.freeze
|
format: 'null',
|
||||||
|
note: 'null',
|
||||||
|
notes: 'null')
|
||||||
|
{ template:, format:, notes:, note: }.freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,6 +25,7 @@ module Referator
|
||||||
end
|
end
|
||||||
|
|
||||||
def [](category, id)
|
def [](category, id)
|
||||||
|
category = category.to_sym if category.instance_of? String
|
||||||
config.categories.exists! category
|
config.categories.exists! category
|
||||||
key = "#{category}-#{id}".freeze
|
key = "#{category}-#{id}".freeze
|
||||||
@references[key].tap do |reference|
|
@references[key].tap do |reference|
|
||||||
|
|
|
@ -11,6 +11,7 @@ module Referator
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_ref(category, id)
|
def make_ref(category, id)
|
||||||
|
category = category.to_sym if category.instance_of? String
|
||||||
config.categories.exists! category
|
config.categories.exists! category
|
||||||
@references[category] ||= []
|
@references[category] ||= []
|
||||||
@references[category].each do |reference|
|
@references[category].each do |reference|
|
||||||
|
@ -21,7 +22,10 @@ module Referator
|
||||||
reference
|
reference
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_footnotes = notes
|
||||||
|
|
||||||
def fetch_note(category, id)
|
def fetch_note(category, id)
|
||||||
|
category = category.to_sym if category.instance_of? String
|
||||||
config.categories.exists! category
|
config.categories.exists! category
|
||||||
index = 0
|
index = 0
|
||||||
config.categories.names.each do |cur_category|
|
config.categories.names.each do |cur_category|
|
||||||
|
@ -35,8 +39,21 @@ module Referator
|
||||||
raise 'Unused note'
|
raise 'Unused note'
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(format)
|
def render_footnotes(format)
|
||||||
config.formats.render :footnotes, format, notes
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
60
test.rb
60
test.rb
|
@ -73,7 +73,7 @@ at_exit do
|
||||||
raise 'Script error' unless @wait_thr.value.success?
|
raise 'Script error' unless @wait_thr.value.success?
|
||||||
end
|
end
|
||||||
|
|
||||||
def cmd(name, data)
|
def cmd(name, data = nil)
|
||||||
@stdin.puts "#{name} #{data.to_json}"
|
@stdin.puts "#{name} #{data.to_json}"
|
||||||
result = @stdout.gets.chomp
|
result = @stdout.gets.chomp
|
||||||
if result.start_with? 'OK '
|
if result.start_with? 'OK '
|
||||||
|
@ -93,12 +93,16 @@ end
|
||||||
|
|
||||||
cmd :REGISTER_SCRIPT,
|
cmd :REGISTER_SCRIPT,
|
||||||
{ name: :render,
|
{ name: :render,
|
||||||
vars: %i[template format notes],
|
vars: %i[template format notes note],
|
||||||
args: ['test/render.rb',
|
args: ['test/render.rb',
|
||||||
{ template: nil },
|
{ template: nil },
|
||||||
{ format: nil }],
|
{ format: nil }],
|
||||||
stdin: ['{"notes":',
|
stdin: ['{',
|
||||||
|
'"notes":',
|
||||||
{ notes: nil },
|
{ 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]
|
raise unless result == REFS[:link][:causa_arcana_com]
|
||||||
end
|
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 #
|
# FETCH_NOTE #
|
||||||
##############
|
##############
|
||||||
|
@ -268,3 +289,36 @@ cmd :RENDER_FOOTNOTES, :json do |result|
|
||||||
|
|
||||||
raise unless result == expected
|
raise unless result == expected
|
||||||
end
|
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
1
test/cite_sup.html.erb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<sup><a href="<%= note['fragment'] %>">[<%= note['index'] %>]</a></sup>
|
Loading…
Reference in a new issue