Class: Docx::Elements::Containers::Paragraph
- Inherits:
-
Object
- Object
- Docx::Elements::Containers::Paragraph
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, #at_xpath, #copy, #html_tag, included, #insert_after, #insert_before, #parent, #parent_paragraph, #prepend_to, #xpath
Methods included from Container
#blank!, #properties, #remove!
Constructor Details
#initialize(node, document_properties = {}, doc = nil) ⇒ Paragraph
18
19
20
21
22
23
24
|
# File 'lib/docx/containers/paragraph.rb', line 18
def initialize(node, document_properties = {}, doc = nil)
@node = node
@properties_tag = 'pPr'
@document_properties = document_properties
@font_size = @document_properties[:font_size]
@document = doc
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
76
77
78
|
# File 'lib/docx/containers/paragraph.rb', line 76
def aligned_center?
alignment == 'center'
end
|
#aligned_left? ⇒ Boolean
68
69
70
|
# File 'lib/docx/containers/paragraph.rb', line 68
def aligned_left?
['left', nil].include?(alignment)
end
|
#aligned_right? ⇒ Boolean
72
73
74
|
# File 'lib/docx/containers/paragraph.rb', line 72
def aligned_right?
alignment == 'right'
end
|
#each_text_run ⇒ Object
Iterate over each text run within a paragraph
64
65
66
|
# File 'lib/docx/containers/paragraph.rb', line 64
def each_text_run
text_runs.each { |tr| yield(tr) }
end
|
#font_color ⇒ Object
88
89
90
91
|
# File 'lib/docx/containers/paragraph.rb', line 88
def font_color
color_tag = @node.xpath('w:r//w:rPr//w:color').first
color_tag ? color_tag.attributes['val'].value : nil
end
|
#font_size ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/docx/containers/paragraph.rb', line 80
def font_size
size_attribute = @node.at_xpath('w:pPr//w:sz//@w:val')
return @font_size unless size_attribute
size_attribute.value.to_i / 2
end
|
#style ⇒ Object
93
94
95
96
97
98
|
# File 'lib/docx/containers/paragraph.rb', line 93
def style
return nil unless @document
@document.style_name_of(style_id) ||
@document.default_paragraph_style
end
|
#style=(identifier) ⇒ Object
Also known as:
style_id=
104
105
106
107
108
|
# File 'lib/docx/containers/paragraph.rb', line 104
def style=(identifier)
id = @document.styles_configuration.style_of(identifier).id
style_property.set_attribute('w:val', id)
end
|
#style_id ⇒ Object
100
101
102
|
# File 'lib/docx/containers/paragraph.rb', line 100
def style_id
style_property.get_attribute('w:val')
end
|
#text=(content) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/docx/containers/paragraph.rb', line 27
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
59
60
61
|
# File 'lib/docx/containers/paragraph.rb', line 59
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.
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/docx/containers/paragraph.rb', line 46
def to_html
html = ''
text_runs.each do |text_run|
html << text_run.to_html
end
styles = { 'font-size' => "#{font_size}pt" }
styles['color'] = "##{font_color}" if font_color
styles['text-align'] = alignment if alignment
html_tag(:p, content: html, styles: styles)
end
|
#to_s ⇒ Object
Also known as:
text
41
42
43
|
# File 'lib/docx/containers/paragraph.rb', line 41
def to_s
text_runs.map(&:text).join('')
end
|