class Docx::Elements::Containers::TextRun
Constants
- DEFAULT_FORMATTING
Attributes
formatting[R]
text[R]
Public Class Methods
new(node, document_properties = {})
click to toggle source
# File lib/docx/containers/text_run.rb, line 23 def initialize(node, document_properties = {}) @node = node @text_nodes = @node.xpath('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
tag()
click to toggle source
# File lib/docx/containers/text_run.rb, line 16 def self.tag 'r' end
Public Instance Methods
bolded?()
click to toggle source
# File lib/docx/containers/text_run.rb, line 77 def bolded? @formatting[:bold] end
font_size()
click to toggle source
# File lib/docx/containers/text_run.rb, line 85 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
italicized?()
click to toggle source
# File lib/docx/containers/text_run.rb, line 73 def italicized? @formatting[:italic] end
parse_formatting()
click to toggle source
# File lib/docx/containers/text_run.rb, line 48 def parse_formatting { italic: !@node.xpath('.//w:i').empty?, bold: !@node.xpath('.//w:b').empty?, underline: !@node.xpath('.//w:u').empty? } end
parse_text()
click to toggle source
Returns text contained within text run
# File lib/docx/containers/text_run.rb, line 44 def parse_text @text_nodes.map(&:content).join('') end
text=(content)
click to toggle source
Set text of text run
# File lib/docx/containers/text_run.rb, line 34 def text=(content) if @text_nodes.size == 1 @text_nodes.first.content = content elsif @text_nodes.empty? new_t = Elements::Text.create_within(self) new_t.content = content end end
to_html()
click to toggle source
Return text as a HTML fragment with formatting based on properties.
# File lib/docx/containers/text_run.rb, line 61 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? # No need to be granular with font size down to the span level if it doesn't vary. styles['font-size'] = "#{font_size}pt" if font_size != @font_size html = html_tag(:span, content: html, styles: styles) unless styles.empty? return html end
to_s()
click to toggle source
# File lib/docx/containers/text_run.rb, line 56 def to_s @text end
underlined?()
click to toggle source
# File lib/docx/containers/text_run.rb, line 81 def underlined? @formatting[:underline] end