Class: Uniword::Generation::DocumentGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/generation/document_generator.rb

Overview

Generates DOCX documents from structured content and style sources.

Combines a style source DOCX (provides styles), a style mapping (maps semantic element names to OOXML style names), and structured content (from YAML/Markdown) into a new DOCX file.

Examples:

Generate a document

generator = DocumentGenerator.new(
  style_source: "iso_template.docx",
  style_mapping: "config/style_mappings/iso_publication.yml"
)
content = StructuredTextParser.parse("content.yml")
generator.generate(content, "output.docx")

Instance Method Summary collapse

Constructor Details

#initialize(style_source:, style_mapping: nil) ⇒ DocumentGenerator

Initialize the generator with style source and optional mapping.

Parameters:

  • style_source (String)

    Path to DOCX file providing styles

  • style_mapping (String, nil) (defaults to: nil)

    Path to YAML style mapping config. Falls back to default mapping when nil.



24
25
26
27
# File 'lib/uniword/generation/document_generator.rb', line 24

def initialize(style_source:, style_mapping: nil)
  @style_source = style_source
  @mapper = StyleMapper.new(style_mapping)
end

Instance Method Details

#generate(structured_content, output_path) ⇒ void

This method returns an undefined value.

Generate a DOCX from structured content elements.

Parameters:

  • structured_content (Array<Hash>)

    Content elements from StructuredTextParser, each with :element, :text, optional :style and :children

  • output_path (String)

    Output DOCX file path



36
37
38
39
40
41
42
43
44
# File 'lib/uniword/generation/document_generator.rb', line 36

def generate(structured_content, output_path)
  builder = create_builder

  structured_content.each do |element|
    add_element(builder, element)
  end

  builder.save(output_path)
end