Class: OoxmlParser::DocxParagraph

Inherits:
OOXMLDocumentObject show all
Extended by:
Gem::Deprecate
Includes:
DocxParagraphHelper
Defined in:
lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb

Overview

Class for data of DocxParagraph

Instance Attribute Summary collapse

Attributes inherited from OOXMLDocumentObject

#parent

Instance Method Summary collapse

Methods included from DocxParagraphHelper

#background_color, #comment_extend_data

Methods inherited from OOXMLDocumentObject

#boolean_attribute_value, #parse_xml

Methods included from OoxmlObjectAttributeHelper

#attribute_enabled?, #option_enabled?

Methods included from OoxmlDocumentObjectHelper

#to_hash

Constructor Details

#initialize(parent: nil) ⇒ DocxParagraph

Returns a new instance of DocxParagraph.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 40

def initialize(parent: nil)
  @number = 0
  @align = :left
  @spacing = Spacing.new
  @ind = Indents.new
  @character_style_array = []
  @page_break = false
  @borders = Borders.new
  @keep_lines = false
  @contextual_spacing = false
  @page_numbering = false
  @keep_next = false
  @orphan_control = true
  super
end

Instance Attribute Details

#alignObject

Returns the value of attribute align.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def align
  @align
end

#bordersObject

Returns the value of attribute borders.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def borders
  @borders
end

#character_style_arrayObject Also known as: elements, runs

Returns the value of attribute character_style_array.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def character_style_array
  @character_style_array
end

#contextual_spacingObject

Returns the value of attribute contextual_spacing.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def contextual_spacing
  @contextual_spacing
end

#field_simpleFieldSimple (readonly)

Returns field simple.

Returns:



30
31
32
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 30

def field_simple
  @field_simple
end

Returns hyperlink in paragraph.

Returns:



24
25
26
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 24

def hyperlink
  @hyperlink
end

#insertedInserted

Returns data inserted by review.

Returns:

  • (Inserted)

    data inserted by review



28
29
30
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 28

def inserted
  @inserted
end

#keep_linesObject

Returns the value of attribute keep_lines.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def keep_lines
  @keep_lines
end

#keep_nextObject

Returns the value of attribute keep_next.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def keep_next
  @keep_next
end

#math_paragraphMathParagraph (readonly)

Returns math paragraph.

Returns:



34
35
36
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 34

def math_paragraph
  @math_paragraph
end

#numberObject

Returns the value of attribute number.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def number
  @number
end

#numberingObject

Returns the value of attribute numbering.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def numbering
  @numbering
end

#orphan_controlObject

Returns the value of attribute orphan_control.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def orphan_control
  @orphan_control
end

#page_breakObject

Returns the value of attribute page_break.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def page_break
  @page_break
end

#page_numberingObject

Returns the value of attribute page_numbering.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def page_numbering
  @page_numbering
end

#paragraph_idInteger

Returns id of paragraph (for comment).

Returns:

  • (Integer)

    id of paragraph (for comment)



32
33
34
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 32

def paragraph_id
  @paragraph_id
end

#paragraph_propertiesParagraphProperties

Returns Properties of current paragraph.

Returns:



26
27
28
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 26

def paragraph_properties
  @paragraph_properties
end

#section_breakObject

Returns the value of attribute section_break.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def section_break
  @section_break
end

#sector_propertiesObject

Returns the value of attribute sector_properties.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def sector_properties
  @sector_properties
end

#spacingObject



226
227
228
229
230
231
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 226

def spacing
  style_spacing = root_object.styles&.default_style(:paragraph)&.paragraph_properties&.spacing
  return Spacing.new.fetch_from_valued_spacing(style_spacing) if style_spacing

  @spacing
end

#styleObject

Returns the value of attribute style.



19
20
21
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 19

def style
  @style
end

#text_idInteger

Returns id of text (for comment).

Returns:

  • (Integer)

    id of text (for comment)



38
39
40
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 38

def text_id
  @text_id
end

Instance Method Details

#==(other) ⇒ True, False

Compare this object to other

Parameters:

  • other (Object)

    any other object

