class Docx::Elements::Bookmark

Attributes

name[RW]

Public Class Methods

new(node) click to toggle source
# File lib/docx/elements/bookmark.rb, line 13
def initialize(node)
  @node = node
  @name = @node['w:name']
end
tag() click to toggle source
# File lib/docx/elements/bookmark.rb, line 9
def self.tag
  'bookmarkStart'
end

Public Instance Methods

get_run_after() click to toggle source

Get text run immediately after bookmark node

# 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_before() click to toggle source

Get text run immediately prior to bookmark node

# 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) click to toggle source

insert multiple lines starting with paragraph containing bookmark node.

# 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) click to toggle source

Insert text after bookmarkStart node

# File lib/docx/elements/bookmark.rb, line 25
def insert_text_after(text)
  text_run = get_run_after
  text_run.text = "#{text}#{text_run.text}"
end
insert_text_before(text) click to toggle source

Insert text before bookmarkStart node

# File lib/docx/elements/bookmark.rb, line 19
def insert_text_before(text)
  text_run = get_run_before
  text_run.text = "#{text_run.text}#{text}"
end