Class: Uniword::Wordprocessingml::Run

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/uniword/wordprocessingml/run.rb

Overview

Text run - inline text with formatting

Generated from OOXML schema: wordprocessingml.yml Element: <w:r>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Run

Initialize with text normalization With Text.cast defined, lutaml-model handles String->Text conversion during attribute assignment via attr.cast_value()



74
75
76
# File 'lib/uniword/wordprocessingml/run.rb', line 74

def initialize(attrs = {})
  super
end

Instance Attribute Details

#parent_paragraphObject

Non-serialized runtime reference to parent paragraph for style inheritance



36
37
38
# File 'lib/uniword/wordprocessingml/run.rb', line 36

def parent_paragraph
  @parent_paragraph
end

Instance Method Details

#accept(visitor) ⇒ void

This method returns an undefined value.

Accept a visitor (Visitor pattern)

Parameters:

  • visitor (BaseVisitor)

    The visitor to accept



82
83
84
# File 'lib/uniword/wordprocessingml/run.rb', line 82

def accept(visitor)
  visitor.visit_run(self)
end

#effective_run_propertiesRunProperties?

Get effective run properties including inherited from paragraph style

This implements OOXML style inheritance where run properties are resolved per-property with explicit values taking precedence over inherited style values.

Priority order (per property):

  1. Explicit run property (highest)
  2. Paragraph style's run property (from rPr in style definition)
  3. Style's basedOn chain (cascade up to Normal/Default)

Returns:



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/uniword/wordprocessingml/run.rb', line 152

def effective_run_properties
  inherited = inherited_from_style

  # If no explicit properties, return inherited directly
  return inherited unless properties

  # If no inherited properties, return explicit directly
  return properties unless inherited

  # Merge: explicit overrides inherited per-property
  merge_properties(inherited, properties)
end

#font_sizeInteger?

Convenience accessor for font size in points (half-points in OOXML) Returns effective size from inherited style if not explicitly set

Returns:

  • (Integer, nil)

    Font size in points, or nil



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/uniword/wordprocessingml/run.rb', line 169

def font_size
  effective = effective_run_properties
  return nil unless effective

  size_val = effective.size
  return nil unless size_val

  # size is stored in half-points, convert to points
  raw = size_val.value
  return nil unless raw

  half_pts = raw.to_i
  half_pts.positive? ? half_pts / 2 : nil
end

#inspectString

Custom inspect for readable output

Returns:

  • (String)

    Human-readable representation



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/uniword/wordprocessingml/run.rb', line 126

def inspect
  text_preview = text_string
  text_preview = "#{text_preview[0, 37]}..." if text_preview.length > 40

  flags = []
  flags << "bold" if properties&.bold&.value == true
  flags << "italic" if properties&.italic&.value == true
  if flags.any?
    "#<#{self.class} text=\"#{text_preview}\", #{flags.join(', ')}>"
  else
    "#<#{self.class} text=\"#{text_preview}\">"
  end
end

#substitute(pattern, replacement) ⇒ self

Substitute text in run

Parameters:

  • pattern (Regexp, String)

    Pattern to match

  • replacement (String)

    Replacement text

Returns:

  • (self)

    For method chaining



98
99
100
101
102
103
104
105
# File 'lib/uniword/wordprocessingml/run.rb', line 98

def substitute(pattern, replacement)
  Array(text).each do |t|
    content = t.content.to_s.gsub(pattern, replacement)
    t.content = content
    t.xml_space = Text.preserve_whitespace?(content) ? "preserve" : nil
  end
  self
end

#substitute_with_block(pattern) {|MatchData| ... } ⇒ self

Substitute with block

Parameters:

  • pattern (Regexp, String)

    Pattern to match

Yields:

  • (MatchData)

    Block receives match data object

Returns:

  • (self)

    For method chaining



112
113
114
115
116
117
118
119
120
121
# File 'lib/uniword/wordprocessingml/run.rb', line 112

def substitute_with_block(pattern, &block)
  Array(text).each do |t|
    content = t.content.to_s.gsub(pattern) do |_match_str|
      yield(Regexp.last_match)
    end
    t.content = content
    t.xml_space = Text.preserve_whitespace?(content) ? "preserve" : nil
  end
  self
end

#text_stringString

Combined string content of all text elements in the run

Returns:

  • (String)

    Concatenated content of every w:t element



89
90
91
# File 'lib/uniword/wordprocessingml/run.rb', line 89

def text_string
  Array(text).join
end