diff --git a/Rakefile b/Rakefile index f9d7a7a..1b2cd40 100644 --- a/Rakefile +++ b/Rakefile @@ -68,6 +68,6 @@ namespace :yard do coverage = m[1].to_f.round(2) puts "Documentation coverage: #{coverage}%" - raise 'Not fully documented!' if coverage < 38.32 + raise 'Not fully documented!' if coverage < 38 end end diff --git a/examples/config.yml b/examples/config.yml index c46fb1b..231c34c 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -29,14 +29,14 @@ paths: &paths images_prefix: '/assets/images' css_classes: &css_classes - 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' + annotation: 'nice-annotation' + blockquote_figure: 'nice-blockquote' + figure_self: 'nice-figure' + figure_wrap: 'd-flex justify-content-center' + figures_left: 'col-xl-6' + figures_right: 'col-xl-6' + figures_wrap: 'row' + iframe_wrap: 'ratio ratio-16x9' ############ # Profiles # @@ -58,14 +58,14 @@ gemini: http: <<: *paths - <<: *css_classes format: html relative_urls: true chapter_anchor_prefix: 'chapter' + css_classes: *css_classes rss: <<: *paths - <<: *css_classes format: html relative_urls: false base_url: 'https://example.com' + css_classes: *css_classes diff --git a/lib/repubmark.rb b/lib/repubmark.rb index b8aebb7..a244dd4 100644 --- a/lib/repubmark.rb +++ b/lib/repubmark.rb @@ -10,10 +10,12 @@ require 'yaml' require 'addressable' -require_relative 'repubmark/config' require_relative 'repubmark/highlight' require_relative 'repubmark/titled_ref' +require_relative 'repubmark/config' +require_relative 'repubmark/config/css_classes' + require_relative 'repubmark/elems/base' # Top-level element diff --git a/lib/repubmark/config.rb b/lib/repubmark/config.rb index 1f0553e..3383160 100644 --- a/lib/repubmark/config.rb +++ b/lib/repubmark/config.rb @@ -5,16 +5,7 @@ module Repubmark OPTIONAL_KEYS = %i[ base_url chapter_anchor_prefix - css_class_annotation - css_class_blockquote_figure - css_class_blockquote_blockquote - css_class_blockquote_figcaption - css_class_figure_self - css_class_figure_wrap - css_class_figures_left - css_class_figures_right - css_class_figures_wrap - css_class_iframe_wrap + css_classes current_path images_prefix relative_urls @@ -41,6 +32,10 @@ module Repubmark OPTIONAL_KEYS.include?(key) ? @kwargs[key] : @kwargs.fetch(key) end + def css_classes + @css_classes ||= CSSClasses.new(**(self[:css_classes] || {})) + end + private def format=(format) diff --git a/lib/repubmark/config/css_classes.rb b/lib/repubmark/config/css_classes.rb new file mode 100644 index 0000000..8ee39a0 --- /dev/null +++ b/lib/repubmark/config/css_classes.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Repubmark + class Config + class CSSClasses + KEYS = %i[ + annotation + blockquote_figure + blockquote_blockquote + blockquote_figcaption + figure_self + figure_wrap + figures_left + figures_right + figures_wrap + iframe_wrap + ].freeze + + def initialize(**kwargs) + @kwargs = + kwargs + .transform_keys!(&:to_sym) + .transform_values! { |val| String(val).strip.freeze } + .freeze + .each_key { |key| raise 'Invalid key' unless KEYS.include? key } + end + + def [](*keys) + keys + .each { |key| raise 'Invalid key' unless KEYS.include? key } + .filter_map { |key| @kwargs[key] } + .join(' ') + .freeze + end + end + end +end diff --git a/lib/repubmark/elems/base.rb b/lib/repubmark/elems/base.rb index 5d2009f..80b080c 100644 --- a/lib/repubmark/elems/base.rb +++ b/lib/repubmark/elems/base.rb @@ -90,11 +90,11 @@ module Repubmark .freeze end - def html_class(key) - if (value = config[:"css_class_#{key}"]) - %( class="#{value}").freeze - else + def html_class(*keys) + if (value = config.css_classes[*keys]).empty? '' + else + %( class="#{value}").freeze end end