Class: Uniword::Wordprocessingml::ParagraphProperties

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

Overview

Paragraph formatting properties

Represents w:pPr element containing paragraph-level formatting. Used in StyleSets and document paragraph elements.

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ ParagraphProperties

Initialize with defaults and handle convenience attributes



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 268

def initialize(attrs = {})
  # Save wrapper attribute values before super clears them
  keep_next_val = attrs.key?(:keep_next) ? attrs.delete(:keep_next) : nil
  keep_lines_val = attrs.key?(:keep_lines) ? attrs.delete(:keep_lines) : nil
  page_break_before_val = attrs.key?(:page_break_before) ? attrs.delete(:page_break_before) : nil
  widow_control_val = attrs.key?(:widow_control) ? attrs.delete(:widow_control) : nil
  style_val = attrs.key?(:style) ? attrs.delete(:style) : nil

  super

  # Set wrapper attributes after super (super clears them)
  if keep_next_val
    self.keep_next_wrapper = Properties::KeepNext.new(value: keep_next_val)
  end
  if keep_lines_val
    self.keep_lines_wrapper = Properties::KeepLines.new(value: keep_lines_val)
  end
  if page_break_before_val
    self.page_break_before_wrapper =
      Properties::PageBreakBefore.new(value: page_break_before_val)
  end
  unless widow_control_val.nil?
    self.widow_control_wrapper =
      Properties::WidowControl.new(value: widow_control_val)
  end
  self.style = style_val if style_val

  # Convert flat attributes to wrapper objects (Pattern 0: after super)
  convert_flat_attributes!
end

Instance Method Details

#convert_flat_attributes!Object

Convert flat convenience attributes to proper wrapper objects This handles cases like ParagraphProperties.new(spacing_before: 120) where the flat attribute is set but the wrapper object is not



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 302

def convert_flat_attributes!
  # Alignment - convert string to Alignment wrapper
  self.alignment = Properties::Alignment.new(value: @alignment) if @alignment.is_a?(String)

  # Spacing - create spacing object from flat spacing attributes
  if (@spacing_before || @spacing_after || @line_spacing || @line_rule) && !@spacing
    self.spacing = Properties::Spacing.new
    spacing.before = @spacing_before if @spacing_before
    spacing.after = @spacing_after if @spacing_after
    spacing.line = @line_spacing.to_i if @line_spacing
    spacing.line_rule = @line_rule if @line_rule
  end

  # Indentation - create indentation object from flat indent attributes
  if (@indent_left || @indent_right || @indent_first_line) && !@indentation
    self.indentation = Properties::Indentation.new
    indentation.left = @indent_left if @indent_left
    indentation.right = @indent_right if @indent_right
    indentation.first_line = @indent_first_line if @indent_first_line
  end

  # Run properties from flat attributes
  return unless @run_properties && !@run_properties.is_a?(RunProperties)

  rp = RunProperties.new
  rp.bold = @run_properties[:bold] if @run_properties[:bold]
  rp.italic = @run_properties[:italic] if @run_properties[:italic]
  rp.underline = @run_properties[:underline] if @run_properties[:underline]
  rp.color = @run_properties[:color] if @run_properties[:color]
  rp.font = @run_properties[:font] if @run_properties[:font]
  rp.size = @run_properties[:size] if @run_properties[:size]
  @run_properties = rp
end

#yaml_alignment_from(instance, value) ⇒ Object



133
134
135
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 133

def yaml_alignment_from(instance, value)
  instance.alignment = Properties::Alignment.new(value: value) if value
end

#yaml_alignment_to(instance, _doc) ⇒ Object



137
138
139
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 137

def yaml_alignment_to(instance, _doc)
  instance.alignment&.value
end

#yaml_contextual_spacing_from(instance, value) ⇒ Object



165
166
167
168
169
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 165

def yaml_contextual_spacing_from(instance, value)
  return if value.nil?

  instance.contextual_spacing = Properties::ContextualSpacing.new(value: value)
end

#yaml_contextual_spacing_to(instance, _doc) ⇒ Object



171
172
173
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 171

def yaml_contextual_spacing_to(instance, _doc)
  instance.contextual_spacing&.value
end

#yaml_keep_lines_from(instance, value) ⇒ Object



149
150
151
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 149

def yaml_keep_lines_from(instance, value)
  instance.keep_lines_wrapper = Properties::KeepLines.new(value: value) unless value.nil?
end

#yaml_keep_lines_to(instance, _doc) ⇒ Object



153
154
155
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 153

def yaml_keep_lines_to(instance, _doc)
  instance.keep_lines_wrapper&.value
end

#yaml_keep_next_from(instance, value) ⇒ Object



141
142
143
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 141

def yaml_keep_next_from(instance, value)
  instance.keep_next_wrapper = Properties::KeepNext.new(value: value) unless value.nil?
end

#yaml_keep_next_to(instance, _doc) ⇒ Object



145
146
147
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 145

def yaml_keep_next_to(instance, _doc)
  instance.keep_next_wrapper&.value
end

#yaml_outline_level_from(instance, value) ⇒ Object



157
158
159
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 157

def yaml_outline_level_from(instance, value)
  instance.outline_level = Properties::OutlineLevel.new(value: value.to_i) if value
end

#yaml_outline_level_to(instance, _doc) ⇒ Object



161
162
163
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 161

def yaml_outline_level_to(instance, _doc)
  instance.outline_level&.value
end

#yaml_page_break_before_from(instance, value) ⇒ Object



175
176
177
178
179
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 175

def yaml_page_break_before_from(instance, value)
  return if value.nil?

  instance.page_break_before_wrapper = Properties::PageBreakBefore.new(value: value)
end

#yaml_page_break_before_to(instance, _doc) ⇒ Object



181
182
183
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 181

def yaml_page_break_before_to(instance, _doc)
  instance.page_break_before_wrapper&.value
end

#yaml_style_from(instance, value) ⇒ Object

YAML transform methods (instance methods called by lutaml-model's with: transform mechanism)



121
122
123
124
125
126
127
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 121

def yaml_style_from(instance, value)
  if value
    instance.style = [
      Properties::StyleReference.new(value: value),
    ]
  end
end

#yaml_style_to(instance, _doc) ⇒ Object



129
130
131
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 129

def yaml_style_to(instance, _doc)
  Array(instance.style).first&.value
end

#yaml_widow_control_from(instance, value) ⇒ Object



185
186
187
188
189
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 185

def yaml_widow_control_from(instance, value)
  return if value.nil?

  instance.widow_control_wrapper = Properties::WidowControl.new(value: value)
end

#yaml_widow_control_to(instance, _doc) ⇒ Object



191
192
193
# File 'lib/uniword/wordprocessingml/paragraph_properties.rb', line 191

def yaml_widow_control_to(instance, _doc)
  instance.widow_control_wrapper&.value
end