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
}
TAG =
'r'

Constants included from Element

Element::DEFAULT_TAG

Instance Attribute Summary collapse

Attributes included from Element

#node

Instance Method Summary collapse

Methods included from Element

#append_to, #copy, included, #insert_after, #insert_before, #parent, #parent_paragraph, #prepend_to

Methods included from Container

#blank!, #properties

Constructor Details

#initialize(node) ⇒ TextRun

Returns a new instance of TextRun.



21
22
23
24
25
26
27
# File 'lib/docx/containers/text_run.rb', line 21

def initialize(node)
  @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
end

Instance Attribute Details

#formattingObject (readonly)

Returns the value of attribute formatting.



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

def formatting
  @formatting
end

#textObject

Returns the value of attribute text.



18
19
20
# File 'lib/docx/containers/text_run.rb', line 18

def text
  @text
end

Instance Method Details

#bolded?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/docx/containers/text_run.rb', line 60

def bolded?
  @formatting[:bold]
end

#italicized?Boolean

Returns:

  • (Boolean)


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

def italicized?
  @formatting[:italic]
end

#parse_formattingObject



44
45
46
47
48
49
50
# File 'lib/docx/containers/text_run.rb', line 44

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



40
41
42
# File 'lib/docx/containers/text_run.rb', line 40

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

#to_sObject



52
53
54
# File 'lib/docx/containers/text_run.rb', line 52

def to_s
  @text
end

#underlined?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/docx/containers/text_run.rb', line 64

def underlined?
  @formatting[:underline]
end