Class: Docx::Elements::Containers::TextRun
- Inherits:
-
Object
- Object
- Docx::Elements::Containers::TextRun
show all
- Includes:
- Container, Element
- Defined in:
- lib/docx/containers/text_run.rb
Constant Summary
collapse
- DEFAULT_FORMATTING =
{
italic: false,
bold: false,
underline: false
}
Constants included
from Element
Element::DEFAULT_TAG
Instance Attribute Summary collapse
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 = {}) ⇒ TextRun
Returns a new instance of TextRun.
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/docx/containers/text_run.rb', line 24
def initialize(node, document_properties = {})
@node = node
@text_nodes = @node.xpath('w:t').map {|t_node| Elements::Text.new(t_node) }
@text_nodes = @node.xpath('w:t|w:r/w:t').map {|t_node| Elements::Text.new(t_node) }
@properties_tag = 'rPr'
@text = parse_text || ''
@formatting = parse_formatting || DEFAULT_FORMATTING
@document_properties = document_properties
@font_size = @document_properties[:font_size]
end
|
Instance Attribute Details
Returns the value of attribute formatting.
21
22
23
|
# File 'lib/docx/containers/text_run.rb', line 21
def formatting
@formatting
end
|
#text ⇒ Object
Returns the value of attribute text.
20
21
22
|
# File 'lib/docx/containers/text_run.rb', line 20
def text
@text
end
|
#text_nodes ⇒ Object
Returns the value of attribute text_nodes.
22
23
24
|
# File 'lib/docx/containers/text_run.rb', line 22
def text_nodes
@text_nodes
end
|
Class Method Details
.tag ⇒ Object
16
17
18
|
# File 'lib/docx/containers/text_run.rb', line 16
def self.tag
'r'
end
|
Instance Method Details
#bolded? ⇒ Boolean
88
89
90
|
# File 'lib/docx/containers/text_run.rb', line 88
def bolded?
@formatting[:bold]
end
|
#font_size ⇒ Object
108
109
110
111
|
# File 'lib/docx/containers/text_run.rb', line 108
def font_size
size_tag = @node.xpath('w:rPr//w:sz').first
size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size
end
|
#href ⇒ Object
100
101
102
|
# File 'lib/docx/containers/text_run.rb', line 100
def href
@document_properties[:hyperlinks][hyperlink_id]
end
|
#hyperlink? ⇒ Boolean
96
97
98
|
# File 'lib/docx/containers/text_run.rb', line 96
def hyperlink?
@node.name == 'hyperlink'
end
|
#hyperlink_id ⇒ Object
104
105
106
|
# File 'lib/docx/containers/text_run.rb', line 104
def hyperlink_id
@node.attributes['id'].value
end
|
#italicized? ⇒ Boolean
84
85
86
|
# File 'lib/docx/containers/text_run.rb', line 84
def italicized?
@formatting[:italic]
end
|
58
59
60
61
62
63
64
|
# File 'lib/docx/containers/text_run.rb', line 58
def parse_formatting
{
italic: !@node.xpath('.//w:i').empty?,
bold: !@node.xpath('.//w:b').empty?,
underline: !@node.xpath('.//w:u').empty?
}
end
|
#parse_text ⇒ Object
Returns text contained within text run
47
48
49
|
# File 'lib/docx/containers/text_run.rb', line 47
def parse_text
@text_nodes.map(&:content).join('')
end
|
#substitute(match, replacement) ⇒ Object
Substitute text in text @text_nodes
52
53
54
55
56
|
# File 'lib/docx/containers/text_run.rb', line 52
def substitute(match, replacement)
@text_nodes.each do |text_node|
text_node.content = text_node.content.gsub(match, replacement)
end
end
|
#to_html ⇒ Object
Return text as a HTML fragment with formatting based on properties.
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/docx/containers/text_run.rb', line 71
def to_html
html = @text
html = html_tag(:em, content: html) if italicized?
html = html_tag(:strong, content: html) if bolded?
styles = {}
styles['text-decoration'] = 'underline' if underlined?
styles['font-size'] = "#{font_size}pt" if font_size != @font_size
html = html_tag(:span, content: html, styles: styles) unless styles.empty?
html = html_tag(:a, content: html, attributes: {href: href, target: "_blank"}) if hyperlink?
return html
end
|
#to_s ⇒ Object
66
67
68
|
# File 'lib/docx/containers/text_run.rb', line 66
def to_s
@text
end
|
#underlined? ⇒ Boolean
92
93
94
|
# File 'lib/docx/containers/text_run.rb', line 92
def underlined?
@formatting[:underline]
end
|