referator/lib/referator/machine.rb

98 lines
2.0 KiB
Ruby

# 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)
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_exec(command)
File.open File.expand_path command.data, workdir do |file|
code = ''
loop do
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_ref(command)
@config.repo.register_ref(**command.data_kwargs!)
nil
end
############
# Creation #
############
def do_make_ref(command)
@config.freeze
String @footnotes.make_ref(**command.data_kwargs!)
end
#############
# Rendering #
#############
def do_render_footnotes(_command)
@config.freeze
String @footnotes.render_footnotes
end
#########
# Utils #
#########
def category_and_id(category:, id:) = [category, id]
end
end