Class: Rouge::Lexer
- Inherits:
-
Object
show all
- Defined in:
- lib/rouge/lexer.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opts = {}, &b) ⇒ Lexer
-*- instance methods -*- #
97
98
99
|
# File 'lib/rouge/lexer.rb', line 97
def initialize(opts={}, &b)
options(opts)
end
|
Class Method Details
.aliases(*args) ⇒ Object
77
78
79
|
# File 'lib/rouge/lexer.rb', line 77
def aliases(*args)
args.each { |arg| Lexer.register(arg, self) }
end
|
.analyze_text(text) ⇒ Object
return a number between 0 and 1 indicating the likelihood that the text given should be lexed with this lexer.
157
158
159
|
# File 'lib/rouge/lexer.rb', line 157
def self.analyze_text(text)
0
end
|
.default_options ⇒ Object
11
12
13
|
# File 'lib/rouge/lexer.rb', line 11
def default_options
@default_options ||= {}
end
|
.filenames(*fnames) ⇒ Object
81
82
83
|
# File 'lib/rouge/lexer.rb', line 81
def filenames(*fnames)
(@filenames ||= []).concat(fnames)
end
|
.find(name) ⇒ Object
15
16
17
|
# File 'lib/rouge/lexer.rb', line 15
def find(name)
registry[name.to_s]
end
|
.guess(info = {}) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/rouge/lexer.rb', line 19
def guess(info={})
by_mimetype = guess_by_mimetype(info[:mimetype]) if info[:mimetype]
return by_mimetype if by_mimetype
by_filename = guess_by_filename(info[:filename]) if info[:filename]
return by_filename if by_filename
by_source = guess_by_source(info[:source]) if info[:source]
return by_source if by_source
return Lexers::Text
end
|
.guess_by_filename(fname) ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/rouge/lexer.rb', line 39
def guess_by_filename(fname)
fname = File.basename(fname)
registry.values.detect do |lexer|
lexer.filenames.any? do |pattern|
File.fnmatch?(pattern, fname)
end
end
end
|
.guess_by_mimetype(mt) ⇒ Object
33
34
35
36
37
|
# File 'lib/rouge/lexer.rb', line 33
def guess_by_mimetype(mt)
registry.values.detect do |lexer|
lexer.mimetypes.include? mt
end
end
|
.guess_by_source(source) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/rouge/lexer.rb', line 48
def guess_by_source(source)
source = TextAnalyzer.new(source)
best_result = 0
best_match = nil
registry.values.each do |lexer|
result = lexer.analyze_text(source) || 0
return lexer if result == 1
if result > best_result
best_match = lexer
best_result = result
end
end
best_match
end
|
.lex(stream, opts = {}, &b) ⇒ Object
7
8
9
|
# File 'lib/rouge/lexer.rb', line 7
def lex(stream, opts={}, &b)
new(opts).lex(stream, &b)
end
|
.mimetypes(*mts) ⇒ Object
85
86
87
|
# File 'lib/rouge/lexer.rb', line 85
def mimetypes(*mts)
(@mimetypes ||= []).concat(mts)
end
|
.register(name, lexer) ⇒ Object
66
67
68
|
# File 'lib/rouge/lexer.rb', line 66
def register(name, lexer)
registry[name.to_s] = lexer
end
|
.tag(t = nil) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/rouge/lexer.rb', line 70
def tag(t=nil)
return @tag if t.nil?
@tag = t.to_s
aliases @tag
end
|
Instance Method Details
#debug(&b) ⇒ Object
115
116
117
|
# File 'lib/rouge/lexer.rb', line 115
def debug(&b)
puts(b.call) if option :debug
end
|
#get_tokens(stream) ⇒ Object
119
120
121
|
# File 'lib/rouge/lexer.rb', line 119
def get_tokens(stream)
lex(stream).to_a
end
|
#lex(string, opts = {}, &b) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/rouge/lexer.rb', line 127
def lex(string, opts={}, &b)
return enum_for(:lex, string) unless block_given?
reset! unless opts[:continue]
last_token = nil
last_val = nil
stream_tokens(StringScanner.new(string)) do |tok, val|
next if val.empty?
if tok == last_token
last_val << val
next
end
b.call(last_token, last_val) if last_token
last_token = tok
last_val = val
end
b.call(last_token, last_val) if last_token
end
|
#option(k, v = :absent) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/rouge/lexer.rb', line 107
def option(k, v=:absent)
if v == :absent
options[k]
else
options({ k => v })
end
end
|
#options(o = {}) ⇒ Object
101
102
103
104
105
|
# File 'lib/rouge/lexer.rb', line 101
def options(o={})
(@options ||= {}).merge!(o)
self.class.default_options.merge(@options)
end
|
#reset! ⇒ Object
123
124
125
|
# File 'lib/rouge/lexer.rb', line 123
def reset!
end
|
#stream_tokens(stream, &b) ⇒ Object
150
151
152
|
# File 'lib/rouge/lexer.rb', line 150
def stream_tokens(stream, &b)
raise 'abstract'
end
|