Class: Glyph::Bookmark

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

Overview

This class is used to model bookmarks within a Glyph document. It contains methods to store bookmark data and resolve link paths automatically.

Since:

  • 0.4.0

Direct Known Subclasses

Header

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Bookmark

Initializes a bookmark object from a hash containing bookmark data.

Parameters:

  • hash (Hash)

    the bookmark hash

Options Hash (hash):

  • :id (String)

    the bookmark ID

  • :file (String)

    the file containing the bookmark

  • :title (String)

    the title of the bookmark

  • :definition (String)

    the file where the bookmark was defined

Raises:

  • (RuntimeError)

    if the bookmark ID is not specified

  • (RuntimeError)

    if the bookmark ID is invalid (it must contain only letters, numbers, - or _)

Since:

  • 0.4.0


20
21
22
23
24
25
26
27
# File 'lib/glyph/bookmark.rb', line 20

def initialize(hash)
	@id = hash[:id].to_sym rescue nil
	@file = hash[:file]
	@title = hash[:title]
	@definition = hash[:definition]
	raise RuntimeError, "Bookmark ID not specified" unless @id
	raise RuntimeError, "Invalid bookmark ID: #{@id}" unless check_id
end

Instance Attribute Details

#definitionObject

Since:

  • 0.4.0


10
11
12
# File 'lib/glyph/bookmark.rb', line 10

def definition
  @definition
end

#fileObject

Since:

  • 0.4.0


10
11
12
# File 'lib/glyph/bookmark.rb', line 10

def file
  @file
end

#titleObject

Since:

  • 0.4.0


10
11
12
# File 'lib/glyph/bookmark.rb', line 10

def title
  @title
end

Instance Method Details

#==(b) ⇒ Object

Returns true if the two bookmarks have the same ID and file

Parameters:

Raises:

  • (RuntimeError)

    if the parameter supplied is not a bookmark

Since:

  • 0.4.0


37
38
39
40
# File 'lib/glyph/bookmark.rb', line 37

def ==(b)
	raise RuntimeError, "#{b.inspect} is not a bookmark" unless b.is_a? Glyph::Bookmark
	self.code == b.code && self.file == b.file
end

#codeObject

Returns the bookmark ID.

Since:

  • 0.4.0


30
31
32
# File 'lib/glyph/bookmark.rb', line 30

def code
	@id
end

Returns the appropriate link path to the bookmark, depending on the specified file

Parameters:

  • file (String) (defaults to: nil)

    the file where the link to the bookmark must be placed

Returns:

  • (String)

    the link to the bookmark

Since:

  • 0.4.0


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/glyph/bookmark.rb', line 45

def link(file=nil)
	if multiple_output_files? then
		dest_file = @file.to_s
		dest_file += '.glyph' unless dest_file.match /\..+$/
		dest_file.gsub!(/^text\//, '') unless Glyph.lite?
		external_file = dest_file.to_s.gsub(/\..+$/, Glyph["output.#{Glyph['document.output']}.extension"]) 
		f = (file.blank? || file != @file) ? "#{Glyph["output.#{Glyph['document.output']}.base"]}#{external_file}" : ""
		"#{f}##{@id}"
	else
		"##{@id}"
	end
end

#to_sString Also known as: to_str

Returns the bookmark id

Returns:

  • (String)

    the bookmark ID

Since:

  • 0.4.0


60
61
62
# File 'lib/glyph/bookmark.rb', line 60

def to_s
	@id.to_s
end