Class: JekyllMermaidPrebuild::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-mermaid-prebuild/configuration.rb

Overview

Configuration wrapper for site config

Constant Summary collapse

DEFAULT_OUTPUT_DIR =
"assets/svg"
CACHE_DIR =
".jekyll-cache/jekyll-mermaid-prebuild"
DEFAULT_CHART_BG_LIGHT =
"white"
DEFAULT_CHART_BG_DARK =
"black"
DEFAULT_PREFERS_COLOR_SCHEME_MODE =

Parsed mode when PCS is omitted, invalid, or empty (not the YAML string "light").

:light
MAX_CHART_BACKGROUND_LENGTH =
256
INVALID_CHART_BACKGROUND =
/[\x00-\x1f"'<>;`\\]/
PREFERS_COLOR_SCHEME_YAML_KEY =

YAML keys under mermaid_prebuild — hyphenated to align with CSS (@media (prefers-color-scheme), background-color).

"prefers-color-scheme"
BACKGROUND_COLOR_YAML_KEY =
"background-color"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Configuration

Initialize configuration from Jekyll site

Parameters:

  • site (Jekyll::Site)

    the Jekyll site



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 25

def initialize(site)
  config = site.config["mermaid_prebuild"] || {}
  @output_dir = parse_output_dir(config["output_dir"])
  @enabled = config.fetch("enabled", true)
  pcs_raw = config[PREFERS_COLOR_SCHEME_YAML_KEY]
  parse_prefers_color_scheme(pcs_raw)

  pp = config["postprocessing"] || {}
  @text_centering = pp.fetch("text_centering", true)
  @overflow_protection = pp.fetch("overflow_protection", true)
  @edge_label_padding = parse_edge_label_padding(pp["edge_label_padding"])
  @emoji_width_compensation = parse_emoji_width_compensation(pp["emoji_width_compensation"])
end

Instance Attribute Details

#chart_background_darkObject (readonly)

Returns the value of attribute chart_background_dark.



19
20
21
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 19

def chart_background_dark
  @chart_background_dark
end

#chart_background_lightObject (readonly)

Returns the value of attribute chart_background_light.



19
20
21
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 19

def chart_background_light
  @chart_background_light
end

#edge_label_paddingObject (readonly)

Returns the value of attribute edge_label_padding.



19
20
21
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 19

def edge_label_padding
  @edge_label_padding
end

#emoji_width_compensationObject (readonly)

Returns the value of attribute emoji_width_compensation.



19
20
21
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 19

def emoji_width_compensation
  @emoji_width_compensation
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



19
20
21
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 19

def output_dir
  @output_dir
end

#overflow_protectionObject (readonly)

Returns the value of attribute overflow_protection.



19
20
21
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 19

def overflow_protection
  @overflow_protection
end

#prefers_color_schemeObject (readonly)

Returns the value of attribute prefers_color_scheme.



19
20
21
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 19

def prefers_color_scheme
  @prefers_color_scheme
end

#text_centeringObject (readonly)

Returns the value of attribute text_centering.



19
20
21
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 19

def text_centering
  @text_centering
end

Instance Method Details

#cache_dirString

Get the cache directory path

Returns:

  • (String)

    cache directory path



49
50
51
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 49

def cache_dir
  CACHE_DIR
end

#chart_background_invalid?(value) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 137

def chart_background_invalid?(value)
  INVALID_CHART_BACKGROUND.match?(value)
end

#coerce_chart_background(value, default, label) ⇒ String

Returns frozen sanitized CSS fragment.

Parameters:

  • value (Object)

    raw color string or nil (use default)

  • default (String)

    fallback literal

  • label (String)

    "light" or "dark" for logging

Returns:

  • (String)

    frozen sanitized CSS fragment



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 112

def coerce_chart_background(value, default, label)
  return finalize_background(default) if value.nil?

  str = value.to_s.strip
  if str.empty?
    Jekyll.logger.warn "MermaidPrebuild:",
                       "Invalid chart background (#{label}): empty string; using #{default.inspect}"
    return finalize_background(default)
  end

  if str.length > MAX_CHART_BACKGROUND_LENGTH
    Jekyll.logger.warn "MermaidPrebuild:",
                       "Invalid chart background (#{label}): value too long; using #{default.inspect}"
    return finalize_background(default)
  end

  if chart_background_invalid?(str)
    Jekyll.logger.warn "MermaidPrebuild:",
                       "Invalid chart background (#{label}): disallowed characters; using #{default.inspect}"
    return finalize_background(default)
  end

  str.freeze
end

#config_hash_fetch(hash, key) ⇒ Object?

Read a config key from a Hash (string key as in YAML, or matching Symbol).

Parameters:

  • hash (Hash)
  • key (String)

    exact key (e.g. "mode", "background-color", "light")

Returns:

  • (Object, nil)


102
103
104
105
106
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 102

def config_hash_fetch(hash, key)
  return nil unless hash.is_a?(Hash)

  hash.fetch(key) { hash.fetch(key.to_sym, nil) }
end

#enabled?Boolean

Check if the plugin is enabled

Returns:

  • (Boolean)

    true if enabled



42
43
44
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 42

def enabled?
  @enabled
end

#finalize_background(value) ⇒ String

Returns frozen copy.

Parameters:

  • value (String)

Returns:

  • (String)

    frozen copy



143
144
145
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 143

def finalize_background(value)
  value.to_s.dup.freeze
end

#normalize_prefers_mode(raw) ⇒ Symbol

Returns :light, :dark, or :auto.

Parameters:

  • raw (Object)

Returns:

  • (Symbol)

    :light, :dark, or :auto



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 80

def normalize_prefers_mode(raw)
  s = raw.to_s.strip.downcase
  return DEFAULT_PREFERS_COLOR_SCHEME_MODE if s.empty?

  case s
  when "light" then :light
  when "dark" then :dark
  when "auto" then :auto
  else
    Jekyll.logger.warn(
      "MermaidPrebuild:",
      "Invalid #{PREFERS_COLOR_SCHEME_YAML_KEY} mode #{raw.inspect}; using light"
    )
    DEFAULT_PREFERS_COLOR_SCHEME_MODE
  end
end

#parse_edge_label_padding(value) ⇒ Numeric

Returns non-negative padding in SVG user units; 0 means disabled.

Parameters:

  • value (Object)

    raw config (numeric or off)

Returns:

  • (Numeric)

    non-negative padding in SVG user units; 0 means disabled



169
170
171
172
173
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 169

def parse_edge_label_padding(value)
  return 0 unless value.is_a?(Numeric)

  value.negative? ? 0 : value
end

#parse_emoji_width_compensation(value) ⇒ Hash<String, Boolean>

Returns a frozen Hash of diagram type (string) => boolean. Non-hash values are rejected → {}.

Parameters:

  • value (Object)

    raw config value

Returns:

  • (Hash<String, Boolean>)

    frozen hash, empty if invalid/absent



151
152
153
154
155
156
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 151

def parse_emoji_width_compensation(value)
  return {}.freeze unless value.is_a?(Hash)

  result = value.transform_keys(&:to_s).transform_values { |v| v == true }
  result.freeze
end

#parse_output_dir(dir) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 158

def parse_output_dir(dir)
  return DEFAULT_OUTPUT_DIR unless dir.is_a?(String)

  dir = dir.strip
  return DEFAULT_OUTPUT_DIR if dir.empty?

  dir.split("/").reject(&:empty?).join("/")
end

#parse_prefers_color_scheme(value) ⇒ void

This method returns an undefined value.

Parse the prefers-color-scheme block (see PREFERS_COLOR_SCHEME_YAML_KEY) from a Hash only (mode + optional background-color map). Non-Hash values fall back to :light and default backgrounds with a warning.

Parameters:

  • value (Object)

    raw site config value



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jekyll-mermaid-prebuild/configuration.rb', line 59

def parse_prefers_color_scheme(value)
  if !value.nil? && !value.is_a?(Hash)
    Jekyll.logger.warn(
      "MermaidPrebuild:",
      "Invalid #{PREFERS_COLOR_SCHEME_YAML_KEY} (expected a Hash); " \
      "using light mode and default backgrounds"
    )
  end

  mode_raw = config_hash_fetch(value, "mode")
  @prefers_color_scheme = normalize_prefers_mode(mode_raw)

  bg_container = config_hash_fetch(value, BACKGROUND_COLOR_YAML_KEY)
  light_raw = config_hash_fetch(bg_container, "light")
  dark_raw = config_hash_fetch(bg_container, "dark")
  @chart_background_light = coerce_chart_background(light_raw, DEFAULT_CHART_BG_LIGHT, "light")
  @chart_background_dark = coerce_chart_background(dark_raw, DEFAULT_CHART_BG_DARK, "dark")
end