Module: Coradoc::Mirror::FrontmatterQuery
- Defined in:
- lib/coradoc/mirror/frontmatter_query.rb
Overview
Public read-API: extract a flat Ruby Hash from a Mirror document’s frontmatter node.
Why this exists: site generators (e.g. metanorma.org’s convert-adoc.rb) need frontmatter as a plain Hash for templating VitePress/Jekyll frontmatter output. Without this, callers would either (a) re-parse the source YAML — violating FrontmatterBlock::Codec’s “single source of truth” rule — or (b) hand-walk the typed FrontmatterEntry / FrontmatterValue tree themselves, duplicating the reverse builder’s logic. This module is the single entry point for both problems.
Reuses FrontmatterTreeToHash — same translator the reverse builder uses — so the read-path is shared (DRY/MECE).
Class Method Summary collapse
- .find_frontmatter(mirror_doc) ⇒ Object
-
.has_frontmatter?(mirror_doc) ⇒ Boolean
True if the document carries a frontmatter node with at least one entry.
-
.to_hash(mirror_doc) ⇒ Hash{String,Object}
Flat key→value mapping; empty Hash if the document has no frontmatter node or no entries.
Class Method Details
.find_frontmatter(mirror_doc) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/coradoc/mirror/frontmatter_query.rb', line 40 def find_frontmatter(mirror_doc) return nil if mirror_doc.nil? content = mirror_doc.content return nil unless content content.find { |node| node.is_a?(Node) && node.type == 'frontmatter' } end |
.has_frontmatter?(mirror_doc) ⇒ Boolean
Returns true if the document carries a frontmatter node with at least one entry.
35 36 37 38 |
# File 'lib/coradoc/mirror/frontmatter_query.rb', line 35 def has_frontmatter?(mirror_doc) frontmatter = find_frontmatter(mirror_doc) !frontmatter.nil? && !(frontmatter.attrs&.entries || []).empty? end |
.to_hash(mirror_doc) ⇒ Hash{String,Object}
Returns flat key→value mapping; empty Hash if the document has no frontmatter node or no entries.
24 25 26 27 28 29 30 |
# File 'lib/coradoc/mirror/frontmatter_query.rb', line 24 def to_hash(mirror_doc) frontmatter = find_frontmatter(mirror_doc) return {} unless frontmatter entries = frontmatter.attrs&.entries || [] FrontmatterTreeToHash.to_hash(entries) end |