Class: Coradoc::Docx::Transform::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/docx/transform/context.rb

Overview

Shared context for a single OOXML → CoreModel transform pass.

Holds resolvers, footnote content, image references, and the rule registry. Passed to every rule’s #apply method so rules can delegate sub-transforms (e.g., transform runs inside a paragraph).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(styles_configuration: nil, numbering_configuration: nil, footnotes: {}, registry: nil) ⇒ Context

Returns a new instance of Context.

Parameters:

  • styles_configuration (Object, nil) (defaults to: nil)

    Uniword styles config

  • numbering_configuration (Object, nil) (defaults to: nil)

    Uniword numbering config

  • footnotes (Hash{String => Array}) (defaults to: {})

    footnote id → content paragraphs

  • registry (RuleRegistry) (defaults to: nil)

    rule dispatch registry



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

def initialize(styles_configuration: nil, numbering_configuration: nil,
               footnotes: {}, registry: nil)
  @style_resolver = StyleResolver.new(styles_configuration)
  @numbering_resolver = NumberingResolver.new(numbering_configuration)
  @footnotes = footnotes
  @image_refs = []
  @registry = registry
end

Instance Attribute Details

#footnotesObject (readonly)

Returns the value of attribute footnotes.



13
14
15
# File 'lib/coradoc/docx/transform/context.rb', line 13

def footnotes
  @footnotes
end

#image_refsObject (readonly)

Returns the value of attribute image_refs.



13
14
15
# File 'lib/coradoc/docx/transform/context.rb', line 13

def image_refs
  @image_refs
end

#numbering_resolverObject (readonly)

Returns the value of attribute numbering_resolver.



13
14
15
# File 'lib/coradoc/docx/transform/context.rb', line 13

def numbering_resolver
  @numbering_resolver
end

#registryObject (readonly)

Returns the value of attribute registry.



13
14
15
# File 'lib/coradoc/docx/transform/context.rb', line 13

def registry
  @registry
end

#style_resolverObject (readonly)

Returns the value of attribute style_resolver.



13
14
15
# File 'lib/coradoc/docx/transform/context.rb', line 13

def style_resolver
  @style_resolver
end

Instance Method Details

#footnote_content(id) ⇒ String?

Fetch footnote content by ID

Parameters:

  • id (String, Integer)

    footnote ID

Returns:

  • (String, nil)

    footnote text content



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

def footnote_content(id)
  return nil unless id

  paragraphs = @footnotes[id.to_s]
  return nil unless paragraphs

  paragraphs.map do |para|
    extract_paragraph_text(para)
  end.join("\n")
end

#register_image(ref) ⇒ Object

Record an image reference for later extraction

Parameters:

  • ref (Hash)

    image reference with :src, :alt, etc.



58
59
60
# File 'lib/coradoc/docx/transform/context.rb', line 58

def register_image(ref)
  @image_refs << ref
end

#transform(element) ⇒ Coradoc::CoreModel::Base, ...

Transform an element using the registry

Parameters:

  • element (Object)

    OOXML element

Returns:

  • (Coradoc::CoreModel::Base, Array, String, nil)


33
34
35
36
37
38
# File 'lib/coradoc/docx/transform/context.rb', line 33

def transform(element)
  return nil if element.nil?

  rule = @registry.find_rule(element)
  rule.apply(element, self)
end