class Docx::Elements::Containers::Paragraph

Public Class Methods

new(node, document_properties = {}) click to toggle source

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

# 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
tag() click to toggle source
# File lib/docx/containers/paragraph.rb, line 11
def self.tag
  'p'
end

Public Instance Methods

aligned_center?() click to toggle source
# File lib/docx/containers/paragraph.rb, line 74
def aligned_center?
  alignment == 'center'
end
aligned_left?() click to toggle source
# File lib/docx/containers/paragraph.rb, line 66
def aligned_left?
  ['left', nil].include?(alignment)
end
aligned_right?() click to toggle source
# File lib/docx/containers/paragraph.rb, line 70
def aligned_right?
  alignment == 'right'
end
each_text_run() { |tr| ... } click to toggle source

Iterate over each text run within a paragraph

# File lib/docx/containers/paragraph.rb, line 62
def each_text_run
  text_runs.each { |tr| yield(tr) }
end
font_size() click to toggle source
# 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()
Alias for: to_s
text=(content) click to toggle source

Set text of paragraph

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

Array of text runs contained within paragraph

# File lib/docx/containers/paragraph.rb, line 57
def text_runs
  @node.xpath('w:r|w:hyperlink/w:r').map { |r_node| Containers::TextRun.new(r_node, @document_properties) }
end
to_html() click to toggle source

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

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

Return text of paragraph

# File lib/docx/containers/paragraph.rb, line 40
def to_s
  text_runs.map(&:text).join('')
end
Also aliased as: text

Private Instance Methods

alignment() click to toggle source

Returns the alignment if any, or nil if left

# File lib/docx/containers/paragraph.rb, line 88
def alignment
  alignment_tag = @node.xpath('.//w:jc').first
  alignment_tag ? alignment_tag.attributes['val'].value : nil
end