Class: Uniword::Builder::ParagraphBuilder

Inherits:
BaseBuilder
  • Object
show all
Includes:
HasBorders, HasShading
Defined in:
lib/uniword/builder/paragraph_builder.rb

Overview

Builds and configures Paragraph objects.

Uses << operator to append child elements with smart type routing.

Examples:

Create a simple paragraph

para = ParagraphBuilder.new
para << 'Hello World'
para.build

Styled paragraph with mixed content

para = ParagraphBuilder.new
para.style = 'Heading1'
para << Builder.text('Title', bold: true, size: 24)
para << Builder.hyperlink('https://example.com', 'link')
para << Builder.tab_stop(position: 7200)
para.build

Instance Attribute Summary

Attributes inherited from BaseBuilder

#model

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasShading

#shading

Methods included from HasBorders

#borders

Methods inherited from BaseBuilder

#build, from_model, #initialize

Constructor Details

This class inherits a constructor from Uniword::Builder::BaseBuilder

Class Method Details

.default_model_classObject



25
26
27
# File 'lib/uniword/builder/paragraph_builder.rb', line 25

def self.default_model_class
  Wordprocessingml::Paragraph
end

Instance Method Details

#<<(element) ⇒ self

Append a child element. Routes by type:

  • String -> creates a Run

  • Run -> appends to runs

  • Hyperlink -> appends to hyperlinks

  • TabStop -> appends to properties.tabs

  • BookmarkStart/End -> appends to bookmarks

  • StructuredDocumentTag -> appends to sdts

Parameters:

Returns:

  • (self)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/uniword/builder/paragraph_builder.rb', line 39

def <<(element)
  case element
  when String
    @model.runs << Wordprocessingml::Run.new(text: element)
  when Wordprocessingml::Run
    @model.runs << element
  when Wordprocessingml::Hyperlink
    @model.hyperlinks << element
  when Properties::TabStop
    ensure_properties
    @model.properties.tabs ||= Properties::Tabs.new
    @model.properties.tabs << element
  when Wordprocessingml::BookmarkStart
    @model.bookmark_starts << element
  when Wordprocessingml::BookmarkEnd
    @model.bookmark_ends << element
  when Wordprocessingml::StructuredDocumentTag
    @model.sdts << element
  when RunBuilder
    @model.runs << element.build
  else
    raise ArgumentError, "Cannot add #{element.class} to paragraph"
  end
  self
end

#align=(value) ⇒ Object



70
71
72
73
# File 'lib/uniword/builder/paragraph_builder.rb', line 70

def align=(value)
  ensure_properties.alignment = Properties::Alignment.new(value: value.to_s)
  self
end

#contextual_spacing=(value) ⇒ Object



137
138
139
140
# File 'lib/uniword/builder/paragraph_builder.rb', line 137

def contextual_spacing=(value)
  ensure_properties.contextual_spacing = value
  self
end

#indent(left: nil, right: nil, first_line: nil, hanging: nil) ⇒ self

Set paragraph indentation

Parameters:

  • left (Integer, nil) (defaults to: nil)

    Left indent in twips

  • right (Integer, nil) (defaults to: nil)

    Right indent in twips

  • first_line (Integer, nil) (defaults to: nil)

    First line indent in twips

  • hanging (Integer, nil) (defaults to: nil)

    Hanging indent in twips

Returns:

  • (self)


99
100
101
102
103
104
105
106
107
# File 'lib/uniword/builder/paragraph_builder.rb', line 99

def indent(left: nil, right: nil, first_line: nil, hanging: nil)
  ensure_properties.indentation ||= Properties::Indentation.new
  ind = @model.properties.indentation
  ind.left = left if left
  ind.right = right if right
  ind.first_line = first_line if first_line
  ind.hanging = hanging if hanging
  self
end

#keep_next(value = true) ⇒ Object



125
126
127
128
129
# File 'lib/uniword/builder/paragraph_builder.rb', line 125

def keep_next(value = true)
  ensure_properties.keep_next_wrapper =
    value ? Properties::KeepNext.new(value: true) : nil
  self
end

#numbering(num_id, level = 0) ⇒ self

Set numbering

Parameters:

  • num_id (Integer)

    Numbering definition ID

  • level (Integer) (defaults to: 0)

    Numbering level (0-based, default 0)

Returns:

  • (self)


114
115
116
117
118
119
120
121
122
123
# File 'lib/uniword/builder/paragraph_builder.rb', line 114

def numbering(num_id, level = 0)
  props = ensure_properties
  props.num_id = num_id
  props.ilvl = level
  props.numbering_properties = Properties::NumberingProperties.new(
    num_id: Properties::NumberingId.new(value: num_id),
    ilvl: Properties::NumberingLevel.new(value: level),
  )
  self
end

#outline_level=(value) ⇒ Object



142
143
144
145
# File 'lib/uniword/builder/paragraph_builder.rb', line 142

def outline_level=(value)
  ensure_properties.outline_level = value
  self
end

#page_break_before(value = true) ⇒ Object



131
132
133
134
135
# File 'lib/uniword/builder/paragraph_builder.rb', line 131

def page_break_before(value = true)
  ensure_properties.page_break_before_wrapper =
    value ? Properties::PageBreakBefore.new(value: true) : nil
  self
end

#spacing(before: nil, after: nil, line: nil, rule: nil) ⇒ self

Set paragraph spacing

Parameters:

  • before (Integer, nil) (defaults to: nil)

    Spacing before in twips

  • after (Integer, nil) (defaults to: nil)

    Spacing after in twips

  • line (Integer, nil) (defaults to: nil)

    Line spacing in twips

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

    Line rule (‘auto’, ‘exact’, ‘atLeast’)

Returns:

  • (self)


82
83
84
85
86
87
88
89
90
# File 'lib/uniword/builder/paragraph_builder.rb', line 82

def spacing(before: nil, after: nil, line: nil, rule: nil)
  ensure_properties.spacing ||= Properties::Spacing.new
  props = @model.properties.spacing
  props.before = before if before
  props.after = after if after
  props.line = line if line
  props.line_rule = rule if rule
  self
end

#style=(name) ⇒ Object



65
66
67
68
# File 'lib/uniword/builder/paragraph_builder.rb', line 65

def style=(name)
  ensure_properties.style = Properties::StyleReference.new(value: name)
  self
end