Class: Uniword::Bookmark

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/bookmark.rb

Overview

Represents a bookmark or anchor in a document.

Responsibility: Store bookmark metadata for creating internal links and anchors within a document. Bookmarks allow users to navigate to specific locations within a document.

A bookmark consists of:

  • A name that uniquely identifies it

  • An optional target element reference

Examples:

Create a bookmark

bookmark = Uniword::Bookmark.new(
  name: 'section1',
  target_element: paragraph
)

Access bookmark properties

bookmark.name           # => 'section1'
bookmark.target_element # => paragraph

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, target_element: nil, bookmark_id: nil) ⇒ Bookmark

Initialize a new Bookmark.

Examples:

Create a bookmark

bookmark = Bookmark.new(name: 'intro')

Create a bookmark with target

bookmark = Bookmark.new(name: 'intro', target_element: paragraph)

Create a bookmark with OOXML ID for round-trip

bookmark = Bookmark.new(name: 'intro', bookmark_id: '1')

Parameters:

  • name (String)

    The unique bookmark name

  • target_element (Element, nil) (defaults to: nil)

    The element this bookmark references

  • bookmark_id (String, nil) (defaults to: nil)

    The OOXML bookmark ID



47
48
49
50
51
# File 'lib/uniword/bookmark.rb', line 47

def initialize(name:, target_element: nil, bookmark_id: nil)
  @name = name
  @target_element = target_element
  @bookmark_id = bookmark_id
end

Instance Attribute Details

#bookmark_idString?

Returns The OOXML bookmark ID for round-trip preservation.

Returns:

  • (String, nil)

    The OOXML bookmark ID for round-trip preservation



31
32
33
# File 'lib/uniword/bookmark.rb', line 31

def bookmark_id
  @bookmark_id
end

#nameString

Returns The unique name/identifier for this bookmark.

Returns:

  • (String)

    The unique name/identifier for this bookmark



25
26
27
# File 'lib/uniword/bookmark.rb', line 25

def name
  @name
end

#target_elementElement?

Returns The target element this bookmark references.

Returns:

  • (Element, nil)

    The target element this bookmark references



28
29
30
# File 'lib/uniword/bookmark.rb', line 28

def target_element
  @target_element
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check equality with another bookmark.

Parameters:

  • other (Bookmark)

    The other bookmark to compare

Returns:

  • (Boolean)

    true if bookmarks have the same name



88
89
90
91
92
# File 'lib/uniword/bookmark.rb', line 88

def ==(other)
  return false unless other.is_a?(Bookmark)

  @name == other.name
end

#anchor_nameString

Generate HTML anchor name.

Sanitizes the bookmark name for use in HTML anchors.

Examples:

Get anchor name

bookmark = Bookmark.new(name: 'My Section!')
bookmark.anchor_name # => "my_section"

Returns:

  • (String)

    The sanitized anchor name



69
70
71
# File 'lib/uniword/bookmark.rb', line 69

def anchor_name
  @name.to_s.downcase.gsub(/[^a-z0-9_-]/, "_")
end

#hashInteger

Generate hash code for bookmark.

Returns:

  • (Integer)

    Hash code based on name



99
100
101
# File 'lib/uniword/bookmark.rb', line 99

def hash
  @name.hash
end

#target?Boolean

Check if bookmark has a target element.

Returns:

  • (Boolean)

    true if bookmark has a target element



56
57
58
# File 'lib/uniword/bookmark.rb', line 56

def target?
  !@target_element.nil?
end

#to_hHash

Convert to hash representation.

Returns:

  • (Hash)

    Hash representation of the bookmark



76
77
78
79
80
81
82
# File 'lib/uniword/bookmark.rb', line 76

def to_h
  {
    name: @name,
    target_element: @target_element,
    bookmark_id: @bookmark_id,
  }
end