Improve eval, remove globals

This commit is contained in:
Alex Kotov 2024-02-24 06:46:49 +04:00
parent 1a233ac6aa
commit d66fc6eda1
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
1 changed files with 17 additions and 18 deletions

View File

@ -10,12 +10,12 @@ require 'repubmark'
FORMATS = %w[word_count html gemtext].freeze
$format = String(ARGV[0]).freeze
raise "Invalid format: #{$format.inspect}" unless FORMATS.include? $format
format = String(ARGV[0]).freeze
raise "Invalid format: #{format.inspect}" unless FORMATS.include? format
$template = $stdin.read.freeze
template = $stdin.read.freeze
$config = Repubmark::Config.new(
config = Repubmark::Config.new(
base_url: 'https://causa-arcana.com',
css_class_annotation: 'nice-annotation',
css_class_blockquote_figure: 'nice-blockquote',
@ -29,18 +29,17 @@ $config = Repubmark::Config.new(
relative_urls: false,
)
$article = Repubmark::Elems::Article.new $config
$article.tap do |article| # rubocop:disable Lint/UnusedBlockArgument
eval $template # rubocop:disable Security/Eval
end
$article = Repubmark::Elems::Article.new config
case $format
when 'word_count'
puts $article.word_count
when 'html'
puts $article.to_html.strip
when 'gemtext'
puts $article.to_gemtext.strip
else
raise 'Unknown blog format'
end
context = BasicObject.new
def context.article = $article
context.instance_eval template, __FILE__, __LINE__
output =
case format
when 'word_count' then $article.word_count
when 'html' then $article.to_html
when 'gemtext' then $article.to_gemtext
end.to_s.strip
puts output unless output.empty?