Returns:

  • (True, False)

    result of comparision



89
90
91
92
93
94
95
96
97
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 89

def ==(other)
  ignored_attributes = %i[@number @parent]
  all_instance_variables = instance_variables
  significan_attribues = all_instance_variables - ignored_attributes
  significan_attribues.each do |current_attributes|
    return false unless instance_variable_get(current_attributes) == other.instance_variable_get(current_attributes)
  end
  true
end

#fill_style_data(character_style) ⇒ void

This method returns an undefined value.

Fill data from styles

Parameters:



246
247
248
249
250
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 246

def fill_style_data(character_style)
  @style = root_object.document_style_by_id(@paragraph_style_ref.value)
  parse_paragraph_style(@style.paragraph_properties_node, character_style) if @style.paragraph_properties_node
  character_style.parse_properties(@style.run_properties_node) if @style.run_properties_node
end

#indIndents

Returns value of indents.

Returns:



234
235
236
237
238
239
240
241
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 234

def ind
  return @ind if @ind != Indents.new

  indents_from_styles = root_object.styles&.default_style(:paragraph)&.paragraph_properties&.indent
  return indents_from_styles if indents_from_styles

  Indents.new
end

#initialize_copy(source) ⇒ void

This method returns an undefined value.

Constructor for copy of object

Parameters:



61
62
63
64
65
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 61

def initialize_copy(source)
  super
  @character_style_array = source.character_style_array.clone
  @spacing = source.instance_variable_get(:@spacing).clone
end

#nonempty_runsArray<OOXMLDocumentObject>

Returns array of child objects that contains data.

Returns:



68
69
70
71
72
73
74
75
76
77
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 68

def nonempty_runs
  @character_style_array.select do |cur_run|
    case cur_run
    when DocxParagraphRun, ParagraphRun
      !cur_run.empty?
    when DocxFormula, StructuredDocumentTag, BookmarkStart, BookmarkEnd, CommentRangeStart, CommentRangeEnd
      true
    end
  end
end

#parse(node, par_number = 0, default_character = DocxParagraphRun.new, parent: nil) ⇒ DocxParagraph

Parse object

Parameters:

  • node (Nokogiri::XML:Node)

    node with DocxParagraph

  • par_number (Integer) (defaults to: 0)

    number of paragraph

  • default_character (DocxParagraphRun) (defaults to: DocxParagraphRun.new)

    style for paragraph

  • parent (OOXMLDocumentObject) (defaults to: nil)

    parent of run

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 105

def parse(node, par_number = 0, default_character = DocxParagraphRun.new, parent: nil)
  @parent ||= parent
  default_character_style = default_character.dup
  character_styles_array = []
  custom_character_style = default_character_style.dup
  custom_character_style.parent = self
  char_number = 0
  node.attributes.each do |key, value|
    case key
    when 'paraId'
      @paragraph_id = value.value.to_i
    when 'textId'
      @text_id = value.value.to_i
    end
  end
  node.xpath('*').each do |node_child|
    case node_child.name
    when 'bookmarkStart'
      character_styles_array << BookmarkStart.new(parent: self).parse(node_child)
    when 'bookmarkEnd'
      character_styles_array << BookmarkEnd.new(parent: self).parse(node_child)
    when 'pPr'
      @paragraph_properties = ParagraphProperties.new(parent: self).parse(node_child)
      parse_paragraph_style(node_child, custom_character_style)
    when 'commentRangeStart'
      character_styles_array << CommentRangeStart.new(parent: self).parse(node_child)
    when 'commentRangeEnd'
      character_styles_array << CommentRangeEnd.new(parent: self).parse(node_child)
    when 'fldSimple'
      @field_simple = FieldSimple.new(parent: self).parse(node_child)
      @page_numbering = true if field_simple.page_numbering?
      character_styles_array += field_simple.runs
    when 'r'
      run = custom_character_style.dup
      node_child.xpath('w:instrText').each do |insrt_text|
        @page_numbering = true if insrt_text.text.include?('PAGE')
      end
      run.parse(node_child, char_number, parent: self)
      character_styles_array << run
      char_number += 1
    when 'hyperlink'
      @hyperlink = Hyperlink.new(parent: self).parse(node_child)
      node_child.xpath('w:r').each do |r_tag|
        hyperlink_run = default_character_style.dup
        hyperlink_run.parent = self
        if @hyperlink.id
          @hyperlink.parent = hyperlink_run
          hyperlink_run.link = @hyperlink
        elsif @hyperlink.anchor
          hyperlink_run.link = @hyperlink.anchor
        end
        hyperlink_run.parse(r_tag, char_number, parent: self)
        character_styles_array << hyperlink_run
        char_number += 1
      end
      node_child.xpath('w:fldSimple').each do |simple_field|
        hyperlink_field_simple = FieldSimple.new(parent: self).parse(simple_field)
        character_styles_array += hyperlink_field_simple.runs
      end
    when 'oMathPara'
      @math_paragraph = MathParagraph.new(parent: self).parse(node_child)
      character_styles_array << math_paragraph.math
    when 'ins'
      @inserted = Inserted.new(parent: self).parse(node_child)
    when 'sdt'
      character_styles_array << StructuredDocumentTag.new(parent: self).parse(node_child)
    end
  end
  @number = par_number
  character_styles_array.last.text = character_styles_array.last.text.rstrip if character_styles_array.last.instance_of?(DocxParagraphRun)
  @character_style_array = character_styles_array
  @parent = parent
  self
