Class: Docx::Elements::Containers::Paragraph
- Inherits:
-
Object
- Object
- Docx::Elements::Containers::Paragraph
- Defined in:
- lib/docx/containers/paragraph.rb
Constant Summary
Constants included from Element
Instance Attribute Summary
Attributes included from Element
Class Method Summary collapse
Instance Method Summary collapse
- #aligned_center? ⇒ Boolean
- #aligned_left? ⇒ Boolean
- #aligned_right? ⇒ Boolean
-
#each_text_run ⇒ Object
Iterate over each text run within a paragraph.
- #font_size ⇒ Object
-
#initialize(node, document_properties = {}) ⇒ Paragraph
constructor
Child elements: pPr, r, fldSimple, hlink, subDoc msdn.microsoft.com/en-us/library/office/ee364458(v=office.11).aspx.
-
#text=(content) ⇒ Object
Set text of paragraph.
-
#text_runs ⇒ Object
Array of text runs contained within paragraph.
-
#to_html ⇒ Object
Return paragraph as a <p></p> HTML fragment with formatting based on properties.
-
#to_s ⇒ Object
(also: #text)
Return text of paragraph.
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
.tag ⇒ Object
11 12 13 |
# File 'lib/docx/containers/paragraph.rb', line 11 def self.tag 'p' end |
Instance Method Details
#aligned_center? ⇒ Boolean
74 75 76 |
# File 'lib/docx/containers/paragraph.rb', line 74 def aligned_center? alignment == 'center' end |
#aligned_left? ⇒ Boolean
66 67 68 |
# File 'lib/docx/containers/paragraph.rb', line 66 def aligned_left? ['left', nil].include?(alignment) end |
#aligned_right? ⇒ Boolean
70 71 72 |
# File 'lib/docx/containers/paragraph.rb', line 70 def aligned_right? alignment == 'right' end |
#each_text_run ⇒ Object
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_size ⇒ Object
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_runs ⇒ Object
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_html ⇒ Object
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_s ⇒ Object 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 |