我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用pygments.highlight()。
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
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 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 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 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 _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)
def save(self, *args, **kwargs): """ Use the `pygments` library to create a highlighted HTML representation of the code snippet. """ lexer = get_lexer_by_name(self.language) linenos = self.linenos and 'table' or False options = self.title and {'title': self.title} or {} formatter = HtmlFormatter(style=self.style, linenos=linenos, full=True, **options) self.highlighted = highlight(self.code, lexer, formatter) super(Snippet, self).save(*args, **kwargs) # limit the number of instances retained snippets = Snippet.objects.all() if len(snippets) > 100: snippets[0].delete()
def show_asm(self, item, primary): cur = self.db_cursor() if primary: db = "main" else: db = "diff" ea = str(int(item[1], 16)) sql = "select prototype, assembly, name from %s.functions where address = ?" sql = sql % db cur.execute(sql, (ea, )) row = cur.fetchone() if row is None: Warning("Sorry, there is no assembly available for the selected function.") else: fmt = HtmlFormatter() fmt.noclasses = True fmt.linenos = True asm = self.prettify_asm(row["assembly"]) final_asm = "; %s\n%s proc near\n%s\n%s endp\n" final_asm = final_asm % (row["prototype"], row["name"], asm, row["name"]) src = highlight(final_asm, NasmLexer(), fmt) title = "Assembly for %s" % row["name"] cdiffer = CHtmlViewer() cdiffer.Show(src, title) cur.close()
def show_pseudo(self, item, primary): cur = self.db_cursor() if primary: db = "main" else: db = "diff" ea = str(int(item[1], 16)) sql = "select prototype, pseudocode, name from %s.functions where address = ?" sql = sql % db cur.execute(sql, (str(ea), )) row = cur.fetchone() if row is None or row["prototype"] is None or row["pseudocode"] is None: Warning("Sorry, there is no pseudo-code available for the selected function.") else: fmt = HtmlFormatter() fmt.noclasses = True fmt.linenos = True func = "%s\n%s" % (row["prototype"], row["pseudocode"]) src = highlight(func, CppLexer(), fmt) title = "Pseudo-code for %s" % row["name"] cdiffer = CHtmlViewer() cdiffer.Show(src, title) cur.close()
def get_experiment(path): logdir = APP.config['logdir'] complete_path = os.path.join(logdir, path) if os.path.isfile(complete_path): file_content = get_file(complete_path) if path.endswith(".log"): result = ansiconv.to_html(html.escape(file_content)) elif path.endswith(".ini"): lexer = IniLexer() formatter = HtmlFormatter(linenos=True) result = highlight(file_content, lexer, formatter) else: result = "Unknown file type: '{}'.".format(complete_path) else: result = "File '{}' does not exist.".format(complete_path) return Response(result, mimetype='text/html', status=200)
def show_response(self, response): """Print an HTTP Response.""" if response.headers.get('Content-Type', None) == 'application/json': try: body = json.dumps(response.json(), indent=2) except json.JSONDecodeError: body = response.text else: body = response.text http_txt = self.HTTP_TPL.substitute( http_version=str(float(response.raw.version) / 10), status_code=response.status_code, reason=response.reason, headers=self.key_value_pairs(response.headers), body=body, ) return highlight(http_txt, self.http_lexer, self.formatter)
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">%s</div>\n' % code return code except: return '<pre class="%s"><code>%s</code></pre>\n' % ( lang, mistune.escape(text) )
def block_code(self, text, lang): linenos = inlinestyles = 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 = HtmlFormatter( noclasses=inlinestyles, linenos=linenos, cssclass='codehilite' ) code = highlight(text, lexer, formatter) if linenos: return '<div class="highlight-wrapper">%s</div>\n' % code return '<div class="doc doc-code">%s</div>%s' % (lang.upper(), code) except: return '<pre class="%s"><code>%s</code></pre>\n' % ( lang, mistune.escape(text) )
def __call__(self, value: Any, *, indent: int=0, indent_first: bool=False, highlight: bool=False): self._stream = io.StringIO() self._format(value, indent_current=indent, indent_first=indent_first) s = self._stream.getvalue() if highlight and pyg_lexer: # apparently highlight adds a trailing new line we don't want s = pygments.highlight(s, lexer=pyg_lexer, formatter=pyg_formatter).rstrip('\n') return s
def pprint(s, file=None): highlight = isatty(file) if force_highlight is None else force_highlight print(pformat(s, highlight=highlight), file=file, flush=True)
def block_code(self, code, lang): guess = 'python3' if code.lstrip().startswith('<?php'): guess = 'php' elif code.lstrip().startswith(('<', '{%')): guess = 'html+jinja' elif code.lstrip().startswith(('function', 'var', '$')): guess = 'javascript' lexer = get_lexer_by_name(lang or guess, stripall=True) return highlight(code, lexer, HtmlFormatter()) # ???????md?????????????????????????
def syntax_highlight(filename='', language=None): mako_lexer = MakoLexer() if compat.py3k: python_lexer = Python3Lexer() else: python_lexer = PythonLexer() if filename.startswith('memory:') or language == 'mako': return lambda string: highlight(string, mako_lexer, pygments_html_formatter) return lambda string: highlight(string, python_lexer, pygments_html_formatter)
def _create_html_file(self, sourcefile, htmlfile, errors): name = self.generator.get_name() root = ElementTree.fromstring(CPPCHECK_HTML_FILE) title = root.find('head/title') title.text = 'cppcheck - report - %s' % name body = root.find('body') for div in body.findall('div'): if div.get('id') == 'page': page = div break for div in page.findall('div'): if div.get('id') == 'header': h1 = div.find('h1') h1.text = 'cppcheck report - %s' % name if div.get('id') == 'content': content = div srcnode = self.generator.bld.root.find_node(sourcefile) hl_lines = [e['line'] for e in errors if e.has_key('line')] formatter = CppcheckHtmlFormatter(linenos=True, style='colorful', hl_lines=hl_lines, lineanchors='line') formatter.errors = [e for e in errors if e.has_key('line')] css_style_defs = formatter.get_style_defs('.highlight') lexer = pygments.lexers.guess_lexer_for_filename(sourcefile, "") s = pygments.highlight(srcnode.read(), lexer, formatter) table = ElementTree.fromstring(s) content.append(table) s = ElementTree.tostring(root, method='html') s = CCPCHECK_HTML_TYPE + s node = self.generator.path.get_bld().find_or_declare(htmlfile) node.write(s) return css_style_defs