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

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

Constant Summary

Constants included from Element

Element::DEFAULT_TAG

Instance Attribute Summary

Attributes included from Element

#node

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Element

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

Methods included from Container

#blank!, #properties, #remove!

Constructor Details

#initialize(node, document_properties = {}) ⇒ Paragraph

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



18
19
20
21
22
23
# File 'lib/docx/containers/paragraph.rb', line 18

def initialize(node, document_properties = {})
  @node = node
  @properties_tag = 'pPr'
  @document_properties = document_properties
  @font_size = @document_properties[:font_size]
end

Class Method Details

.tagObject



11
12
13
# File 'lib/docx/containers/paragraph.rb', line 11

def self.tag
  'p'
end

Instance Method Details

#aligned_center?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/docx/containers/paragraph.rb', line 74

def aligned_center?
  alignment == 'center'
end

#aligned_left?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/docx/containers/paragraph.rb', line 66

def aligned_left?
  ['left', nil].include?(alignment)
end

#aligned_right?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/docx/containers/paragraph.rb', line 70

def aligned_right?
  alignment == 'right'
end

#each_text_runObject

Iterate over each text run within a paragraph



62
63
64
# File 'lib/docx/containers/paragraph.rb', line 62

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

#font_sizeObject



78
79
80
81
# File 'lib/docx/containers/paragraph.rb', line 78

def font_size
  size_tag = @node.xpath('w:pPr//w:sz').first
  size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size
end

#text=(content) ⇒ Object

Set text of paragraph



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/docx/containers/paragraph.rb', line 26

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

Array of text runs contained within paragraph



57
58
59
# File 'lib/docx/containers/paragraph.rb', line 57

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

#to_htmlObject

Return paragraph as a <p></p> HTML fragment with formatting based on properties.



45
46
47
48
49
50
51
52
53
# File 'lib/docx/containers/paragraph.rb', line 45

def to_html
  html = ''
  text_runs.each do |text_run|
    html << text_run.to_html
  end
  styles = { 'font-size' => "#{font_size}pt" }
  styles['text-align'] = alignment if alignment
  html_tag(:p, content: html, styles: styles)
end

#to_sObject Also known as: text

Return text of paragraph



40
41
42
# File 'lib/docx/containers/paragraph.rb', line 40

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