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)


58
59
60
# File 'lib/docx/containers/text_run.rb', line 58

def bolded?
  @formatting[:bold]
end

#italicized?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/docx/containers/text_run.rb', line 54

def italicized?
  @formatting[:italic]
end

#parse_formattingObject



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

def parse_formatting
  {
    italic:    !@node.xpath('.//w:i').empty?,
    bold:      !@node.xpath('.//w:b').empty?,
    underline: !@node.xpath('.//w:u').empty?
  }
end

#parse_textObject



38
39
40
# File 'lib/docx/containers/text_run.rb', line 38

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

#to_sObject



50
51
52
# File 'lib/docx/containers/text_run.rb', line 50

def to_s
  @text
end

#underlined?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/docx/containers/text_run.rb', line 62

def underlined?
  @formatting[:underline]
end