Add class Referator::Machine
This commit is contained in:
parent
c8d1e94456
commit
58ebf111cf
3 changed files with 85 additions and 60 deletions
|
@ -31,70 +31,13 @@ workdir = ARGV.first
|
||||||
workdir = Dir.pwd if String(workdir).strip.empty?
|
workdir = Dir.pwd if String(workdir).strip.empty?
|
||||||
workdir = File.expand_path(workdir).freeze
|
workdir = File.expand_path(workdir).freeze
|
||||||
|
|
||||||
config = Referator::Config.new workdir
|
machine = Referator::Machine.new workdir
|
||||||
footnotes = Referator::Footnotes.new config
|
|
||||||
|
|
||||||
while (line = $stdin.gets)
|
while (line = $stdin.gets)
|
||||||
begin
|
begin
|
||||||
command = Referator::Command.parse line
|
command = Referator::Command.parse line
|
||||||
case command.name
|
result = machine.call command
|
||||||
##################
|
success result
|
||||||
# Administrative #
|
|
||||||
##################
|
|
||||||
when :exit
|
|
||||||
success
|
|
||||||
exit
|
|
||||||
when :ping
|
|
||||||
success :pong
|
|
||||||
##########
|
|
||||||
# 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
|
rescue => e
|
||||||
failure serialize_err e
|
failure serialize_err e
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,7 @@ require_relative 'referator/config/formats'
|
||||||
require_relative 'referator/config/repo'
|
require_relative 'referator/config/repo'
|
||||||
require_relative 'referator/config/scripts'
|
require_relative 'referator/config/scripts'
|
||||||
require_relative 'referator/footnotes'
|
require_relative 'referator/footnotes'
|
||||||
|
require_relative 'referator/machine'
|
||||||
require_relative 'referator/note'
|
require_relative 'referator/note'
|
||||||
require_relative 'referator/reference'
|
require_relative 'referator/reference'
|
||||||
require_relative 'referator/script'
|
require_relative 'referator/script'
|
||||||
|
|
81
lib/referator/machine.rb
Normal file
81
lib/referator/machine.rb
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Referator
|
||||||
|
class Machine
|
||||||
|
attr_reader :workdir
|
||||||
|
|
||||||
|
def initialize(workdir)
|
||||||
|
self.workdir = workdir
|
||||||
|
|
||||||
|
@config = Config.new self.workdir
|
||||||
|
@footnotes = Footnotes.new @config
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(command)
|
||||||
|
case command.name
|
||||||
|
##################
|
||||||
|
# Administrative #
|
||||||
|
##################
|
||||||
|
when :exit
|
||||||
|
exit
|
||||||
|
when :ping
|
||||||
|
:pong
|
||||||
|
##########
|
||||||
|
# Config #
|
||||||
|
##########
|
||||||
|
when :register_script
|
||||||
|
@config.scripts.register(**command.data.transform_keys(&:to_sym))
|
||||||
|
nil
|
||||||
|
when :register_category
|
||||||
|
@config.categories.register command.data
|
||||||
|
nil
|
||||||
|
when :register_format
|
||||||
|
@config.formats.register(**command.data.transform_keys(&:to_sym))
|
||||||
|
nil
|
||||||
|
when :register_ref
|
||||||
|
@config.repo.register_ref(**command.data.transform_keys(&:to_sym))
|
||||||
|
nil
|
||||||
|
########
|
||||||
|
# Info #
|
||||||
|
########
|
||||||
|
when :ref
|
||||||
|
@config.freeze
|
||||||
|
@config.repo[command.data['category'], command.data['id']].to_h
|
||||||
|
############
|
||||||
|
# Creation #
|
||||||
|
############
|
||||||
|
when :make_ref
|
||||||
|
@config.freeze
|
||||||
|
@footnotes.make_ref(command.data['category'], command.data['id']).to_h
|
||||||
|
#############
|
||||||
|
# Rendering #
|
||||||
|
#############
|
||||||
|
when :fetch_footnotes
|
||||||
|
@config.freeze
|
||||||
|
@footnotes.fetch_footnotes
|
||||||
|
when :fetch_note
|
||||||
|
@config.freeze
|
||||||
|
@footnotes.fetch_note(command.data['category'], command.data['id']).to_h
|
||||||
|
when :render_footnotes
|
||||||
|
@config.freeze
|
||||||
|
String @footnotes.render_footnotes command.data
|
||||||
|
when :render_cite_sup
|
||||||
|
@config.freeze
|
||||||
|
String \
|
||||||
|
@footnotes.render_cite_sup(**command.data.transform_keys(&:to_sym))
|
||||||
|
else
|
||||||
|
raise 'Invalid command'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def workdir=(workdir)
|
||||||
|
workdir = Pathname.new(workdir).expand_path.freeze
|
||||||
|
raise 'Expected absolute path' unless workdir.absolute?
|
||||||
|
raise 'Expected existing directory' unless workdir.directory?
|
||||||
|
|
||||||
|
@workdir = workdir
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue