Class: Docx::Elements::Bookmark

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/docx/elements/bookmark.rb

Constant Summary collapse

TAG =
'bookmarkStart'

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

Constructor Details

#initialize(node) ⇒ Bookmark

Returns a new instance of Bookmark.



11
12
13
14
# File 'lib/docx/elements/bookmark.rb', line 11

def initialize(node)
  @node = node
  @name = @node['w:name']
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/docx/elements/bookmark.rb', line 7

def name
  @name
end

Instance Method Details

#get_run_afterObject



61
62
63
64
65
66
67
68
69
# File 'lib/docx/elements/bookmark.rb', line 61

def get_run_after
  if (r_node = @node.at_xpath("./following-sibling::w:r"))
    Containers::TextRun.new(r_node)
  else
    new_r = Containers::TextRun.create_with(self)
    new_r.insert_after(self)
    new_r
  end
end

#get_run_beforeObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/docx/elements/bookmark.rb', line 47

def get_run_before
  # at_xpath returns the first match found and preceding-sibling returns siblings in the 
  # order they appear in the document not the order as they appear when moving out from
  # the starting node
  if not (r_nodes = @node.xpath("./preceding-sibling::w:r")).empty?
    r_node = r_nodes.last
    Containers::TextRun.new(r_node)
  else
    new_r = Containers::TextRun.create_with(self)
    new_r.insert_before(self)
    new_r
  end
end

#insert_multiple_lines(text_array) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/docx/elements/bookmark.rb', line 26

def insert_multiple_lines(text_array)
  # Hold paragraphs to be inserted into, corresponding to the index of the strings in the text array
  paragraphs = []
  paragraph = self.parent_paragraph
  # Remove text from paragraph
  paragraph.blank!
  paragraphs << paragraph
  for i in 0...(text_array.size - 1)
    # Copy previous paragraph
    new_p = paragraphs[i].copy
    # Insert as sibling of previous paragraph
    new_p.insert_after(paragraphs[i])
    paragraphs << new_p
  end

  # Insert text into corresponding newly created paragraphs
  paragraphs.each_index do |index|
    paragraphs[index].text = text_array[index]
  end
end

#insert_text_after(text) ⇒ Object



21
22
23
24
# File 'lib/docx/elements/bookmark.rb', line 21

def insert_text_after(text)
  text_run = get_run_before
  text_run.text = "#{text_run.text}#{text}"
end

#insert_text_before(text) ⇒ Object



16
17
18
19
# File 'lib/docx/elements/bookmark.rb', line 16

def insert_text_before(text)
  text_run = get_run_after
  text_run.text = "#{text}#{text_run.text}"
end