class Docx::Elements::Containers::TextRun

Constants

DEFAULT_FORMATTING

Attributes

formatting[R]
text[R]
text_nodes[R]

Public Class Methods

new(node, document_properties = {}) click to toggle source
# 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
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 88
def bolded?
  @formatting[:bold]
end
font_size() click to toggle source
# 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() click to toggle source
# File lib/docx/containers/text_run.rb, line 100
def href
  @document_properties[:hyperlinks][hyperlink_id]
end
italicized?() click to toggle source
# File lib/docx/containers/text_run.rb, line 84
def italicized?
  @formatting[:italic]
end
parse_formatting() click to toggle source
# 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() click to toggle source

Returns text contained within text run

# File lib/docx/containers/text_run.rb, line 47
def parse_text
  @text_nodes.map(&:content).join('')
end
substitute(match, replacement) click to toggle source

Substitute text in text @text_nodes

# 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
text=(content) click to toggle source

Set text of text run

# File lib/docx/containers/text_run.rb, line 37
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 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?
  # 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?
  html = html_tag(:a, content: html, attributes: {href: href, target: "_blank"}) if hyperlink?
  return html
end
to_s() click to toggle source
# File lib/docx/containers/text_run.rb, line 66
def to_s
  @text
end
underlined?() click to toggle source
# File lib/docx/containers/text_run.rb, line 92
def underlined?
  @formatting[:underline]
end