From 90dae92c305c0ab94dc6faa0ab9f9bb67ab78cbb Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 24 Feb 2024 09:34:05 +0400 Subject: [PATCH] Load config from file --- config/gemini.yml | 6 ++++++ config/http.yml | 15 +++++++++++++++ config/rss.yml | 15 +++++++++++++++ config/word_count.yml | 2 ++ exe/repubmark | 25 +++---------------------- lib/repubmark.rb | 3 ++- lib/repubmark/config.rb | 20 ++++++++++++++++++-- 7 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 config/gemini.yml create mode 100644 config/http.yml create mode 100644 config/rss.yml create mode 100644 config/word_count.yml diff --git a/config/gemini.yml b/config/gemini.yml new file mode 100644 index 0000000..1be26d5 --- /dev/null +++ b/config/gemini.yml @@ -0,0 +1,6 @@ +--- +format: gemtext + +base_url: 'gemini://causa-arcana.com' +current_path: '/xx/blog/xxxx/xx/xx/xxxxx.xxx' +relative_urls: true diff --git a/config/http.yml b/config/http.yml new file mode 100644 index 0000000..9bd9d1d --- /dev/null +++ b/config/http.yml @@ -0,0 +1,15 @@ +--- +format: html + +base_url: 'https://causa-arcana.com' +current_path: '/xx/blog/xxxx/xx/xx/xxxxx.xxx' +relative_urls: true + +css_class_annotation: 'nice-annotation' +css_class_blockquote_figure: 'nice-blockquote' +css_class_figure_self: 'nice-figure' +css_class_figure_wrap: 'd-flex justify-content-center' +css_class_figures_left: 'col-xl-6' +css_class_figures_right: 'col-xl-6' +css_class_figures_wrap: 'row' +css_class_iframe_wrap: 'ratio ratio-16x9' diff --git a/config/rss.yml b/config/rss.yml new file mode 100644 index 0000000..ef1f34a --- /dev/null +++ b/config/rss.yml @@ -0,0 +1,15 @@ +--- +format: html + +base_url: 'https://causa-arcana.com' +current_path: '/xx/blog/xxxx/xx/xx/xxxxx.xxx' +relative_urls: false + +css_class_annotation: 'nice-annotation' +css_class_blockquote_figure: 'nice-blockquote' +css_class_figure_self: 'nice-figure' +css_class_figure_wrap: 'd-flex justify-content-center' +css_class_figures_left: 'col-xl-6' +css_class_figures_right: 'col-xl-6' +css_class_figures_wrap: 'row' +css_class_iframe_wrap: 'ratio ratio-16x9' diff --git a/config/word_count.yml b/config/word_count.yml new file mode 100644 index 0000000..1c93588 --- /dev/null +++ b/config/word_count.yml @@ -0,0 +1,2 @@ +--- +format: word_count diff --git a/exe/repubmark b/exe/repubmark index 6d3b236..b8eda74 100755 --- a/exe/repubmark +++ b/exe/repubmark @@ -8,35 +8,16 @@ require 'bundler/setup' require 'repubmark' -format = String(ARGV[0]).to_sym -unless Repubmark::FORMATS.include? format - raise "Invalid format: #{format.inspect}" -end - -template = $stdin.read.freeze - -config = Repubmark::Config.new( - base_url: 'https://causa-arcana.com', - css_class_annotation: 'nice-annotation', - css_class_blockquote_figure: 'nice-blockquote', - css_class_figure_self: 'nice-figure', - css_class_figure_wrap: 'd-flex justify-content-center', - css_class_figures_left: 'col-xl-6', - css_class_figures_right: 'col-xl-6', - css_class_figures_wrap: 'row', - css_class_iframe_wrap: 'ratio ratio-16x9', - current_path: '/xx/blog/xxxx/xx/xx/xxx.xxx', - relative_urls: false, -) - +config = Repubmark::Config.load_file String ARGV[0] $article = Repubmark::Elems::Article.new config +template = $stdin.read.freeze context = BasicObject.new def context.article = $article context.instance_eval template, __FILE__, __LINE__ output = - case format + case config.format when :word_count then $article.word_count when :html then $article.to_html when :gemtext then $article.to_gemtext diff --git a/lib/repubmark.rb b/lib/repubmark.rb index e70ed22..83aa5ef 100644 --- a/lib/repubmark.rb +++ b/lib/repubmark.rb @@ -5,6 +5,7 @@ require 'forwardable' require 'open3' require 'pathname' require 'uri' +require 'yaml' require_relative 'repubmark/config' require_relative 'repubmark/highlight' @@ -56,5 +57,5 @@ require_relative 'repubmark/elems/text' require_relative 'repubmark/elems/link' module Repubmark - FORMATS = %i[gemtext html].freeze + FORMATS = %i[gemtext html word_count].freeze end diff --git a/lib/repubmark/config.rb b/lib/repubmark/config.rb index b9d14de..7a3f6ea 100644 --- a/lib/repubmark/config.rb +++ b/lib/repubmark/config.rb @@ -18,8 +18,15 @@ module Repubmark relative_urls ].freeze - def initialize(**kwargs) - raise unless (kwargs.keys.sort - OPTIONAL_KEYS).empty? + attr_reader :format + + def self.load_file(filename) + new(**YAML.safe_load_file(filename).transform_keys(&:to_sym)) + end + + def initialize(format:, **kwargs) + self.format = format + raise unless (kwargs.keys - OPTIONAL_KEYS).empty? @kwargs = kwargs.freeze end @@ -27,5 +34,14 @@ module Repubmark def [](key) OPTIONAL_KEYS.include?(key) ? @kwargs[key] : @kwargs.fetch(key) end + + private + + def format=(format) + format = format.to_sym if format.instance_of? String + raise 'Invalid format' unless FORMATS.include? format + + @format = format + end end end