Remove unnecessary code
This commit is contained in:
parent
4f5feacb12
commit
8b16181aea
12 changed files with 3 additions and 510 deletions
|
@ -8,23 +8,16 @@ require 'pathname'
|
|||
require_relative 'referator/command'
|
||||
require_relative 'referator/config'
|
||||
require_relative 'referator/config/categories'
|
||||
require_relative 'referator/config/formats'
|
||||
require_relative 'referator/config/repo'
|
||||
require_relative 'referator/config/scripts'
|
||||
require_relative 'referator/footnotes'
|
||||
require_relative 'referator/machine'
|
||||
require_relative 'referator/note'
|
||||
require_relative 'referator/reference'
|
||||
require_relative 'referator/script'
|
||||
|
||||
module Referator
|
||||
NAME_RE = /\A\w+(_\w+)*\z/
|
||||
SLUG_RE = /\A\w+(-\w+)*\z/
|
||||
|
||||
SCRIPT_ENUMS = %i[format template].sort.freeze
|
||||
SCRIPT_OBJS = %i[note notes].sort.freeze
|
||||
SCRIPT_VARS = [*SCRIPT_ENUMS, *SCRIPT_OBJS].sort.freeze
|
||||
|
||||
def self.validate_name!(name)
|
||||
name.freeze
|
||||
unless name.instance_of? Symbol
|
||||
|
@ -44,14 +37,4 @@ module Referator
|
|||
|
||||
slug
|
||||
end
|
||||
|
||||
def self.validate_script_var!(var)
|
||||
var.freeze
|
||||
unless var.instance_of? Symbol
|
||||
raise TypeError, "Expected #{String}, got #{slug.class}"
|
||||
end
|
||||
raise 'Invalid var' unless SCRIPT_VARS.include? var
|
||||
|
||||
var
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,25 +9,15 @@ module Referator
|
|||
end
|
||||
|
||||
def freeze
|
||||
scripts.freeze
|
||||
categories.freeze
|
||||
formats.freeze
|
||||
repo.freeze
|
||||
super
|
||||
end
|
||||
|
||||
def scripts
|
||||
@scripts ||= Scripts.new self
|
||||
end
|
||||
|
||||
def categories
|
||||
@categories ||= Categories.new self
|
||||
end
|
||||
|
||||
def formats
|
||||
@formats ||= Formats.new self
|
||||
end
|
||||
|
||||
def repo
|
||||
@repo ||= Repo.new self
|
||||
end
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
# 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
|
|
@ -1,62 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Referator
|
||||
class Config
|
||||
class Scripts
|
||||
attr_reader :config
|
||||
|
||||
def initialize(config)
|
||||
self.config = config
|
||||
@scripts = {}
|
||||
end
|
||||
|
||||
def freeze
|
||||
@scripts.freeze
|
||||
super
|
||||
end
|
||||
|
||||
def register(name:, workdir: nil, vars: [], **kwargs)
|
||||
name = String(name).to_sym
|
||||
Referator.validate_name! name
|
||||
raise 'Already exists' if @scripts.key? name
|
||||
|
||||
script = Script.new(
|
||||
**kwargs,
|
||||
workdir: workdir || config.workdir,
|
||||
vars: vars.map { |var| var.instance_of?(String) ? var.to_sym : var },
|
||||
)
|
||||
|
||||
@scripts[name] = script
|
||||
nil
|
||||
end
|
||||
|
||||
def vars!(name, vars)
|
||||
unless vars.instance_of? Array
|
||||
raise TypeError, "Expected #{Array}, got #{vars.class}"
|
||||
end
|
||||
|
||||
Referator.validate_name! name
|
||||
vars.each { |var| Referator.validate_script_var! var }
|
||||
|
||||
script = @scripts[name] or raise 'Invalid script'
|
||||
|
||||
raise 'Invalid vars' unless script.vars.sort == vars.sort
|
||||
end
|
||||
|
||||
def run(name, **vars)
|
||||
vars! name, vars.keys
|
||||
@scripts[name].call(**vars)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def config=(config)
|
||||
unless config.instance_of? Config
|
||||
raise TypeError, "Expected #{Config}, got #{config.class}"
|
||||
end
|
||||
|
||||
@config = config
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -39,11 +39,6 @@ module Referator
|
|||
raise 'Unused note'
|
||||
end
|
||||
|
||||
def render_footnotes(format)
|
||||
format = format.to_sym if format.instance_of? String
|
||||
config.formats.render_footnotes format, notes
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def config=(config)
|
||||
|
|
|
@ -49,44 +49,15 @@ module Referator
|
|||
: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
|
||||
|
|
|
@ -1,128 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Referator
|
||||
class Script
|
||||
attr_reader :workdir, :vars, :args, :env, :stdin
|
||||
|
||||
def initialize(workdir:, vars:, args: [], env: {}, stdin: [])
|
||||
self.workdir = workdir
|
||||
self.vars = vars
|
||||
|
||||
self.args = args
|
||||
self.env = env
|
||||
self.stdin = stdin
|
||||
|
||||
raise 'Invalid script' if self.args.empty?
|
||||
end
|
||||
|
||||
def call(**kwargs)
|
||||
raise 'Invalid vars' unless kwargs.keys.sort == vars
|
||||
|
||||
stdout, status = Open3.capture2(
|
||||
sub_env(**kwargs),
|
||||
*sub_args(**kwargs),
|
||||
chdir: workdir.to_s,
|
||||
stdin_data: sub_stdin(**kwargs),
|
||||
)
|
||||
raise 'Exit code' unless status.success?
|
||||
|
||||
String(stdout).freeze
|
||||
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
|
||||
|
||||
def vars=(vars)
|
||||
@vars = [*vars].uniq.sort.freeze.each do |var|
|
||||
unless var.instance_of? Symbol
|
||||
raise TypeError, "Expected #{Symbol}, got #{var.class}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def args=(args)
|
||||
@args = [*args].map { |arg| Sub.new vars, arg }.freeze
|
||||
end
|
||||
|
||||
def env=(env)
|
||||
@env = Hash(env).to_h do |key, value|
|
||||
[String(key).freeze, Sub.new(vars, value)]
|
||||
end.freeze
|
||||
end
|
||||
|
||||
def stdin=(stdin)
|
||||
@stdin = Sub.new vars, stdin
|
||||
end
|
||||
|
||||
def sub_args(**kwargs)
|
||||
args.map { |sub| sub.call(**kwargs) }.freeze
|
||||
end
|
||||
|
||||
def sub_env(**kwargs)
|
||||
env.transform_values { |sub| sub.call(**kwargs) }.freeze
|
||||
end
|
||||
|
||||
def sub_stdin(**kwargs)
|
||||
stdin.call(**kwargs)
|
||||
end
|
||||
|
||||
class Sub
|
||||
attr_reader :vars, :template
|
||||
|
||||
def initialize(vars, template)
|
||||
self.vars = vars
|
||||
|
||||
self.template =
|
||||
if template.instance_of?(String) ||
|
||||
template.instance_of?(Symbol) ||
|
||||
template.instance_of?(Hash)
|
||||
[template]
|
||||
else
|
||||
template
|
||||
end
|
||||
end
|
||||
|
||||
def call(**kwargs)
|
||||
raise 'Invalid vars' unless kwargs.keys.sort == vars
|
||||
|
||||
template.map do |part|
|
||||
if part.instance_of? String
|
||||
part
|
||||
else
|
||||
kwargs.fetch part
|
||||
end
|
||||
end.join.freeze
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def vars=(vars)
|
||||
@vars = [*vars].uniq.sort.freeze.each do |var|
|
||||
Referator.validate_script_var! var
|
||||
end
|
||||
end
|
||||
|
||||
def template=(template)
|
||||
raise 'Invalid format' unless template.instance_of? Array
|
||||
|
||||
@template = template.map do |part|
|
||||
part = String(part.keys.first).to_sym if part.instance_of? Hash
|
||||
|
||||
unless part.instance_of?(String) ||
|
||||
part.instance_of?(Symbol) && vars.include?(part)
|
||||
raise 'Invalid format'
|
||||
end
|
||||
|
||||
part.freeze
|
||||
end.freeze
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
109
test.rb
109
test.rb
|
@ -41,9 +41,7 @@ REFS = {
|
|||
}.freeze,
|
||||
}.freeze
|
||||
|
||||
workdir = File.expand_path('test', __dir__).freeze
|
||||
|
||||
@stdin, @stdout, @wait_thr = Open3.popen2 EXE, workdir, chdir: __dir__
|
||||
@stdin, @stdout, @wait_thr = Open3.popen2 EXE, __dir__, chdir: __dir__
|
||||
|
||||
@stdin.sync = true
|
||||
@stdout.sync = true
|
||||
|
@ -75,24 +73,6 @@ end
|
|||
cmd('') { |result| raise unless result.nil? }
|
||||
cmd(:NULL) { |result| raise unless result.nil? }
|
||||
|
||||
###################
|
||||
# REGISTER_SCRIPT #
|
||||
###################
|
||||
|
||||
cmd :REGISTER_SCRIPT,
|
||||
{ name: :render,
|
||||
vars: %i[template format notes note],
|
||||
args: ['./render.rb',
|
||||
{ template: nil },
|
||||
{ format: nil }],
|
||||
stdin: ['{',
|
||||
'"notes":',
|
||||
{ notes: nil },
|
||||
',',
|
||||
'"note":',
|
||||
{ note: nil },
|
||||
'}'] }
|
||||
|
||||
#####################
|
||||
# REGISTER_CATEGORY #
|
||||
#####################
|
||||
|
@ -100,13 +80,6 @@ cmd :REGISTER_SCRIPT,
|
|||
cmd :REGISTER_CATEGORY, :self
|
||||
cmd :REGISTER_CATEGORY, :link
|
||||
|
||||
###################
|
||||
# REGISTER_FORMAT #
|
||||
###################
|
||||
|
||||
cmd :REGISTER_FORMAT, { name: :html, script: :render }
|
||||
cmd :REGISTER_FORMAT, { name: :json, script: :render }
|
||||
|
||||
################
|
||||
# REGISTER_REF #
|
||||
################
|
||||
|
@ -114,7 +87,8 @@ cmd :REGISTER_FORMAT, { name: :json, script: :render }
|
|||
cmd :REGISTER_REF, REFS[:self][:foo]
|
||||
cmd :REGISTER_REF, REFS[:link][:example_com]
|
||||
|
||||
cmd(:EXEC, 'conf_register_ref') { |result| raise unless result.nil? }
|
||||
cmd :REGISTER_REF, REFS[:self][:bar]
|
||||
cmd :REGISTER_REF, REFS[:link][:causa_arcana_com]
|
||||
|
||||
#######
|
||||
# REF #
|
||||
|
@ -193,83 +167,6 @@ cmd :FETCH_NOTE, { category: :link, id: :causa_arcana_com } do |result|
|
|||
raise unless result == REFS[:link][:causa_arcana_com].merge('index' => 4)
|
||||
end
|
||||
|
||||
####################
|
||||
# RENDER_FOOTNOTES #
|
||||
####################
|
||||
|
||||
cmd :RENDER_FOOTNOTES, :html do |result|
|
||||
expected = <<~HTML
|
||||
<h2>Self references</h2>
|
||||
|
||||
<ol>
|
||||
<li id="self-blog-foo" value="1">
|
||||
<a href="/blog/foo.html">Foo</a>
|
||||
</li>
|
||||
<li id="self-blog-bar" value="2">
|
||||
<a href="/blog/bar.html">Bar</a>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<h2>Link references</h2>
|
||||
|
||||
<ol>
|
||||
<li id="link-example-com" value="3">
|
||||
<a href="https://example.com">Example Domain</a>
|
||||
</li>
|
||||
<li id="link-causa-arcana-com" value="4">
|
||||
<a href="https://causa-arcana.com">Causa Arcana</a>
|
||||
</li>
|
||||
</ol>
|
||||
HTML
|
||||
|
||||
raise unless result == expected
|
||||
end
|
||||
|
||||
cmd :RENDER_FOOTNOTES, :json do |result|
|
||||
expected = <<~JSON
|
||||
{
|
||||
"self": [
|
||||
{
|
||||
"index": 1,
|
||||
"category": "self",
|
||||
"id": "/blog/foo",
|
||||
"slug": "blog-foo",
|
||||
"url": "/blog/foo.html",
|
||||
"text": "Foo"
|
||||
},
|
||||
{
|
||||
"index": 2,
|
||||
"category": "self",
|
||||
"id": "/blog/bar",
|
||||
"slug": "blog-bar",
|
||||
"url": "/blog/bar.html",
|
||||
"text": "Bar"
|
||||
}
|
||||
],
|
||||
"link": [
|
||||
{
|
||||
"index": 3,
|
||||
"category": "link",
|
||||
"id": "example_com",
|
||||
"slug": "example-com",
|
||||
"url": "https://example.com",
|
||||
"text": "Example Domain"
|
||||
},
|
||||
{
|
||||
"index": 4,
|
||||
"category": "link",
|
||||
"id": "causa_arcana_com",
|
||||
"slug": "causa-arcana-com",
|
||||
"url": "https://causa-arcana.com",
|
||||
"text": "Causa Arcana"
|
||||
}
|
||||
]
|
||||
}
|
||||
JSON
|
||||
|
||||
raise unless result == expected
|
||||
end
|
||||
|
||||
########
|
||||
# EXIT #
|
||||
########
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
# self references
|
||||
register_ref { \
|
||||
"category": "self", \
|
||||
"id": "/blog/bar", \
|
||||
"slug": "blog-bar", \
|
||||
"text": "Bar" \
|
||||
}
|
||||
|
||||
# link references
|
||||
register_ref { \
|
||||
"category": "link", \
|
||||
"id": "causa_arcana_com", \
|
||||
"slug": "causa-arcana-com", \
|
||||
"url": "https://causa-arcana.com", \
|
||||
"text": "Causa Arcana" \
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
<% unless notes['self'].empty? -%>
|
||||
|
||||
<h2>Self references</h2>
|
||||
|
||||
<ol>
|
||||
<% notes['self'].each do |note| -%>
|
||||
<li id="<%= "#{note['category']}-#{note['slug']}" %>" value="<%= note['index'] %>">
|
||||
<a href="<%= note['id'] %>.html"><%= note['text'] %></a>
|
||||
</li>
|
||||
<% end -%>
|
||||
</ol>
|
||||
<% end -%>
|
||||
<%# -%>
|
||||
<% unless notes['link'].empty? -%>
|
||||
|
||||
<h2>Link references</h2>
|
||||
|
||||
<ol>
|
||||
<% notes['link'].each do |note| -%>
|
||||
<li id="<%= "#{note['category']}-#{note['slug']}" %>" value="<%= note['index'] %>">
|
||||
<a href="<%= note['url'] %>"><%= note['text'] %></a>
|
||||
</li>
|
||||
<% end -%>
|
||||
</ol>
|
||||
<% end -%>
|
|
@ -1,26 +0,0 @@
|
|||
{
|
||||
"self": [
|
||||
<%- notes['self'].each_with_index do |note, index| -%>
|
||||
{
|
||||
"index": <%= Integer(note['index']).to_json %>,
|
||||
"category": "self",
|
||||
"id": <%= String(note['id']).to_json %>,
|
||||
"slug": <%= String(note['slug']).to_json %>,
|
||||
"url": <%= "#{note['id']}.html".to_json %>,
|
||||
"text": <%= String(note['text']).to_json %>
|
||||
}<%= ',' unless index == notes['self'].count - 1 %>
|
||||
<%- end -%>
|
||||
],
|
||||
"link": [
|
||||
<%- notes['link'].each_with_index do |note, index| -%>
|
||||
{
|
||||
"index": <%= Integer(note['index']).to_json %>,
|
||||
"category": "link",
|
||||
"id": <%= String(note['id']).to_json %>,
|
||||
"slug": <%= String(note['slug']).to_json %>,
|
||||
"url": <%= String(note['url']).to_json %>,
|
||||
"text": <%= String(note['text']).to_json %>
|
||||
}<%= ',' unless index == notes['link'].count - 1 %>
|
||||
<%- end -%>
|
||||
]
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'erb'
|
||||
require 'json'
|
||||
require 'pathname'
|
||||
|
||||
def template(filename)
|
||||
pathname = Pathname.new(__dir__).join(filename).expand_path.freeze
|
||||
ERB.new(pathname.read, trim_mode: '-').tap do |erb|
|
||||
erb.filename = pathname.to_s.freeze
|
||||
end.freeze
|
||||
end
|
||||
|
||||
name = "#{ARGV[0]}.#{ARGV[1]}.erb".freeze
|
||||
vars = JSON.parse($stdin.read).freeze
|
||||
|
||||
puts template(name).result_with_hash(vars).strip
|
Loading…
Reference in a new issue