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 -*- #
122
123
124
125
|
# File 'lib/rouge/lexer.rb', line 122
def initialize(opts={}, &b)
options(opts)
@lazy_load_proc = b
end
|
Class Method Details
.aliases(*args) ⇒ Object
102
103
104
|
# File 'lib/rouge/lexer.rb', line 102
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.
177
178
179
|
# File 'lib/rouge/lexer.rb', line 177
def self.analyze_text(text)
0
end
|
.default_options ⇒ Object
36
37
38
|
# File 'lib/rouge/lexer.rb', line 36
def default_options
@default_options ||= {}
end
|
.filenames(*fnames) ⇒ Object
106
107
108
|
# File 'lib/rouge/lexer.rb', line 106
def filenames(*fnames)
(@filenames ||= []).concat(fnames)
end
|
.find(name) ⇒ Object
40
41
42
|
# File 'lib/rouge/lexer.rb', line 40
def find(name)
registry[name.to_s]
end
|
.guess(info = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/rouge/lexer.rb', line 44
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
64
65
66
67
68
69
70
71
|
# File 'lib/rouge/lexer.rb', line 64
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
58
59
60
61
62
|
# File 'lib/rouge/lexer.rb', line 58
def guess_by_mimetype(mt)
registry.values.detect do |lexer|
lexer.mimetypes.include? mt
end
end
|
.guess_by_source(source) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/rouge/lexer.rb', line 73
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
17
18
19
|
# File 'lib/rouge/lexer.rb', line 17
def lex(stream, opts={}, &b)
new(opts).lex(stream, &b)
end
|
.make(opts = {}, &b) ⇒ Object
7
8
9
10
11
12
13
14
15
|
# File 'lib/rouge/lexer.rb', line 7
def make(opts={}, &b)
_sup = self
Class.new(self) do
@lazy_load_proc = b
@default_options = _sup.default_options.merge(opts)
@parent = _sup
end
end
|
.mimetypes(*mts) ⇒ Object
110
111
112
|
# File 'lib/rouge/lexer.rb', line 110
def mimetypes(*mts)
(@mimetypes ||= []).concat(mts)
end
|
.new(*a, &b) ⇒ Object
31
32
33
34
|
# File 'lib/rouge/lexer.rb', line 31
def new(*a, &b)
force_load!
super(*a, &b)
end
|
.register(name, lexer) ⇒ Object
91
92
93
|
# File 'lib/rouge/lexer.rb', line 91
def register(name, lexer)
registry[name.to_s] = lexer
end
|
.tag(t = nil) ⇒ Object
95
96
97
98
99
100
|
# File 'lib/rouge/lexer.rb', line 95
def tag(t=nil)
return @tag if t.nil?
@tag = t.to_s
aliases @tag
end
|
Instance Method Details
#debug(&b) ⇒ Object
141
142
143
|
# File 'lib/rouge/lexer.rb', line 141
def debug(&b)
puts(b.call) if option :debug
end
|
#get_tokens(stream) ⇒ Object
145
146
147
|
# File 'lib/rouge/lexer.rb', line 145
def get_tokens(stream)
lex(stream).to_a
end
|
#lex(string, &b) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/rouge/lexer.rb', line 149
def lex(string, &b)
return enum_for(:lex, string) unless block_given?
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
133
134
135
136
137
138
139
|
# File 'lib/rouge/lexer.rb', line 133
def option(k, v=:absent)
if v == :absent
options[k]
else
options({ k => v })
end
end
|
#options(o = {}) ⇒ Object
127
128
129
130
131
|
# File 'lib/rouge/lexer.rb', line 127
def options(o={})
(@options ||= {}).merge!(o)
self.class.default_options.merge(@options)
end
|
#stream_tokens(stream, &b) ⇒ Object
170
171
172
|
# File 'lib/rouge/lexer.rb', line 170
def stream_tokens(stream, &b)
raise 'abstract'
end
|