97 lines
2.4 KiB
Ruby
Executable file
97 lines
2.4 KiB
Ruby
Executable file
#!/usr/bin/env ruby
|
|
# frozen_string_literal: true
|
|
|
|
lib = File.expand_path('../lib', __dir__).freeze
|
|
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
|
|
|
|
require 'json'
|
|
require 'referator'
|
|
|
|
$stdin.sync = true
|
|
$stdout.sync = true
|
|
|
|
def success(data = nil)
|
|
puts "OK #{data.to_json}"
|
|
end
|
|
|
|
def failure(msg)
|
|
puts "ERR #{String(msg).lines.join(' ')}"
|
|
end
|
|
|
|
workdir = ARGV.first
|
|
workdir = Dir.pwd if String(workdir).strip.empty?
|
|
workdir = File.expand_path(workdir).freeze
|
|
|
|
config = Referator::Config.new workdir
|
|
footnotes = Referator::Footnotes.new config
|
|
stacktrace = nil
|
|
|
|
while (line = $stdin.gets)
|
|
begin
|
|
command = Referator::Command.parse line
|
|
case command.name
|
|
##################
|
|
# Administrative #
|
|
##################
|
|
when :exit
|
|
success
|
|
exit
|
|
when :ping
|
|
success :pong
|
|
when :stacktrace
|
|
success stacktrace
|
|
stacktrace = nil
|
|
##########
|
|
# Config #
|
|
##########
|
|
when :register_script
|
|
config.scripts.register(**command.data.transform_keys(&:to_sym))
|
|
success
|
|
when :register_category
|
|
config.categories.register command.data
|
|
success
|
|
when :register_format
|
|
config.formats.register(**command.data.transform_keys(&:to_sym))
|
|
success
|
|
when :register_ref
|
|
config.repo.register_ref(**command.data.transform_keys(&:to_sym))
|
|
success
|
|
########
|
|
# Info #
|
|
########
|
|
when :ref
|
|
config.freeze
|
|
success config.repo[command.data['category'],
|
|
command.data['id']].to_h
|
|
############
|
|
# Creation #
|
|
############
|
|
when :make_ref
|
|
config.freeze
|
|
success footnotes.make_ref(command.data['category'],
|
|
command.data['id']).to_h
|
|
#############
|
|
# Rendering #
|
|
#############
|
|
when :fetch_footnotes
|
|
config.freeze
|
|
success footnotes.fetch_footnotes
|
|
when :fetch_note
|
|
config.freeze
|
|
success footnotes.fetch_note(command.data['category'],
|
|
command.data['id']).to_h
|
|
when :render_footnotes
|
|
config.freeze
|
|
success String footnotes.render_footnotes command.data
|
|
when :render_cite_sup
|
|
config.freeze
|
|
success String \
|
|
footnotes.render_cite_sup(**command.data.transform_keys(&:to_sym))
|
|
else
|
|
raise 'Invalid command'
|
|
end
|
|
rescue => e
|
|
stacktrace = e.backtrace.map(&:freeze).freeze
|
|
failure e.detailed_message
|
|
end
|
|
end
|