Python pygments.formatters.HtmlFormatter() Examples
The following are 30
code examples of pygments.formatters.HtmlFormatter().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pygments.formatters
, or try the search function
.
Example #1
Source File: wiki.py From pygameweb with BSD 2-Clause "Simplified" License | 6 votes |
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)
Example #2
Source File: stacktracer.py From newspaper-crawler-scripts with GNU General Public License v3.0 | 6 votes |
def stacktraces(): code = [] for threadId, stack in sys._current_frames().items(): code.append("\n# ThreadID: %s" % threadId) for filename, lineno, name, line in traceback.extract_stack(stack): code.append('File: "%s", line %d, in %s' % (filename, lineno, name)) if line: code.append(" %s" % (line.strip())) return highlight("\n".join(code), PythonLexer(), HtmlFormatter( full=False, # style="native", noclasses=True, )) # This part was made by nagylzs
Example #3
Source File: views.py From airflow with Apache License 2.0 | 6 votes |
def code(self, session=None): all_errors = "" try: dag_id = request.args.get('dag_id') dag_orm = DagModel.get_dagmodel(dag_id, session=session) code = DagCode.get_code_by_fileloc(dag_orm.fileloc) html_code = Markup(highlight( code, lexers.PythonLexer(), HtmlFormatter(linenos=True))) except Exception as e: all_errors += ( "Exception encountered during " + "dag_id retrieval/dag retrieval fallback/code highlighting:\n\n{}\n".format(e) ) html_code = Markup('<p>Failed to load file.</p><p>Details: {}</p>').format( escape(all_errors)) return self.render_template( 'airflow/dag_code.html', html_code=html_code, dag=dag_orm, title=dag_id, root=request.args.get('root'), demo_mode=conf.getboolean('webserver', 'demo_mode'), wrapped=conf.getboolean('webserver', 'default_wrap'))
Example #4
Source File: test_html_formatter.py From pygments with BSD 2-Clause "Simplified" License | 6 votes |
def test_external_css(): # test correct behavior # CSS should be in /tmp directory fmt1 = HtmlFormatter(full=True, cssfile='fmt1.css', outencoding='utf-8') # CSS should be in TESTDIR (TESTDIR is absolute) fmt2 = HtmlFormatter(full=True, cssfile=path.join(TESTDIR, 'fmt2.css'), outencoding='utf-8') tfile = tempfile.NamedTemporaryFile(suffix='.html') fmt1.format(tokensource, tfile) try: fmt2.format(tokensource, tfile) assert path.isfile(path.join(TESTDIR, 'fmt2.css')) except IOError: # test directory not writable pass tfile.close() assert path.isfile(path.join(path.dirname(tfile.name), 'fmt1.css')) os.unlink(path.join(path.dirname(tfile.name), 'fmt1.css')) try: os.unlink(path.join(TESTDIR, 'fmt2.css')) except OSError: pass
Example #5
Source File: diaphora_ida.py From maltindex with GNU General Public License v2.0 | 6 votes |
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()
Example #6
Source File: diaphora_ida.py From maltindex with GNU General Public License v2.0 | 6 votes |
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()
Example #7
Source File: test_basic_api.py From pygments with BSD 2-Clause "Simplified" License | 6 votes |
def test_formatter_encodings(): from pygments.formatters import HtmlFormatter # unicode output fmt = HtmlFormatter() tokens = [(Text, u"ä")] out = format(tokens, fmt) assert type(out) is str assert u"ä" in out # encoding option fmt = HtmlFormatter(encoding="latin1") tokens = [(Text, u"ä")] assert u"ä".encode("latin1") in format(tokens, fmt) # encoding and outencoding option fmt = HtmlFormatter(encoding="latin1", outencoding="utf8") tokens = [(Text, u"ä")] assert u"ä".encode("utf8") in format(tokens, fmt)
Example #8
Source File: r_rpy2.py From pyABC with BSD 3-Clause "New" or "Revised" License | 6 votes |
def display_source_ipython(self): """ Convenience method to print the loaded source file as syntax highlighted HTML within IPython. """ from pygments import highlight from pygments.lexers import SLexer from pygments.formatters import HtmlFormatter import IPython.display as display with open(self.source_file) as f: code = f.read() formatter = HtmlFormatter() return display.HTML('<style type="text/css">{}</style>{}'.format( formatter.get_style_defs('.highlight'), highlight(code, SLexer(), formatter)))
Example #9
Source File: controlflowgraph.py From barf-project with BSD 2-Clause "Simplified" License | 6 votes |
def _render_asm(self, instr, mnemonic_width, options, fill_char=""): formatter = HtmlFormatter() formatter.noclasses = True formatter.nowrap = True oprnds_str = ", ".join([oprnd.to_string(**options) for oprnd in instr.operands]) asm_str = instr.prefix + " " if instr.prefix else "" asm_str += instr.mnemonic + fill_char * (mnemonic_width - len(instr.mnemonic)) asm_str += " " + oprnds_str if oprnds_str else "" # TODO Highlight for ARM too. asm_str = highlight(asm_str, NasmLexer(), formatter) asm_str = asm_str.replace("span", "font") asm_str = asm_str.replace('style="color: ', 'color="') asm_str = asm_str.replace('style="border: 1px solid ', 'color="') return self.asm_tpl.format(address=instr.address, size=instr.size, assembly=asm_str)
Example #10
Source File: test_html_formatter.py From pygments with BSD 2-Clause "Simplified" License | 6 votes |
def test_valid_output(): # test all available wrappers fmt = HtmlFormatter(full=True, linenos=True, noclasses=True, outencoding='utf-8') handle, pathname = tempfile.mkstemp('.html') with os.fdopen(handle, 'w+b') as tfile: fmt.format(tokensource, tfile) catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc') try: import subprocess po = subprocess.Popen(['nsgmls', '-s', '-c', catname, pathname], stdout=subprocess.PIPE) ret = po.wait() output = po.stdout.read() po.stdout.close() except OSError: # nsgmls not available pass else: if ret: print(output) assert not ret, 'nsgmls run reported errors' os.unlink(pathname)
Example #11
Source File: test_html_formatter.py From pygments with BSD 2-Clause "Simplified" License | 6 votes |
def test_get_style_defs_contains_style_specific_line_numbers_styles(): class TestStyle(Style): line_number_color = '#ff0000' line_number_background_color = '#0000ff' line_number_special_color = '#00ff00' line_number_special_background_color = '#ffffff' style_defs = HtmlFormatter(style=TestStyle).get_style_defs().splitlines() assert style_defs[1] == ( 'td.linenos pre ' '{ color: #ff0000; background-color: #0000ff; padding: 0 5px 0 5px; }' ) assert style_defs[2] == ( 'span.linenos ' '{ color: #ff0000; background-color: #0000ff; padding: 0 5px 0 5px; }' ) assert style_defs[3] == ( 'td.linenos pre.special ' '{ color: #00ff00; background-color: #ffffff; padding: 0 5px 0 5px; }' ) assert style_defs[4] == ( 'span.linenos.special ' '{ color: #00ff00; background-color: #ffffff; padding: 0 5px 0 5px; }' )
Example #12
Source File: highlight.py From mistune-contrib with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 = HtmlFormatter( noclasses=inlinestyles, linenos=linenos ) code = highlight(text, lexer, formatter) if linenos: return '<div class="highlight-wrapper">%s</div>\n' % code return code except: return '<pre class="%s"><code>%s</code></pre>\n' % ( lang, mistune.escape(text) )
Example #13
Source File: ui_demo.py From chaquopy with MIT License | 6 votes |
def view_source(context, web_view, filename): from base64 import b64encode from os.path import join from pygments import highlight from pygments.formatters import HtmlFormatter from pygments.lexers import get_lexer_for_filename from java.io import BufferedReader, InputStreamReader stream = context.getAssets().open(join(ASSET_SOURCE_DIR, filename)) reader = BufferedReader(InputStreamReader(stream)) text = "\n".join(iter(reader.readLine, None)) formatter = HtmlFormatter() body = highlight(text, get_lexer_for_filename(filename), formatter) html = ("<html><head><style>{}\n{}</style></head><body>{}</body></html>" .format(formatter.get_style_defs(), EXTRA_CSS, body)).encode() web_view.loadData(b64encode(html).decode(), "text/html", "base64")
Example #14
Source File: logbook.py From neuralmonkey with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #15
Source File: logbook.py From neuralmonkey with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #16
Source File: logbook.py From neuralmonkey with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #17
Source File: server.py From orca with BSD 3-Clause "New" or "Revised" License | 6 votes |
def column_definition(table_name, col_name): """ Get the source of a column function. If a column is a registered Series and not a function then all that is returned is {'type': 'series'}. If the column is a registered function then the JSON returned has keys "type", "filename", "lineno", "text", and "html". "text" is the raw text of the function, "html" has been marked up by Pygments. """ col_type = orca.get_table(table_name).column_type(col_name) if col_type != 'function': return jsonify(type=col_type) filename, lineno, source = \ orca.get_raw_column(table_name, col_name).func_source_data() html = highlight(source, PythonLexer(), HtmlFormatter()) return jsonify( type='function', filename=filename, lineno=lineno, text=source, html=html)
Example #18
Source File: pygments_highlights.py From ara with GNU General Public License v3.0 | 6 votes |
def format_data(data): formatter = HtmlFormatter(cssclass="codehilite") if data is None: return data if isinstance(data, bool) or isinstance(data, int) or isinstance(data, float): return highlight(str(data), TextLexer(), formatter) elif isinstance(data, str): try: data = json.dumps(json.loads(data), indent=4, sort_keys=True) lexer = JsonLexer() except (ValueError, TypeError): lexer = TextLexer() elif isinstance(data, dict) or isinstance(data, list): data = json.dumps(data, indent=4, sort_keys=True) lexer = JsonLexer() else: lexer = TextLexer() lexer.stripall = True return highlight(data, lexer, formatter)
Example #19
Source File: pygments2xpre.py From stdm with GNU General Public License v2.0 | 6 votes |
def pygments2xpre(s, language="python"): "Return markup suitable for XPreformatted" try: from pygments import highlight from pygments.formatters import HtmlFormatter except ImportError: return s from pygments.lexers import get_lexer_by_name l = get_lexer_by_name(language) h = HtmlFormatter() from StringIO import StringIO out = StringIO() highlight(s,l,h,out) styles = [(cls, style.split(';')[0].split(':')[1].strip()) for cls, (style, ttype, level) in h.class2style.iteritems() if cls and style and style.startswith('color:')] return _2xpre(out.getvalue(),styles)
Example #20
Source File: server.py From orca with BSD 3-Clause "New" or "Revised" License | 6 votes |
def injectable_definition(inj_name): """ Get the source of an injectable function. If an injectable is a registered Python variable and not a function then all that is returned is {'type': 'variable'}. If the column is a registered function then the JSON returned has keys "type", "filename", "lineno", "text", and "html". "text" is the raw text of the function, "html" has been marked up by Pygments. """ inj_type = orca.injectable_type(inj_name) if inj_type == 'variable': return jsonify(type='variable') else: filename, lineno, source = \ orca.get_injectable_func_source_data(inj_name) html = highlight(source, PythonLexer(), HtmlFormatter()) return jsonify( type='function', filename=filename, lineno=lineno, text=source, html=html)
Example #21
Source File: server.py From orca with BSD 3-Clause "New" or "Revised" License | 6 votes |
def table_definition(table_name): """ Get the source of a table function. If a table is registered DataFrame and not a function then all that is returned is {'type': 'dataframe'}. If the table is a registered function then the JSON returned has keys "type", "filename", "lineno", "text", and "html". "text" is the raw text of the function, "html" has been marked up by Pygments. """ if orca.table_type(table_name) == 'dataframe': return jsonify(type='dataframe') filename, lineno, source = \ orca.get_raw_table(table_name).func_source_data() html = highlight(source, PythonLexer(), HtmlFormatter()) return jsonify( type='function', filename=filename, lineno=lineno, text=source, html=html)
Example #22
Source File: md.py From codimension with GNU General Public License v3.0 | 6 votes |
def block_code(uuid, text, lang, inlinestyles=False, linenos=False): """Renders a code block""" if is_plant_uml(text, lang): fName = Settings().plantUMLCache.getResource(text, uuid) if fName is None: return '<br/><img src="cdm-image:na50.png"><br/>\n' return '<br/><img src="plantuml:' + fName + '"><br/>\n' lexer = get_lexer(text, lang) if lexer: try: formatter = HtmlFormatter(noclasses=inlinestyles, linenos=linenos) code = highlight(text, lexer, formatter) return ''.join([PRE_WRAP_START, '<pre>', code.replace('125%', '100%'), '</pre>', PRE_WRAP_END, '\n']) except: pass return ''.join([PRE_WRAP_START, '<pre>', mistune.escape(text), '</pre>', PRE_WRAP_END, '\n'])
Example #23
Source File: test_cmdline.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def test_normal(): # test that cmdline gives the same output as library api from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter filename = TESTFILE with open(filename, 'rb') as fp: code = fp.read() output = highlight(code, PythonLexer(), HtmlFormatter()) o = check_success('-lpython', '-fhtml', filename) assert o == output
Example #24
Source File: test_html_formatter.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def test_lineanchors(): optdict = dict(lineanchors="foo") outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() assert re.search("<pre><span></span><a name=\"foo-1\">", html)
Example #25
Source File: test_html_formatter.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def test_get_style_defs_contains_default_line_numbers_styles(): style_defs = HtmlFormatter().get_style_defs().splitlines() assert style_defs[1] == ( 'td.linenos pre ' '{ color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }' ) assert style_defs[2] == ( 'span.linenos ' '{ color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }' )
Example #26
Source File: html.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def html_sourcecode(sourcecode): from pygments import highlight from pygments.lexers import SLexer from pygments.formatters import HtmlFormatter formatter = HtmlFormatter() htmlcode = highlight(sourcecode, SLexer(), formatter) d = {'sourcecode': htmlcode, 'syntax_highlighting': formatter.get_style_defs()} html = template_sourcecode.render(d) return html
Example #27
Source File: test_basic_api.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def test_bare_class_handler(): from pygments.formatters import HtmlFormatter from pygments.lexers import PythonLexer try: lex('test\n', PythonLexer) except TypeError as e: assert 'lex() argument must be a lexer instance' in str(e) else: assert False, 'nothing raised' try: format([], HtmlFormatter) except TypeError as e: assert 'format() argument must be a formatter instance' in str(e) else: assert False, 'nothing raised'
Example #28
Source File: test_html_formatter.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def test_get_token_style_defs_uses_css_prefix( formatter_kwargs, style_defs_args, assert_starts_with, assert_contains ): formatter = HtmlFormatter(**formatter_kwargs) for line in formatter.get_token_style_defs(*style_defs_args): assert line.startswith(assert_starts_with) for s in assert_contains: assert s in line
Example #29
Source File: test_html_formatter.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def test_all_options(): def check(optdict): outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) for optdict in [ dict(nowrap=True), dict(linenos=True, full=True), dict(linenos=True, linespans='L'), dict(hl_lines=[1, 5, 10, 'xxx']), dict(hl_lines=[1, 5, 10], noclasses=True), ]: check(optdict) for linenos in [False, 'table', 'inline']: for noclasses in [False, True]: for linenospecial in [0, 5]: for anchorlinenos in [False, True]: optdict = dict( linenos=linenos, noclasses=noclasses, linenospecial=linenospecial, anchorlinenos=anchorlinenos, ) check(optdict)
Example #30
Source File: test_html_formatter.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def test_get_background_style_defs_uses_multiple_css_prefixes(): formatter = HtmlFormatter() lines = formatter.get_background_style_defs([".foo", ".bar"]) assert lines[0].startswith(".foo .hll, .bar .hll {") assert lines[1].startswith(".foo , .bar {")