class Link

Handles logic for Link tokens

Public Class Methods

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

Public Instance Methods

node_representation() click to toggle source
# File lib/rosetta/tokens/link.rb, line 30
def node_representation
  "<#{type} value='#{value}' url='#{url}'>"
end
to_s() click to toggle source
# File lib/rosetta/tokens/link.rb, line 26
def to_s
  "<Token type='#{type}' value='#{value}' url='#{url}'>"
end
type() click to toggle source
# File lib/rosetta/tokens/link.rb, line 34
def type
  :LINK
end
url() click to toggle source
# 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
value() click to toggle source
# 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

Private Instance Methods

url_beginning_index() click to toggle source
# File lib/rosetta/tokens/link.rb, line 52
def url_beginning_index
  open_bracket_count = 0

  @url_beginning_index ||= @source_text.each_char.with_index do |character, index|
    case character
    when '['
      open_bracket_count += 1
    when ']'
      open_bracket_count -= 1
    end

    return index + 1 if open_bracket_count.zero?
  end
end