Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main(fn, lexer=None, options={}):
if lexer is not None:
lxcls = get_lexer_by_name(lexer).__class__
else:
lxcls = find_lexer_class_for_filename(os.path.basename(fn))
if lxcls is None:
name, rest = fn.split('_', 1)
lxcls = find_lexer_class(name)
if lxcls is None:
raise AssertionError('no lexer found for file %r' % fn)
print('Using lexer: %s (%s.%s)' % (lxcls.name, lxcls.__module__,
lxcls.__name__))
debug_lexer = False
# if profile:
# # does not work for e.g. ExtendedRegexLexers
# if lxcls.__bases__ == (RegexLexer,):
# # yes we can! (change the metaclass)
# lxcls.__class__ = ProfilingRegexLexerMeta
# lxcls.__bases__ = (ProfilingRegexLexer,)
def save(self):
if not self.title:
self.title = 'Untitled'
if not self.author:
self.author = 'anonymous'
if not self.uid:
self.uid = get_next_paste_uid(self.private)
if not self.language:
self.language = 'text'
lexer = get_lexer_by_name(self.language)
self.code = '\n'.join(self.code.splitlines())
self.parsed_code = highlight(self.code.decode('utf-8', 'ignore'),
lexer, formatter)
super(Paste, self).save()
def block_code(self, text, lang):
if not lang:
text = tornado.escape.xhtml_escape(text.strip())
return '\n<pre><code>%s</code></pre>\n' % text
lexer = get_lexer_by_name(lang, stripall=True)
formatter = HtmlFormatter()
return highlight(text, lexer, formatter)
def _get_lexer(self, lang):
if lang.lower() == 'sql':
return get_lexer_by_name('postgresql', **self.options)
tries = [ lang ]
if lang.startswith('pl'):
tries.append(lang[2:])
if lang.endswith('u'):
tries.append(lang[:-1])
if lang.startswith('pl') and lang.endswith('u'):
tries.append(lang[2:-1])
for l in tries:
try:
return get_lexer_by_name(l, **self.options)
except ClassNotFound:
pass
else:
# TODO: better logging
# print >>sys.stderr, "language not found:", lang
return None
def _get_lexer(self):
if self.language:
from pygments.lexers import get_lexer_by_name
return get_lexer_by_name(self.language)
elif self.filename:
from pygments.lexers import get_lexer_for_filename
return get_lexer_for_filename(self.filename)
else:
from pygments.lexers import guess_lexer
return guess_lexer(self.data)
def get_lexer(value,arg):
if arg is None:
return lexers.guess_lexer(value)
return lexers.get_lexer_by_name(arg)
def config(self):
import pygments
import pygments.lexers
import pygments.formatters
lexer = pygments.lexers.get_lexer_by_name('yaml')
formatter = pygments.formatters.HtmlFormatter()
config_source_formatted = pygments.highlight(self.handler.read_config_source_or_provide_default_one(), lexer, formatter)
self.render_view("admin/config.html", { 'config_source_formatted': config_source_formatted })
def _get_pygments_lexer(self, lexer_name):
try:
from pygments import lexers, util
except ImportError:
return None
try:
return lexers.get_lexer_by_name(lexer_name)
except util.ClassNotFound:
return None
def Code(language):
class ColoredCode(str):
"""Piece of source code. (extends str)
Takes a string representing a portion of source code.
When printed or when self.__str__() is called the code will be
formated using pygments if possible. self._code_value() is used
to retrieve the code to be formated, its default implementation
is to use self.__call__().
"""
if USE_PYGMENTS:
lexer = pygments.lexers.get_lexer_by_name(language)
if ui.output.colors() >= 256:
formatter = pygments.formatters.Terminal256Formatter
else:
formatter = pygments.formatters.TerminalFormatter
formatter = formatter(style='vim', bg='dark')
def _raw_value(self):
return super().__str__()
def __call__(self):
return self._raw_value()
def _code_value(self):
return self.__call__()
def __str__(self):