Module: Coradoc::AsciiDoc::Transform::FrontmatterAttributeMap

Defined in:
lib/coradoc/asciidoc/transform/frontmatter_attribute_map.rb

Overview

OCP opt-in bridge mapping AsciiDoc document attributes (‘:author:`, `:revdate:`, etc.) to/from FrontmatterBlock data.

NOT auto-registered. Users opt in by invoking the bridge methods from their pipeline (e.g., from a custom transformer extension or rake task). This honors OCP: core conversion never silently rewrites data; opt-in extensions add behavior explicitly.

MECE: lives in its own file, dispatches on ‘attribute_name` / `frontmatter_key` pairs. Does not touch FrontmatterBlock::Codec (YAML I/O) or SchemaResolver (validation).

Mappings (bidirectional):

| Frontmatter key | AsciiDoc attribute | Notes              |
|-----------------|--------------------|--------------------|
| author          | author             |                    |
| date            | revdate            |                    |
| tags            | tags               | Array <-> space str|
| categories      | categories         | Array <-> space str|

Constant Summary collapse

MAPPINGS =

Single source of truth for the attribute <-> frontmatter mapping. Each tuple: [attribute_name(String), front_key(String), kind(:scalar | :array)]

[
  ['author', 'author', :scalar],
  ['revdate', 'date', :scalar],
  ['tags', 'tags', :array],
  ['categories', 'categories', :array]
].freeze

Class Method Summary collapse

Class Method Details

.attributes_from_block(block) ⇒ Hash{String=>String}

Reverse: walk a FrontmatterBlock’s data and produce a Hash of AsciiDoc document attributes. Unknown entry keys are dropped (only mapped keys are translated).

Parameters:

  • block (Coradoc::CoreModel::FrontmatterBlock)

Returns:

  • (Hash{String=>String})


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/coradoc/asciidoc/transform/frontmatter_attribute_map.rb', line 60

def attributes_from_block(block)
  result = {}
  return result unless block&.data

  MAPPINGS.each do |(_, front_key, _)|
    value = block.data[front_key]
    next if value.nil?

    attr_name = front_key_to_attribute(front_key)
    next unless attr_name

    result[attr_name] = serialize_value(value)
  end
  result
end

.entries_from_attributes(attributes) ⇒ Hash{String=>Object}

Build a frontmatter data hash from an AsciiDoc document attributes Hash. Skips unknown keys and empty values.

Parameters:

  • attributes (Hash{String=>Object})

    AsciiDoc document attributes (e.g., from DocumentAttributes#to_hash)

Returns:

  • (Hash{String=>Object})

    frontmatter data hash



44
45
46
47
48
49
50
51
52
# File 'lib/coradoc/asciidoc/transform/frontmatter_attribute_map.rb', line 44

def entries_from_attributes(attributes)
  attributes = normalize_hash(attributes)
  MAPPINGS.each_with_object({}) do |(attr_name, front_key, kind), h|
    raw = attributes[attr_name]
    next if raw.nil? || raw.to_s.strip.empty?

    h[front_key] = build_value(raw, kind)
  end
end