Module: Coradoc::Docx::Transform::FrontmatterCoreProperties

Defined in:
lib/coradoc/docx/transform/frontmatter_core_properties.rb

Overview

Single source of truth for FrontmatterBlock -> OOXML core properties mapping. (MECE: DOCX-specific concerns live in the DOCX gem; CoreModel has no knowledge of OOXML.)

Uniword's DocumentBuilder exposes setter methods (title, author, subject, keywords, description, date_field) that write the docProps/core.xml Dublin Core elements. This module reads FrontmatterBlock entries and invokes the right setters, so the mapping table lives in exactly one place.

Mapping table (single source of truth):

| Frontmatter key       | OOXML element        | Builder method |
|-----------------------|----------------------|----------------|
| title                 | dc:title             | title          |
| author                | dc:creator           | author         |
| date                  | dc:date              | created        |
| description, excerpt  | dc:description       | description    |
| subject               | dc:subject           | subject        |
| tags, categories      | cp:keywords          | keywords       |

Constant Summary collapse

DESCRIPTION_KEYS =
%w[description excerpt].freeze
KEYWORDS_KEYS =
%w[tags categories].freeze

Class Method Summary collapse

Class Method Details

.apply(builder, block) ⇒ void

This method returns an undefined value.

Apply FrontmatterBlock data to a Uniword DocumentBuilder. Skips nil/empty values. Does not overwrite builder values that the caller has set explicitly (caller invokes this AFTER setting document.title, for example).

Parameters:

  • builder (Uniword::Builder::DocumentBuilder)
  • block (Coradoc::CoreModel::FrontmatterBlock, nil)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/coradoc/docx/transform/frontmatter_core_properties.rb', line 39

def apply(builder, block)
  return unless block.is_a?(Coradoc::CoreModel::FrontmatterBlock)

  data = block.data || {}

  if (v = find_first_scalar(data, KEYWORDS_KEYS))
    builder.keywords(v)
  end
  if (v = find_first_scalar(data, %w[author]))
    builder.author(v)
  end
  if (v = find_first_scalar(data, %w[subject]))
    builder.subject(v)
  end
  if (v = find_first_scalar(data, DESCRIPTION_KEYS))
    builder.description(v)
  end
  if (v = find_first_scalar(data, %w[date]))
    builder.created(v)
  end
end

.extract(document) ⇒ Coradoc::CoreModel::FrontmatterBlock?

Locate the FrontmatterBlock in a DocumentElement's children, if any.

Parameters:

  • document (Coradoc::CoreModel::DocumentElement)

Returns:

  • (Coradoc::CoreModel::FrontmatterBlock, nil)


66
67
68
69
70
# File 'lib/coradoc/docx/transform/frontmatter_core_properties.rb', line 66

def extract(document)
  return nil unless document.is_a?(Coradoc::CoreModel::DocumentElement)

  Array(document.children).find { |c| c.is_a?(Coradoc::CoreModel::FrontmatterBlock) }
end