Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lexer#with and Lexer.lookup_fancy #1565

Merged
merged 4 commits into from
Sep 26, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
split out Lexer.lookup_fancy from Lexer.find_fancy
  • Loading branch information
http://jneen.net/ committed Jul 21, 2020
commit f8c331b3e0490c1ea1b5873f31c122b2597c8b11
48 changes: 29 additions & 19 deletions lib/rouge/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,15 @@ 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")
# Same as ::find_fancy, except instead of returning an instantiated
# lexer, returns a pair of [lexer_class, options], so that you can
# modify or provide additional options to the lexer.
#
# If the code matches more than one lexer then Guesser::Ambiguous
# is raised.
#
# This is used in the Redcarpet plugin as well as Rouge's own
# markdown lexer for highlighting internal code blocks.
#
def find_fancy(str, code=nil, additional_options={})

# Please note: the lexer class might be nil!
def lookup_fancy(str, code=nil, additional_options={})
if str && !str.include?('?') && str != 'guess'
lexer_class = find(str)
return lexer_class && lexer_class.new(additional_options)
return [lexer_class, additional_options]
end

name, opts = str ? str.split('?', 2) : [nil, '']
Expand All @@ -84,6 +71,29 @@ def find_fancy(str, code=nil, additional_options={})
self.find(name)
end

[lexer_class, opts]
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")
#
# If the code matches more than one lexer then Guesser::Ambiguous
# is raised.
#
# This is used in the Redcarpet plugin as well as Rouge's own
# markdown lexer for highlighting internal code blocks.
#
def find_fancy(str, code=nil, additional_options={})
lexer_class, opts = lookup_fancy(str, code, additional_options)

lexer_class && lexer_class.new(opts)
end

Expand Down