class Rouge::Lexer
@abstract A lexer transforms text into a stream of `[token, chunk]` pairs.
Attributes
-*- instance methods -*- #
Public Class Methods
Used to specify alternate names this lexer class may be found by.
@example
class Erb < Lexer tag 'erb' aliases 'eruby', 'rhtml' end Lexer.find('eruby') # => Erb
# File lib/rouge/lexer.rb, line 218 def aliases(*args) args.map!(&:to_s) args.each { |arg| Lexer.register(arg, self) } (@aliases ||= []).concat(args) end
@return a list of all lexers.
# File lib/rouge/lexer.rb, line 115 def all registry.values.uniq end
@abstract
Return a number between 0 and 1 indicating the likelihood that the text given should be lexed with this lexer. The default implementation returns 0. Values under 0.5 will only be used to disambiguate filename or mimetype matches.
@param [TextAnalyzer] text
the text to be analyzed, with a couple of handy methods on it, like {TextAnalyzer#shebang?} and {TextAnalyzer#doctype?}
# File lib/rouge/lexer.rb, line 435 def self.analyze_text(text) 0 end
@private
# File lib/rouge/lexer.rb, line 245 def assert_utf8!(str) return if %w(US-ASCII UTF-8 ASCII-8BIT).include? str.encoding.name raise EncodingError.new( "Bad encoding: #{str.encoding.names.join(',')}. " + "Please convert your string to UTF-8." ) end
# File lib/rouge/lexer.rb, line 181 def debug_enabled? !!@debug_enabled end
Specify or get a small demo string for this lexer
# File lib/rouge/lexer.rb, line 108 def demo(arg=:absent) return @demo = arg unless arg == :absent @demo = File.read(demo_file, encoding: 'utf-8') end
Specify or get the path name containing a small demo for this lexer (can be overriden by {demo}).
# File lib/rouge/lexer.rb, line 101 def demo_file(arg=:absent) return @demo_file = Pathname.new(arg) unless arg == :absent @demo_file = Pathname.new(__FILE__).dirname.join('demos', tag) end
Specify or get this lexer's description.
# File lib/rouge/lexer.rb, line 83 def desc(arg=:absent) if arg == :absent @desc else @desc = arg end end
# File lib/rouge/lexer.rb, line 177 def disable_debug! @debug_enabled = false end
# File lib/rouge/lexer.rb, line 173 def enable_debug! @debug_enabled = true end
Specify a list of filename globs associated with this lexer.
@example
class Ruby < Lexer filenames '*.rb', '*.ruby', 'Gemfile', 'Rakefile' end
# File lib/rouge/lexer.rb, line 230 def filenames(*fnames) (@filenames ||= []).concat(fnames) end
Given a string, return the correct lexer class.
# File lib/rouge/lexer.rb, line 25 def find(name) registry[name.to_s] end
Find a lexer, with fancy shiny features.
-
The string you pass can include CGI-style options
Lexer.find_fancy('erb?parent=tex')
-
You can pass the special name 'guess' so we guess for you, and you can pass a second argument of the code to guess by
Lexer.find_fancy('guess', "#!/bin/bash\necho Hello, world")
This is used in the Redcarpet plugin as well as Rouge's own markdown lexer for highlighting internal code blocks.
# File lib/rouge/lexer.rb, line 43 def find_fancy(str, code=nil, additional_options={}) if str && !str.include?('?') && str != 'guess' lexer_class = find(str) return lexer_class && lexer_class.new(additional_options) end name, opts = str ? str.split('?', 2) : [nil, ''] # parse the options hash from a cgi-style string opts = CGI.parse(opts || '').map do |k, vals| val = case vals.size when 0 then true when 1 then vals[0] else vals end [ k.to_s, val ] end opts = additional_options.merge(Hash[opts]) lexer_class = case name when 'guess', nil self.guess(:source => code, :mimetype => opts['mimetype']) when String self.find(name) end lexer_class && lexer_class.new(opts) end
Guess which lexer to use based on a hash of info.
@option info :mimetype
A mimetype to guess by
@option info :filename
A filename to guess by
@option info :source
The source itself, which, if guessing by mimetype or filename fails, will be searched for shebangs, <!DOCTYPE ...> tags, and other hints.
@see Lexer.analyze_text
@see Lexer.guesses
# File lib/rouge/lexer.rb, line 152 def guess(info={}) lexers = guesses(info) return Lexers::PlainText if lexers.empty? return lexers[0] if lexers.size == 1 raise Guesser::Ambiguous.new(lexers) end
# File lib/rouge/lexer.rb, line 165 def guess_by_filename(fname) guess :filename => fname end
# File lib/rouge/lexer.rb, line 161 def guess_by_mimetype(mt) guess :mimetype => mt end
# File lib/rouge/lexer.rb, line 169 def guess_by_source(source) guess :source => source end
Guess which lexer to use based on a hash of info.
This accepts the same arguments as Lexer.guess
, but will never throw an error. It will return a (possibly empty) list of potential lexers to use.
# File lib/rouge/lexer.rb, line 124 def guesses(info={}) mimetype, filename, source = info.values_at(:mimetype, :filename, :source) custom_globs = info[:custom_globs] guessers = (info[:guessers] || []).dup guessers << Guessers::Mimetype.new(mimetype) if mimetype guessers << Guessers::GlobMapping.by_pairs(custom_globs, filename) if custom_globs && filename guessers << Guessers::Filename.new(filename) if filename guessers << Guessers::Modeline.new(source) if source guessers << Guessers::Source.new(source) if source Guesser.guess(guessers, Lexer.all) end
Lexes `stream` with the given options. The lex is delegated to a new instance.
@see lex
# File lib/rouge/lexer.rb, line 20 def lex(stream, opts={}, &b) new(opts).lex(stream, &b) end
Specify a list of mimetypes associated with this lexer.
@example
class Html < Lexer mimetypes 'text/html', 'application/xhtml+xml' end
# File lib/rouge/lexer.rb, line 240 def mimetypes(*mts) (@mimetypes ||= []).concat(mts) end
Create a new lexer with the given options. Individual lexers may specify extra options. The only current globally accepted option is `:debug`.
@option opts :debug
Prints debug information to stdout. The particular info depends on the lexer in question. In regex lexers, this will log the state stack at the beginning of each step, along with each regex tried and each stream consumed. Try it, it's pretty useful.
# File lib/rouge/lexer.rb, line 271 def initialize(opts={}) @options = {} opts.each { |k, v| @options[k.to_s] = v } @debug = Lexer.debug_enabled? && bool_option(:debug) end
# File lib/rouge/lexer.rb, line 95 def option(name, desc) option_docs[name.to_s] = desc end
# File lib/rouge/lexer.rb, line 91 def option_docs @option_docs ||= InheritableHash.new(superclass.option_docs) end
Used to specify or get the canonical name of this lexer class.
@example
class MyLexer < Lexer tag 'foo' end MyLexer.tag # => 'foo' Lexer.find('foo') # => MyLexer
# File lib/rouge/lexer.rb, line 202 def tag(t=nil) return @tag if t.nil? @tag = t.to_s Lexer.register(@tag, self) end
Specify or get this lexer's title. Meant to be human-readable.
# File lib/rouge/lexer.rb, line 75 def title(t=nil) if t.nil? t = tag.capitalize end @title ||= t end
Protected Class Methods
@private
# File lib/rouge/lexer.rb, line 187 def register(name, lexer) registry[name.to_s] = lexer end
Private Class Methods
# File lib/rouge/lexer.rb, line 254 def registry @registry ||= {} end
Public Instance Methods
# File lib/rouge/lexer.rb, line 278 def as_bool(val) case val when nil, false, 0, '0', 'off' false when Array val.empty? ? true : as_bool(val.last) else true end end
# File lib/rouge/lexer.rb, line 306 def as_lexer(val) return as_lexer(val.last) if val.is_a?(Array) return val.new(@options) if val.is_a?(Class) && val < Lexer case val when Lexer val when String lexer_class = Lexer.find(val) lexer_class && lexer_class.new(@options) end end
# File lib/rouge/lexer.rb, line 295 def as_list(val) case val when Array val.flat_map { |v| as_list(v) } when String val.split(',') else [] end end
# File lib/rouge/lexer.rb, line 289 def as_string(val) return as_string(val.last) if val.is_a?(Array) val ? val.to_s : nil end
# File lib/rouge/lexer.rb, line 319 def as_token(val) return as_token(val.last) if val.is_a?(Array) case val when Token val else Token[val] end end
# File lib/rouge/lexer.rb, line 329 def bool_option(name, &default) if @options.key?(name.to_s) as_bool(@options[name.to_s]) else default ? default.call : false end end
# File lib/rouge/lexer.rb, line 353 def hash_option(name, defaults, &val_cast) name = name.to_s out = defaults.dup base = @options.delete(name.to_s) base = {} unless base.is_a?(Hash) base.each { |k, v| out[k.to_s] = val_cast ? val_cast.call(v) : v } @options.keys.each do |key| next unless key =~ /(\w+)\[(\w+)\]/ and $1 == name value = @options.delete(key) out[$2] = val_cast ? val_cast.call(value) : value end out end
Given a string, yield [token, chunk] pairs. If no block is given, an enumerator is returned.
@option opts :continue
Continue the lex from the previous state (i.e. don't call #reset!)
# File lib/rouge/lexer.rb, line 383 def lex(string, opts={}, &b) return enum_for(:lex, string, opts) unless block_given? Lexer.assert_utf8!(string) reset! unless opts[:continue] # consolidate consecutive tokens of the same type last_token = nil last_val = nil stream_tokens(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
# File lib/rouge/lexer.rb, line 341 def lexer_option(name, &default) as_lexer(@options.delete(name.to_s, &default)) end
# File lib/rouge/lexer.rb, line 345 def list_option(name, &default) as_list(@options.delete(name.to_s, &default)) end
@abstract
Called after each lex is finished. The default implementation is a noop.
# File lib/rouge/lexer.rb, line 375 def reset! end
@abstract
Yield `[token, chunk]` pairs, given a prepared input stream. This must be implemented.
@param [StringScanner] stream
the stream
# File lib/rouge/lexer.rb, line 421 def stream_tokens(stream, &b) raise 'abstract' end
# File lib/rouge/lexer.rb, line 337 def string_option(name, &default) as_string(@options.delete(name.to_s, &default)) end
delegated to {Lexer.tag}
# File lib/rouge/lexer.rb, line 410 def tag self.class.tag end
# File lib/rouge/lexer.rb, line 349 def token_option(name, &default) as_token(@options.delete(name.to_s, &default)) end