From d66fc6eda1d3145cd41cf26d3475023de6246664 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 24 Feb 2024 06:46:49 +0400 Subject: [PATCH] Improve eval, remove globals --- exe/repubmark | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/exe/repubmark b/exe/repubmark index fca1e0c..46c0dea 100755 --- a/exe/repubmark +++ b/exe/repubmark @@ -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?