def style_string(code, lexer=PanelcodeLexer(), style=SolarizedStyle, full=False): """Render plaintext Panelcode as syntax-highlighting HTML. Code is returned as tokens tagged with <span class="sometokenID">. Uses a custom Panelcode lexer and a color style that targets Panelcode token types and distributions. The style argument can be a string naming any built-in style, e.g. 'paraiso-dark'. """ formatter = HtmlFormatter(style=style) formatter.full = full html_str = highlight(code, lexer, formatter) return html_str
def all_styles(code, outpath, lexer=PanelcodeLexer(), prefix='test_', suffix='.html'): """Given code, outputs an HTML file for each available built-in style. A testing class for quickly previewing how a particular approach to lexer token naming will look across a variety of pre-existing styles. """ # print(STYLE_MAP.keys()) for smkey in STYLE_MAP.keys(): # pylint: disable=C0201 fname = outpath + '/' + prefix + smkey + suffix with open(fname, 'w') as htmlfile: formatter = HtmlFormatter(style=smkey) formatter.full = True highlight(code, lexer, formatter, outfile=htmlfile)
def show_paste(name): if not is_valid_filename(name): return abort(404) syntax = False keys = request.args.keys() if keys: syntax = keys[0] syntax = syntax.replace('.volt', '.d').replace('.v', '.d') syntax = 'd' if syntax == 'v' else syntax path = os.path.join(app.config['PASTE'], name) if not os.path.exists(path): return abort(404) with codecs.open(path, 'r', 'utf-8') as f: if syntax: lexer = get_best_lexer('x.' + syntax, syntax) formatter = HtmlFormatter(encoding=None, linenos=True) formatter.full = True return highlight(f.read(), lexer, formatter) return Response(f.read(), mimetype='text/plain')