Class: Docx::Elements::Containers::TextRun

Inherits:
Object
  • Object
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.



23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#formattingObject (readonly)

Returns the value of attribute formatting.



21
22
23
# File 'lib/docx/containers/text_run.rb', line 21

def formatting
  @formatting
end

#textObject

Returns the value of attribute text.



20
21
22
# File 'lib/docx/containers/text_run.rb', line 20

def text
  @text
end

Class Method Details

.tagObject



16
17
18
# File 'lib/docx/containers/text_run.rb', line 16

def self.tag
  'r'
end

Instance Method Details

#bolded?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/docx/containers/text_run.rb', line 77

def bolded?
  @formatting[:bold]
end

#font_sizeObject



85
86
87
88
# 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?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/docx/containers/text_run.rb', line 73

def italicized?
  @formatting[:italic]
end

#parse_formattingObject



48
49
50
51
52
53
54
# 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_textObject

Returns text contained within text run



44
45
46
# File 'lib/docx/containers/text_run.rb', line 44

def parse_text
  @text_nodes.map(&:content).join('')
end

#to_htmlObject

Return text as a HTML fragment with formatting based on properties.



61
62
63
64
65
66
67
68
69
70
71
# 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_sObject



56
57
58
# File 'lib/docx/containers/text_run.rb', line 56

def to_s
  @text
end

#underlined?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/docx/containers/text_run.rb', line 81

def underlined?
  @formatting[:underline]
end