@@ -12,14 +12,14 @@ class TextRun
1212 bold : false ,
1313 underline : false
1414 }
15-
15+
1616 def self . tag
1717 'r'
1818 end
1919
2020 attr_reader :text
2121 attr_reader :formatting
22-
22+
2323 def initialize ( node , document_properties = { } )
2424 @node = node
2525 @text_nodes = @node . xpath ( 'w:t' ) . map { |t_node | Elements ::Text . new ( t_node ) }
@@ -40,6 +40,7 @@ def text=(content)
4040 new_t = Elements ::Text . create_within ( self )
4141 new_t . content = content
4242 end
43+ reset_text
4344 end
4445
4546 # Returns text contained within text run
@@ -52,6 +53,7 @@ def substitute(match, replacement)
5253 @text_nodes . each do |text_node |
5354 text_node . content = text_node . content . gsub ( match , replacement )
5455 end
56+ reset_text
5557 end
5658
5759 def parse_formatting
@@ -74,7 +76,7 @@ def to_html
7476 styles = { }
7577 styles [ 'text-decoration' ] = 'underline' if underlined?
7678 # No need to be granular with font size down to the span level if it doesn't vary.
77- styles [ 'font-size' ] = "#{ font_size } pt" if font_size != @font_size
79+ styles [ 'font-size' ] = "#{ font_size } pt" if font_size != @font_size
7880 html = html_tag ( :span , content : html , styles : styles ) unless styles . empty?
7981 html = html_tag ( :a , content : html , attributes : { href : href , target : "_blank" } ) if hyperlink?
8082 return html
@@ -83,11 +85,11 @@ def to_html
8385 def italicized?
8486 @formatting [ :italic ]
8587 end
86-
88+
8789 def bolded?
8890 @formatting [ :bold ]
8991 end
90-
92+
9193 def underlined?
9294 @formatting [ :underline ]
9395 end
@@ -102,12 +104,18 @@ def href
102104
103105 def hyperlink_id
104106 @node . attributes [ 'id' ] . value
105- end
107+ end
106108
107109 def font_size
108110 size_tag = @node . xpath ( 'w:rPr//w:sz' ) . first
109111 size_tag ? size_tag . attributes [ 'val' ] . value . to_i / 2 : @font_size
110112 end
113+
114+ private
115+
116+ def reset_text
117+ @text = parse_text
118+ end
111119 end
112120 end
113121 end
0 commit comments