Class: Link

Inherits:
BasicToken show all
Defined in:
lib/rosetta/tokens/link.rb

Overview

Handles logic for Link tokens

Constant Summary

Constants inherited from BasicToken

BasicToken::INLINE_CLASS_NAMES, BasicToken::TOP_LEVEL_CLASS_NAMES

Instance Attribute Summary

Attributes inherited from BasicToken

#source_text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicToken

#accept, #initialize, #inline?

Constructor Details

This class inherits a constructor from BasicToken

Class Method Details

.consume(text) ⇒ Object



19
20
21
22
23
24
# File 'lib/rosetta/tokens/link.rb', line 19

def self.consume(text)
  closing_index = text.rindex(')')
  source = text[0..closing_index]

  new(source)
end

.matches?(text) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rosetta/tokens/link.rb', line 7

def self.matches?(text)
  # Naive check for link.
  return false unless text.start_with?('[')

  closing_bracket_index = text.index(']')
  return false if closing_bracket_index.nil?

  return false if text[closing_bracket_index + 1] != '('

  text[closing_bracket_index + 1..].include?(')')
end

Instance Method Details

#node_representationObject



30
31
32
# File 'lib/rosetta/tokens/link.rb', line 30

def node_representation
  "<#{type} value='#{value}' url='#{url}'>"
end

#to_sObject



26
27
28
# File 'lib/rosetta/tokens/link.rb', line 26

def to_s
  "<Token type='#{type}' value='#{value}' url='#{url}'>"
end

#typeObject



34
35
36
# File 'lib/rosetta/tokens/link.rb', line 34

def type
  :LINK
end

#urlObject



45
46
47
48
# File 'lib/rosetta/tokens/link.rb', line 45

def url
  # We want the value inside the parenthesis, and should exclude the the parens themselves.
  @source_text[url_beginning_index + 1...-1]
end

#valueObject



38
39
40
41
42
43
# File 'lib/rosetta/tokens/link.rb', line 38

def value
  final_bracket_index = url_beginning_index - 1

  # We want the value inside brackets, and should exclude the brackets themselves.
  @source_text[1...final_bracket_index]
end