Module: Coradoc::Docx::Transform::OrderedContent

Included in:
Rules::HeadingRule, Rules::ListItemRule, Rules::ParagraphRule, Rules::TableRule
Defined in:
lib/coradoc/docx/transform/ordered_content.rb

Overview

Utility for iterating paragraph content in document order.

OOXML paragraphs have separate arrays for runs, hyperlinks, SDTs, etc. but ‘element_order` (from lutaml-model mixed_content) preserves the interleaved sequence. This module provides a single method to walk paragraph content in the correct order.

Used by ParagraphRule, ListItemRule, and HeadingRule.

Instance Method Summary collapse

Instance Method Details

#extract_plain_text(children) ⇒ String

Flatten children array to plain text string.

Parameters:

  • children (Array)

    mixed content (Strings, InlineElements, Blocks)

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
# File 'lib/coradoc/docx/transform/ordered_content.rb', line 46

def extract_plain_text(children)
  children.map do |c|
    case c
    when CoreModel::TextContent then c.text
    when CoreModel::InlineElement then c.content.to_s
    when CoreModel::Block then c.content.to_s
    else c.to_s
    end
  end.join
end

#transform_paragraph_content(paragraph, context) ⇒ Array

Iterate paragraph inline content in document order.

Parameters:

  • paragraph (Uniword::Wordprocessingml::Paragraph)
  • context (Context)

    transform context with registry

Returns:

  • (Array)

    transformed content (Strings, InlineElements, etc.)



20
21
22
23
24
25
26
27
28
# File 'lib/coradoc/docx/transform/ordered_content.rb', line 20

def transform_paragraph_content(paragraph, context)
  order = paragraph.is_a?(Uniword::Wordprocessingml::Paragraph) ? paragraph.element_order : nil

  if order && !order.empty?
    wrap_text_children(transform_ordered(paragraph, order, context))
  else
    wrap_text_children(transform_sequential(paragraph, context))
  end
end

#wrap_text_children(items) ⇒ Array<CoreModel::Base>

Wrap raw strings in TextContent so all children are Base instances.

Parameters:

  • items (Array)

    mixed Strings and CoreModel::Base

Returns:

  • (Array<CoreModel::Base>)


34
35
36
37
38
39
40
# File 'lib/coradoc/docx/transform/ordered_content.rb', line 34

def wrap_text_children(items)
  items.map do |item|
    next nil if item.nil?

    item.is_a?(String) ? CoreModel::TextContent.new(text: item) : item
  end.compact
end