Class: Rouge::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/rouge/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



3
4
5
# File 'lib/rouge/token.rb', line 3

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



4
5
6
# File 'lib/rouge/token.rb', line 4

def parent
  @parent
end

#shortnameObject

Returns the value of attribute shortname.



5
6
7
# File 'lib/rouge/token.rb', line 5

def shortname
  @shortname
end

Class Method Details

.baseObject



70
71
72
# File 'lib/rouge/token.rb', line 70

def base
  @base ||= new
end

.each_token(&b) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/rouge/token.rb', line 88

def each_token(&b)
  recurse = proc do |token|
    b.call(token)
    token.sub_tokens.each_value(&recurse)
  end

  base.sub_tokens.each_value(&recurse)
end

.get(name) ⇒ Object Also known as: []



74
75
76
77
78
# File 'lib/rouge/token.rb', line 74

def get(name)
  return name if name.is_a? Token

  base[name]
end

.token(name, shortname) ⇒ Object



82
83
84
85
86
# File 'lib/rouge/token.rb', line 82

def token(name, shortname)
  tok = get(name)
  tok.shortname = shortname
  tok
end

Instance Method Details

#===(other) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/rouge/token.rb', line 53

def ===(other)
  immediate = if self.class == other.class
    self == other
  else
    self.name == other
  end

  immediate || !!(other.parent && self === other.parent)
end

#[](name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/rouge/token.rb', line 32

def [](name)
  name = name.to_s

  name.split('.').inject(self) do |tok, name|
    tok.sub_tokens[name] || tok.make_single(name)
  end
end

#ancestors(&b) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/rouge/token.rb', line 44

def ancestors(&b)
  return enum_for(:ancestors) unless block_given?

  if parent
    yield self
    parent.ancestors(&b)
  end
end

#inspectObject



63
64
65
66
67
# File 'lib/rouge/token.rb', line 63

def inspect
  parts = [name.inspect]
  parts << shortname.inspect if shortname
  "#<Token #{parts.join(' ')}>"
end

#make(name, shortname = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/rouge/token.rb', line 25

def make(name, shortname=nil)
  names = name.split('.')
  names.inject(self) do |tok, name|
    tok.make_single(name)
  end
end

#make_single(name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rouge/token.rb', line 8

def make_single(name)
  name = name.to_s
  new_name = [self.name, name].compact.join('.')

  new_token = self.clone
  parent = self
  new_token.instance_eval do
    @name = new_name
    @parent = parent
    @sub_tokens = {}
  end

  sub_tokens[name] = new_token

  new_token
end

#sub_tokensObject



40
41
42
# File 'lib/rouge/token.rb', line 40

def sub_tokens
  @sub_tokens ||= {}
end