Class: Metanorma::Html::Theme

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/metanorma/html/theme.rb

Constant Summary collapse

THEMES_DIR =
File.join(File.dirname(__FILE__), "..", "..", "..", "data",
"themes")
TEMPLATES_ROOT =
File.join(File.dirname(__FILE__), "templates")

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.extra_themes_dirsObject (readonly)

Returns the value of attribute extra_themes_dirs.



130
131
132
# File 'lib/metanorma/html/theme.rb', line 130

def extra_themes_dirs
  @extra_themes_dirs
end

Instance Attribute Details

#theme_dirObject

--- Non-YAML state (set programmatically) ---



121
122
123
# File 'lib/metanorma/html/theme.rb', line 121

def theme_dir
  @theme_dir
end

Class Method Details

.from_directory(path, flavor, themes_dir = THEMES_DIR) ⇒ Object



157
158
159
160
161
# File 'lib/metanorma/html/theme.rb', line 157

def self.from_directory(path, flavor, themes_dir = THEMES_DIR)
  theme = from_yaml(File.read(path))
  theme.theme_dir = File.join(themes_dir, flavor.to_s)
  theme
end

.from_file(path) ⇒ Object



153
154
155
# File 'lib/metanorma/html/theme.rb', line 153

def self.from_file(path)
  from_yaml(File.read(path))
end

.from_theme_dir(dir) ⇒ Object

Load a theme from an external theme directory: dir/theme.yaml with optional assets/, templates/ and custom.css resolved relative to dir. This is the entry point for organization-local themes living outside any gem.



167
168
169
170
171
# File 'lib/metanorma/html/theme.rb', line 167

def self.from_theme_dir(dir)
  theme = from_yaml(File.read(File.join(dir, "theme.yaml")))
  theme.theme_dir = dir
  theme
end

.load(flavor) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/metanorma/html/theme.rb', line 141

def self.load(flavor)
  themes_dirs.each do |themes_dir|
    dir_theme = File.join(themes_dir, flavor.to_s, "theme.yaml")
    flat_theme = File.join(themes_dir, "#{flavor}.yaml")

    return from_directory(dir_theme, flavor, themes_dir) if File.exist?(dir_theme)
    return from_file(flat_theme) if File.exist?(flat_theme)
  end

  new
end

.register_themes_dir(dir) ⇒ Object



132
133
134
# File 'lib/metanorma/html/theme.rb', line 132

def register_themes_dir(dir)
  @extra_themes_dirs.unshift(File.expand_path(dir))
end

.themes_dirsObject



136
137
138
# File 'lib/metanorma/html/theme.rb', line 136

def themes_dirs
  @extra_themes_dirs + [THEMES_DIR]
end

Instance Method Details

#logosObject



173
174
175
# File 'lib/metanorma/html/theme.rb', line 173

def logos
  logos_light
end

#resolve_asset(filename) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/metanorma/html/theme.rb', line 206

def resolve_asset(filename)
  if theme_assets_dir
    flavor_path = File.join(theme_assets_dir, filename)
    return flavor_path if File.exist?(flavor_path)
  end
  nil
end

#resolve_template(template_name) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/metanorma/html/theme.rb', line 198

def resolve_template(template_name)
  if theme_templates_dir
    flavor_path = File.join(theme_templates_dir, template_name)
    return flavor_path if File.exist?(flavor_path)
  end
  File.join(TEMPLATES_ROOT, template_name)
end

#theme_assets_dirObject



184
185
186
187
188
189
# File 'lib/metanorma/html/theme.rb', line 184

def theme_assets_dir
  return nil unless theme_dir

  dir = File.join(theme_dir, "assets")
  File.directory?(dir) ? dir : nil
end

#theme_css_pathObject



191
192
193
194
195
196
# File 'lib/metanorma/html/theme.rb', line 191

def theme_css_path
  return nil unless theme_dir

  path = File.join(theme_dir, "custom.css")
  File.exist?(path) ? path : nil
end

#theme_templates_dirObject



177
178
179
180
181
182
# File 'lib/metanorma/html/theme.rb', line 177

def theme_templates_dir
  return nil unless theme_dir

  dir = File.join(theme_dir, "templates")
  File.directory?(dir) ? dir : nil
end

#to_cssObject



271
272
273
# File 'lib/metanorma/html/theme.rb', line 271

def to_css
  to_css_root + to_css_extras
end

#to_css_extrasObject



263
264
265
266
267
268
269
# File 'lib/metanorma/html/theme.rb', line 263

def to_css_extras
  css = ""
  css += ".title-section::before { content: \"\"; position: absolute; inset: 0; pointer-events: none; #{cover_before_bg} }\n" if cover_before_bg
  css += ".title-section::after { #{cover_after_bg} }\n" if cover_after_bg
  css += extra_css.to_s if extra_css
  css
end

#to_css_rootObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/metanorma/html/theme.rb', line 214

def to_css_root
  root = <<~CSS
    :root {
      --mn-primary: #{primary};
      --mn-accent: #{accent};
      --mn-gradient: #{gradient};
      --mn-primary-light: #{primary_light};
      --mn-accent-light: #{accent_light};
      --mn-primary-dark: #{primary_dark};
      --color-text: #{text_color};
      --color-text-light: #{text_light};
      --color-text-muted: #{text_muted};
      --color-bg: #{bg};
      --color-bg-light: #{bg_light};
      --color-border: #{border};
      --color-sidebar-bg: #{sidebar_bg};
      --font-body: #{font_body};
      --font-sans: #{font_sans};
      --font-mono: #{font_mono};
      --content-max-width: #{content_max_width};
      --sidebar-width: #{sidebar_width};
      --header-height: #{header_height};
      --radius-sm: #{radius_sm};
      --radius-md: #{radius_md};
      --shadow-sm: #{shadow_sm};
      --shadow-md: #{shadow_md};
      --note-bg: #{note_bg};
      --note-border: #{note_border};
      --note-color: #{note_color};
      --example-bg: #{example_bg};
      --example-border: #{example_border};
      --example-color: #{example_color};
      --admonition-bg: #{admonition_bg};
      --admonition-border: #{admonition_border};
      --admonition-color: #{admonition_color};
  CSS
  root += "    --mn-accent-deep: #{accent_deep};\n" if accent_deep
  root += "    --mn-warm: #{warm};\n" if warm
  root += "    --mn-warm-light: #{warm_light};\n" if warm_light
  root += "    --mn-header-bg: #{header_background};\n" if header_background
  root += "    --mn-cover-bg: #{cover_background};\n" if cover_background
  root += "    --mn-progress-color: #{progress_bar_color};\n" if progress_bar_color
  root += "    --mn-footer-border: #{footer_border_color};\n" if footer_border_color
  root += "    --mn-cover-separator: #{cover_separator_color};\n" if cover_separator_color
  root += "  }\n"
  root += generate_dark_mode_block if dark_bg
  root
end