def format(self, record):
super(SqlFormatter, self).format(record)
sql = record.sql.strip()
if self.parse:
sql = sqlparse.format(sql, reindent=self.reindent, keyword_case=self.keyword_case)
if hasattr(record, 'duration'):
sql = "({0:.3f}ms) {1}".format(record.duration, sql)
if self.highlight:
sql = highlight(
sql,
self._lexer,
self._formatter
)
return sql
python类highlight()的实例源码
def patch_fenced_rule(self):
"""
Patch Python Markdown with our own fenced block extension.
if the Python Markdown's 'fenced_code' extension was already configured,
we will replace it.
"""
config = self.getConfigs()
fenced = SuperFencesBlockPreprocessor(self.markdown)
indented_code = SuperFencesCodeBlockProcessor(self)
fenced.config = config
indented_code.config = config
indented_code.markdown = self.markdown
hiliter = SuperFencesHiliteTreeprocessor(self.markdown)
hiliter.config = self.getConfigs()
self.markdown.treeprocessors["hilite"] = hiliter
self.markdown.superfences[0]["formatter"] = fenced.highlight
self.markdown.parser.blockprocessors['code'] = indented_code
self.markdown.preprocessors.add('fenced_code_block', fenced, ">normalize_whitespace")
def render_md(md_str):
"""
Render a markdown string
:param md_str: markdown string to render
:return: html text
"""
return misaka.Markdown(
renderer,
extensions=('fenced-code',
'tables',
'footnotes',
'autolink',
'strikethrough',
'underline',
'highlight',
'quote',
'superscript')
)(md_str)
# regex to match the post file with correct format
def block_code(text, lang, inlinestyles=False, linenos=False):
if not lang:
text = text.strip()
return u'<pre><code>%s</code></pre>\n' % mistune.escape(text)
try:
lexer = get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter(
noclasses=inlinestyles, linenos=linenos
)
code = highlight(text, lexer, formatter)
if linenos:
return '<div class="highlight-wrapper">%s</div>\n' % code
return code
except BaseException:
return '<pre class="%s"><code>%s</code></pre>\n' % (
lang, mistune.escape(text)
)
def block_code(self, code, lang):
"Block code highlighter and formater"
try:
if not lang:
lexer = guess_lexer(code, stripall=True)
else:
lexer = get_lexer_by_name(lang, stripall=True)
detected = True
code = highlight(code, lexer, self.code_formatter)
except:
code = escape(code)
lang = None
self.info.code.append(code)
template = self.jinja2.get_template('code')
rv = template.render(code=code, lang=lang, site=self.site, meta=self.meta)
rv = rv.encode('utf-8')
return rv
def unhighlight(text):
hits = re.findall(
'<div class="highlight"><pre><span></span>(?P<text>.+?)</pre></div>', text, re.M | re.S)
for h in hits:
# print 'h',h.strip()
if h.strip():
if h.find('<span') == -1: # it's note
# print 'no span'
h_and_context = re.findall(
r'<div class="highlight"><pre><span></span>' + re.escape(h) + '</pre></div>', text, re.M | re.S)
if h_and_context:
h_and_context = h_and_context[0]
h_and_context_unhigh = h_and_context.replace(
'<div class="highlight">', '').replace('</pre></div>', '</pre>')
text = text.replace(h_and_context, h_and_context_unhigh)
else:
h_and_context = re.findall(
r'<div class="highlight"><pre><span></span>' + re.escape(h) + '</pre></div>', text, re.M | re.S)
# print h_and_context
return text
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _format_code(html_code):
# syntax highlighting:
code_highlighted = highlight(html_code, HtmlLexer(), HtmlFormatter(style='monokai', noclasses=True))
# add formatting for diff-markers:
common_diff_style = ' margin-right: 3px; padding-right: 7px; padding-left: 7px;'
code_formatted = code_highlighted \
.replace('[-', '<span style="background: #71332c;' + common_diff_style + '">') \
.replace('-]', '</span>') \
.replace('{+', '<span style="background: #2e4d28;' + common_diff_style + '">') \
.replace('+}', '</span>')
# wrap in general layout:
code_wrapped = '<div style="color: #efefef; margin: 3px; margin-left: 17px;line-height: 150%%;">%s</div>'\
%(code_formatted)
return code_wrapped
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def patch_fenced_rule(self):
"""
Patch Python Markdown with our own fenced block extension.
if the Python Markdown's 'fenced_code' extension was already configured,
we will replace it.
"""
config = self.getConfigs()
fenced = SuperFencesBlockPreprocessor(self.markdown)
indented_code = SuperFencesCodeBlockProcessor(self)
fenced.config = config
indented_code.config = config
indented_code.markdown = self.markdown
hiliter = SuperFencesHiliteTreeprocessor(self.markdown)
hiliter.config = self.getConfigs()
self.markdown.treeprocessors["hilite"] = hiliter
self.markdown.superfences[0]["formatter"] = fenced.highlight
self.markdown.parser.blockprocessors['code'] = indented_code
self.markdown.preprocessors.add('fenced_code_block', fenced, ">normalize_whitespace")
def emit(self, record):
"""Pretty-print and syntax highlight a log statement if all these conditions are met:
* This is a DEBUG message
* We're outputting to a terminal
* The log message args is a dict containing keys starting with 'xml_' and values as bytes
"""
if record.levelno == logging.DEBUG and self.is_tty() and isinstance(record.args, dict):
for key, value in record.args.items():
if not key.startswith('xml_'):
continue
if not isinstance(value, bytes):
continue
if not is_xml(value[:10].decode('utf-8', errors='ignore')):
continue
try:
if PY2:
record.args[key] = self.highlight_xml(self.prettify_xml(value)).encode('utf-8')
else:
record.args[key] = self.highlight_xml(self.prettify_xml(value))
except Exception:
# Something bad happened, but we don't want to crash the program just because logging failed
pass
return super(PrettyXmlHandler, self).emit(record)
def display_matches(m, selector, filename, opts):
matches = matching_lines(m.match(selector, filename), filename)
if opts.get('l'):
files = {}
for line, no, _ in matches:
if opts.get('l'):
if filename not in files:
click.echo(filename)
# do not repeat files
files[filename] = True
else:
lines = {}
for line, no, col in matches:
text = highlight(line.strip(), PythonLexer(), TerminalFormatter())
if not opts['e']:
if no not in lines:
lines[no] = True
click.echo('{}:{} {}'.format(filename, no, text),
nl=False)
else:
click.echo('{}:{}:{} {}'.format(filename, no, col, text),
nl=False)
def highlight(text, mime=None, lang=None, linenos=False, title=""):
formatter = HTMLFormatter(
cssclass="code",
linenos=linenos,
full=True,
title=title
)
try:
if mime:
lexer = pygments.lexers.get_lexer_for_mimetype(mime)
elif lang:
lexer = pygments.lexers.get_lexer_by_name(lang)
else:
lexer = pygments.lexers.guess_lexer(text)
except pygments.util.ClassNotFound:
return tag.pre(text)
return Markup(pygments.highlight(text, lexer, formatter))
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def code(self):
dag_id = request.args.get('dag_id')
dag = dagbag.get_dag(dag_id)
title = dag_id
try:
with open(dag.fileloc, 'r') as f:
code = f.read()
html_code = highlight(
code, lexers.PythonLexer(), HtmlFormatter(linenos=True))
except IOError as e:
html_code = str(e)
return self.render(
'airflow/dag_code.html', html_code=html_code, dag=dag, title=title,
root=request.args.get('root'),
demo_mode=conf.getboolean('webserver', 'demo_mode'))
def format_resp(resp):
# I can see no way to get the HTTP version
headers = ["HTTP/1.1 %d %s" % (resp.status_code, resp.reason or '')]
headers.extend('%s: %s' % k for k in resp.headers.items())
headers = '\n'.join(headers)
if 'json' in resp.headers.get('Content-Type', '').lower():
body = json.dumps(resp.json(), sort_keys=True, indent=4)
else:
body = resp.content
if pygments:
mime = resp.headers.get('Content-Type')
http_lexer = pygments.lexers.get_lexer_by_name('http')
formatter = pygments.formatters.get_formatter_by_name(formatter_name)
try:
body_lexer = pygments.lexers.get_lexer_for_mimetype(mime)
except pygments.util.ClassNotFound:
body_lexer = pygments.lexers.get_lexer_by_name('text')
headers = pygments.highlight(headers, http_lexer, formatter)
body = pygments.highlight(body, body_lexer, formatter)
return '\n'.join([headers, '', body])
def printf(self,txt,append=True,highlight=False,curr=''):
if append:
try:
if hasattr(self.result,'page'):
curr = self.result.page().currentFrame().toHtml().replace('</body></html>','')
plain = self.result.page().currentFrame().toPlainText()
else:
curr = self.result.toHtml().replace('</body></html>','')
plain = self.result.toPlainText()
except:
traceback.print_exc()
curr = ''
txt = self.highlight(txt) if (highlight is True) else txt #highlight=1 can still be used to force highlighting
if (self.css and '<style>' not in curr) or not curr or not append:
txt = ('<html><head><style>%s</style></head><body>%s</body></html>'%(self.css,txt))
else:
txt = (str(curr)+'<br>'+txt+'</body></html>')
self.result.setHtml(txt)
try:
self.result.keyPressEvent(Qt.QKeyEvent(Qt.QEvent.KeyPress,Qt.Qt.Key_End,Qt.Qt.NoModifier))
#self.result.page().mainFrame().scroll(0,self.result.page().mainFrame().contentsSize().height())
except:
traceback.print_exc()
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def print_args():
"""Prints out all command-line parameters."""
bg = terminal_bg()
if bg is True:
style = 'xcode'
elif bg is False:
style = 'monokai'
pprint = print_
try:
if bg is not None:
import pygments
from pygments.lexers import Python3Lexer
from pygments.formatters import Terminal256Formatter
pprint = partial(pygments.highlight, lexer=Python3Lexer(),
formatter=Terminal256Formatter(style=style), outfile=sys.stdout)
except ImportError:
pass
print_('Parameters:')
for key in sorted(ARGS):
v = repr(getattr(ARGS, key))
print_('% 14s: ' % key, end='')
pprint(v)
print_()
def format(self, record):
sql = record.sql.strip()
if sqlparse:
# Indent the SQL query
sql = sqlparse.format(sql, reindent=True)
if pygments:
# Highlight the SQL query
sql = pygments.highlight(
sql,
SqlLexer(),
Terminal256Formatter(style='monokai')
)
record.statement = sql
return super(SQLFormatter, self).format(record)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
import pygments
import pygments.formatters
class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
for tup in inner:
yield tup
yield 0, "</code>"
def wrap(self, source, outfile):
"""Return the source with a code, pre, and div."""
return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
def _wiki_code(content):
"""
>>> u = _wiki_code('<code class="python">my_code()</code>')
>>> expected = '<div class="highlight"><pre><span class="n">my_code</span><span class="p">()</span>\\n</pre></div>\\n'
>>> expected in u
True
"""
css_class = "python"
if css_class:
content = content.replace('<code>', '<code class=\"%s\">' % css_class);
#syntax_css = u"<style>%s</style>" % HtmlFormatter().get_style_defs('.highlight')
#content = syntax_css + content
return re.sub(r'\<code\s+class\s*\=\s*\"([^\"]+)\"\>(.*?)\<\/code>',
_wiki_code_callback, content, flags=re.I | re.S)