class OoxmlParser::DocxParagraph

Class for data of DocxParagraph

Attributes

align[RW]
borders[RW]
character_style_array[RW]
contextual_spacing[RW]
elements[RW]
field_simple[R]

@return [FieldSimple] field simple

inserted[RW]

@return [Inserted] data inserted by review

keep_lines[RW]
keep_next[RW]
math_paragraph[R]

@return [MathParagraph] math paragraph

number[RW]
numbering[RW]
orphan_control[RW]
page_break[RW]
page_numbering[RW]
paragraph_id[RW]

@return [Integer] id of paragraph (for comment)

paragraph_properties[RW]

@return [ParagraphProperties] Properties of current paragraph

runs[RW]
section_break[RW]
sector_properties[RW]
spacing[W]

Set spacing value

style[RW]
text_id[RW]

@return [Integer] id of text (for comment)

Public Class Methods

new(parent: nil) click to toggle source
Calls superclass method OoxmlParser::OOXMLDocumentObject::new
# 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

Public Instance Methods

==(other) click to toggle source

Compare this object to other @param other [Object] any other object @return [True, False] result of comparision

# 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) click to toggle source

Fill data from styles @param character_style [DocxParagraphRun] style to parse @return [void]

# 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
ind() click to toggle source

@return [Indents] value of indents

# 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) click to toggle source

Constructor for copy of object @param source [DocxParagraph] original object @return [void]

Calls superclass method
# 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_runs() click to toggle source

@return [Array<OOXMLDocumentObject>] array of child objects that contains data

# 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) click to toggle source

Parse object @param node [Nokogiri::XML:Node] node with DocxParagraph @param par_number [Integer] number of paragraph @param default_character [DocxParagraphRun] style for paragraph @param parent [OOXMLDocumentObject] parent of run @return [DocxParagraph] result of parse

# 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)) click to toggle source

Parse style @param node [Nokogiri::XML:Node] node with style @param default_char_style [DocxParagraphRun] style for paragraph @return [DocxParagraph] result of parse

# 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
sdt() click to toggle source

@return [OoxmlParser::StructuredDocumentTag] Return first sdt element

# 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
spacing() click to toggle source
# 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
with_data?() click to toggle source

@return [True, false] if structure contain any user data

# File lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb, line 82
def with_data?
  !nonempty_runs.empty? || paragraph_properties&.section_properties
end