Class: Docx::Elements::Containers::Paragraph

Inherits:
Object
  • Object
show all
Includes:
Container, Element
Defined in:
lib/docx/containers/paragraph.rb

Constant Summary collapse

TAG =
'p'

Constants included from Element

Element::DEFAULT_TAG

Instance Attribute Summary

Attributes included from Element

#node

Instance Method Summary collapse

Methods included from Element

#append_to, #copy, included, #insert_after, #insert_before, #parent, #parent_paragraph, #prepend_to

Methods included from Container

#blank!, #properties

Constructor Details

#initialize(node) ⇒ Paragraph

Child elements: pPr, r, fldSimple, hlink, subDoc msdn.microsoft.com/en-us/library/office/ee364458(v=office.11).aspx



15
16
17
18
# File 'lib/docx/containers/paragraph.rb', line 15

def initialize(node)
  @node = node
  @properties_tag = 'pPr'
end

Instance Method Details

#each_text_runObject



42
43
44
# File 'lib/docx/containers/paragraph.rb', line 42

def each_text_run
  text_runs.each { |tr| yield(tr) }
end

#text=(content) ⇒ Object

Handle direct text insertion into paragraph on some conditions



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/docx/containers/paragraph.rb', line 21

def text=(content)
  if text_runs.size == 1
    text_runs.first.text = content
  elsif text_runs.size == 0
    new_r = TextRun.create_within(self)
    new_r.text = content
  else
    text_runs.each {|r| r.node.remove }
    new_r = TextRun.create_within(self)
    new_r.text = content
  end
end

#text_runsObject



38
39
40
# File 'lib/docx/containers/paragraph.rb', line 38

def text_runs
  @node.xpath('w:r').map {|r_node| Containers::TextRun.new(r_node) }
end

#to_sObject Also known as: text



34
35
36
# File 'lib/docx/containers/paragraph.rb', line 34

def to_s
  text_runs.map(&:text).join('')
end