end

#parse_paragraph_style(node, default_char_style = DocxParagraphRun.new(parent: self)) ⇒ DocxParagraph

Parse style

Parameters:

  • node (Nokogiri::XML:Node)

    node with style

  • default_char_style (DocxParagraphRun) (defaults to: DocxParagraphRun.new(parent: self))

    style for paragraph

Returns:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 184

def parse_paragraph_style(node, default_char_style = DocxParagraphRun.new(parent: self))
  node.xpath('*').each do |node_child|
    case node_child.name
    when 'pageBreakBefore'
      @page_break = true if node_child.attribute('val').nil? || node_child.attribute('val').value != 'false'
    when 'pBdr'
      @borders = ParagraphBorders.new(parent: self).parse(node_child)
    when 'keepLines'
      if node_child.attribute('val').nil?
        @keep_lines = true
      else
        @keep_lines = true unless node_child.attribute('val').value == 'false'
      end
    when 'widowControl'
      @orphan_control = option_enabled?(node_child)
    when 'keepNext'
      @keep_next = true
    when 'contextualSpacing'
      @contextual_spacing = true
    when 'pStyle'
      @paragraph_style_ref = ParagraphStyleRef.new(parent: self).parse(node_child)
      fill_style_data(default_char_style)
    when 'ind'
      @ind = root_object.default_paragraph_style.instance_variable_get(:@ind).dup.parse(node_child)
    when 'numPr'
      @numbering = NumberingProperties.new(parent: self).parse(node_child)
    when 'jc'
      @justification_object = ValuedChild.new(:string, parent: self).parse(node_child)
      @align = @justification_object.value.to_sym unless @justification_object.value.nil?
      @align = :justify if @justification_object.value == 'both'
    when 'spacing'
      @valued_spacing = ParagraphSpacing.new(parent: self).parse(node_child)
      @spacing = @spacing.fetch_from_valued_spacing(@valued_spacing)
    when 'sectPr'
      @sector_properties = PageProperties.new(parent: self).parse(node_child, self, default_char_style)
      @section_break ||= @sector_properties.section_break
    end
  end
  @parent = parent
  self
end

#sdtOoxmlParser::StructuredDocumentTag

Return first sdt element

Returns:



256
257
258
259
260
261
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 256

def sdt
  @character_style_array.each do |cur_element|
    return cur_element if cur_element.is_a?(StructuredDocumentTag)
  end
  nil
end

#with_data?True, false

Returns if structure contain any user data.

Returns:

  • (True, false)

    if structure contain any user data



82
83
84
# File 'lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb', line 82

def with_data?
  !nonempty_runs.empty? || paragraph_properties&.section_properties
end