69 lines
1.4 KiB
Ruby
69 lines
1.4 KiB
Ruby
# 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
|