Class: Docx::Elements::Bookmark

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

Constant Summary

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

Constructor Details

#initialize(node) ⇒ Bookmark

Returns a new instance of Bookmark.



13
14
15
16
# File 'lib/docx/elements/bookmark.rb', line 13

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

Class Method Details

.tagObject



9
10
11
# File 'lib/docx/elements/bookmark.rb', line 9

def self.tag
  'bookmarkStart'
end

Instance Method Details

#get_run_afterObject

Get text run immediately after bookmark node



68
69
70
71
72
73
74
75
76
# File 'lib/docx/elements/bookmark.rb', line 68

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

Get text run immediately prior to bookmark node



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/docx/elements/bookmark.rb', line 53

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

insert multiple lines starting with paragraph containing bookmark node.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/docx/elements/bookmark.rb', line 31

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

Insert text after bookmarkStart node



25
26
27
28
# File 'lib/docx/elements/bookmark.rb', line 25

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

#insert_text_before(text) ⇒ Object

Insert text before bookmarkStart node



19
20
21
22
# File 'lib/docx/elements/bookmark.rb', line 19

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