139 lines
2.7 KiB
Ruby
139 lines
2.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Referator
|
|
class Machine
|
|
attr_reader :workdir
|
|
|
|
def initialize(workdir)
|
|
self.workdir = workdir
|
|
|
|
@running = true
|
|
|
|
@config = Config.new self.workdir
|
|
@footnotes = Footnotes.new @config
|
|
end
|
|
|
|
def running? = @running
|
|
|
|
def call(command)
|
|
method_name = "do_#{command.name}"
|
|
raise 'Invalid command' unless respond_to? method_name, true
|
|
|
|
send method_name, command
|
|
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
|
|
|
|
##################
|
|
# Administrative #
|
|
##################
|
|
|
|
def do_exit(_command)
|
|
@running = false
|
|
nil
|
|
end
|
|
|
|
def do_null(_command)
|
|
nil
|
|
end
|
|
|
|
def do_ping(_command)
|
|
:pong
|
|
end
|
|
|
|
def do_exec(command)
|
|
File.open File.expand_path command.data, workdir do |file|
|
|
code = ''
|
|
while running?
|
|
line = file.gets&.strip
|
|
break if line.nil?
|
|
next if line.empty? || line.start_with?('#')
|
|
|
|
if line.end_with? '\\'
|
|
code += line[...-1]
|
|
else
|
|
call Command.parse "#{code}#{line}"
|
|
code = ''
|
|
end
|
|
end
|
|
end
|
|
nil
|
|
end
|
|
|
|
##########
|
|
# Config #
|
|
##########
|
|
|
|
def do_register_script(command)
|
|
@config.scripts.register(**command.data_kwargs!)
|
|
nil
|
|
end
|
|
|
|
def do_register_category(command)
|
|
@config.categories.register command.data
|
|
nil
|
|
end
|
|
|
|
def do_register_format(command)
|
|
@config.formats.register(**command.data_kwargs!)
|
|
nil
|
|
end
|
|
|
|
def do_register_ref(command)
|
|
@config.repo.register_ref(**command.data_kwargs!)
|
|
nil
|
|
end
|
|
|
|
########
|
|
# Info #
|
|
########
|
|
|
|
def do_ref(command)
|
|
@config.freeze
|
|
@config.repo[*category_and_id(**command.data_kwargs!)].to_h
|
|
end
|
|
|
|
############
|
|
# Creation #
|
|
############
|
|
|
|
def do_make_ref(command)
|
|
@config.freeze
|
|
@footnotes.make_ref(*category_and_id(**command.data_kwargs!)).to_h
|
|
end
|
|
|
|
#############
|
|
# Rendering #
|
|
#############
|
|
|
|
def do_fetch_footnotes(_command)
|
|
@config.freeze
|
|
@footnotes.fetch_footnotes
|
|
end
|
|
|
|
def do_fetch_note(command)
|
|
@config.freeze
|
|
@footnotes.fetch_note(*category_and_id(**command.data_kwargs!)).to_h
|
|
end
|
|
|
|
def do_render_footnotes(command)
|
|
@config.freeze
|
|
String @footnotes.render_footnotes command.data
|
|
end
|
|
|
|
#########
|
|
# Utils #
|
|
#########
|
|
|
|
def category_and_id(category:, id:) = [category, id]
|
|
end
|
|
end
|