in lib/pygments/mentos.py [0:0]
def return_lexer(self, lexer, args, inputs, code=None):
"""
Accepting a variety of possible inputs, return a Lexer object.
The inputs argument should be a hash with at least one of the following
keys:
- 'lexer' ("python")
- 'mimetype' ("text/x-ruby")
- 'filename' ("yeaaah.py")
The code argument should be a string, such as "import derp".
The code guessing method is not especially great. It is advised that
clients pass in a literal lexer name whenever possible, which provides
the best probability of match (100 percent).
"""
if lexer:
if inputs:
return lexers.get_lexer_by_name(lexer, **inputs)
else:
return lexers.get_lexer_by_name(lexer)
if inputs:
if 'lexer' in inputs:
return lexers.get_lexer_by_name(inputs['lexer'], **inputs)
elif 'mimetype' in inputs:
return lexers.get_lexer_for_mimetype(inputs['mimetype'], **inputs)
elif 'filename' in inputs:
name = inputs['filename']
# If we have code and a filename, pygments allows us to guess
# with both. This is better than just guessing with code.
if code:
return lexers.guess_lexer_for_filename(name, code, **inputs)
else:
return lexers.get_lexer_for_filename(name, **inputs)
# If all we got is code, try anyway.
if code:
return lexers.guess_lexer(code, **inputs)
else:
return None