Python pygments.formatters() Examples
The following are 30
code examples of pygments.formatters().
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
, or try the search function
.
Example #1
Source File: markdown2.py From Preeminent with MIT License | 6 votes |
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)
Example #2
Source File: main.py From pyformat.info with MIT License | 6 votes |
def generate_css(base_folder, target_folder): log.info("Generating CSS.") file_mapping = {} target_folder = target_folder try: target_folder.mkdir(parents=True) except FileExistsError: pass pygments_css = base_folder / '_pygments.scss' with open(str(pygments_css), 'w') as fp: fp.write(pygments.formatters.HtmlFormatter().get_style_defs( '.highlight')) for file_ in base_folder.glob('*.scss'): if not file_.name.startswith('_'): target_path = target_folder / (file_.stem + '.{}.css') target_path = compile_sass(file_, target_path) file_mapping[file_.name] = target_path.name return file_mapping
Example #3
Source File: markdown2.py From MarkdownLivePreview with MIT License | 6 votes |
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)
Example #4
Source File: markdown2easy.py From awesome-python3-webapp with GNU General Public License v2.0 | 6 votes |
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)
Example #5
Source File: markdown2.py From awesome-python3-webapp with GNU General Public License v2.0 | 6 votes |
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)
Example #6
Source File: markdown2.py From FastWordQuery with GNU General Public License v3.0 | 6 votes |
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)
Example #7
Source File: style_transfer.py From style_transfer with MIT License | 6 votes |
def printargs(): """Prints out all command-line parameters.""" switch = {BGColor.LIGHT: 'xcode', BGColor.DARK: 'vim', BGColor.UNKNOWN: 'default'} style = switch[terminal_bg()] pprint = print try: 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('% 16s: ' % key, end='') pprint(v) print()
Example #8
Source File: markdown.py From honeything with GNU General Public License v3.0 | 6 votes |
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 = HtmlCodeFormatter(cssclass="codehilite", **formatter_opts) return pygments.highlight(codeblock, lexer, formatter)
Example #9
Source File: markdown.py From honeything with GNU General Public License v3.0 | 6 votes |
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 = HtmlCodeFormatter(cssclass="codehilite", **formatter_opts) return pygments.highlight(codeblock, lexer, formatter)
Example #10
Source File: markdown2.py From python-webapp-blog with GNU General Public License v3.0 | 6 votes |
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)
Example #11
Source File: plugin_highlight.py From deen with Apache License 2.0 | 6 votes |
def add_argparser(argparser, *args, **kwargs): # Python 2 argparse does not support aliases if sys.version_info.major < 3 or \ (sys.version_info.major == 3 and sys.version_info.minor < 2): parser = argparser.add_parser(DeenPluginSyntaxHighlighter.cmd_name, help=DeenPluginSyntaxHighlighter.cmd_help) else: parser = argparser.add_parser(DeenPluginSyntaxHighlighter.cmd_name, help=DeenPluginSyntaxHighlighter.cmd_help, aliases=DeenPluginSyntaxHighlighter.aliases) parser.add_argument('plugindata', action='store', help='input data', nargs='?') parser.add_argument('--list', action='store_true', dest='list', default=False, help='list available lexers') parser.add_argument('--list-formatters', action='store_true', dest='listformatters', default=False, help='list available formatters') parser.add_argument('-f', '--file', dest='plugininfile', default=None, help='file name or - for STDIN', metavar='filename') parser.add_argument('--formatter', help='formatter to use', type=str.lower, default=None, metavar='formatter') parser.add_argument('-l', '--lexer', help='hash algorithm for signature', default=None, type=str.lower, metavar='lexer') parser.add_argument('-n', '--numbers', action='store_true', dest='numbers', default=False, help='print line numbers')
Example #12
Source File: markdown2.py From dl with Apache License 2.0 | 6 votes |
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)
Example #13
Source File: markdown2.py From dl with Apache License 2.0 | 6 votes |
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)
Example #14
Source File: markdown2.py From cloze-overlapper with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #15
Source File: markdown2.py From termite-visualizations with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 = HtmlCodeFormatter(cssclass="codehilite", **formatter_opts) return pygments.highlight(codeblock, lexer, formatter)
Example #16
Source File: browsertab.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
def _show_source_pygments(self) -> None: def show_source_cb(source: str) -> None: """Show source as soon as it's ready.""" # WORKAROUND for https://github.com/PyCQA/pylint/issues/491 # pylint: disable=no-member lexer = pygments.lexers.HtmlLexer() formatter = pygments.formatters.HtmlFormatter( full=True, linenos='table') # pylint: enable=no-member highlighted = pygments.highlight(source, lexer, formatter) tb = objreg.get('tabbed-browser', scope='window', window=self._tab.win_id) new_tab = tb.tabopen(background=False, related=True) new_tab.set_html(highlighted, self._tab.url()) new_tab.data.viewing_source = True self._tab.dump_async(show_source_cb)
Example #17
Source File: app_globals.py From allura with Apache License 2.0 | 5 votes |
def highlight(self, text, lexer=None, filename=None): if not text: if lexer == 'diff': return Markup('<em>File contents unchanged</em>') return Markup('<em>Empty file</em>') # Don't use line numbers for diff highlight's, as per [#1484] if lexer == 'diff': formatter = pygments.formatters.HtmlFormatter(cssclass='codehilite', linenos=False) else: formatter = self.pygments_formatter text = h.really_unicode(text) if lexer is None: if len(text) < asint(config.get('scm.view.max_syntax_highlight_bytes', 500000)): try: lexer = pygments.lexers.get_lexer_for_filename(filename, encoding='chardet') except pygments.util.ClassNotFound: pass else: lexer = pygments.lexers.get_lexer_by_name(lexer, encoding='chardet') if lexer is None or len(text) >= asint(config.get('scm.view.max_syntax_highlight_bytes', 500000)): # no highlighting, but we should escape, encode, and wrap it in # a <pre> text = cgi.escape(text) return Markup('<pre>' + text + '</pre>') else: return Markup(pygments.highlight(text, lexer, formatter))
Example #18
Source File: utils.py From ceph-lcm with Apache License 2.0 | 5 votes |
def colorize(text, color, lexer): if pygments is None or not color: return text lexer_obj = pygments.lexers.get_lexer_by_name(lexer, ensurenl=False) formatter_obj = pygments.formatters.get_formatter_by_name( "terminal", bg=color) colorized = pygments.highlight(text, lexer_obj, formatter_obj) return colorized
Example #19
Source File: plugin_highlight.py From deen with Apache License 2.0 | 5 votes |
def process(self, data, lexer=None, formatter=None): super(DeenPluginSyntaxHighlighter, self).process(data) if not lexer: lexer = pygments.lexers.TextLexer() if not formatter: formatter = pygments.formatters.NullFormatter() data = pygments.highlight(data, lexer, formatter) if not isinstance(data, (bytes, bytearray)): data = data.encode() return data
Example #20
Source File: highlight.py From ok with Apache License 2.0 | 5 votes |
def highlight(filename, source): """Highlights an input string into a list of HTML strings, one per line.""" if not source: return [] # pygments does not play nice with empty files try: lexer = pygments.lexers.guess_lexer_for_filename(filename, source, stripnl=False) except pygments.util.ClassNotFound: lexer = pygments.lexers.TextLexer(stripnl=False) highlighted = pygments.highlight(source, lexer, pygments.formatters.HtmlFormatter(nowrap=True)) return highlighted.splitlines(keepends=True)
Example #21
Source File: cli.py From bake with ISC License | 5 votes |
def echo_json(obj): _json = json.dumps(obj, indent=2) if sys.stdin.isatty(): _json = pygments.highlight( _json, pygments.lexers.JsonLexer(), pygments.formatters.TerminalFormatter() ) click.echo(_json, err=False)
Example #22
Source File: plugin_highlight.py From deen with Apache License 2.0 | 5 votes |
def process_cli(self, args): if not PYGMENTS: self.error = MissingDependencyException('pygments is not available') return if not self.content: if not args.plugindata: if not args.plugininfile: self.content = self.read_content_from_file('-') else: self.content = self.read_content_from_file(args.plugininfile) else: self.content = args.plugindata if not self.content: return style = pygments.styles.get_style_by_name('colorful') if args.lexer: lexer = pygments.lexers.get_lexer_by_name(args.lexer) else: lexer = pygments.lexers.guess_lexer(self.content.decode()) if args.formatter: self.log.info('Guessing formatter') formatter = pygments.formatters.get_formatter_by_name(args.formatter) else: import curses curses.setupterm() if curses.tigetnum('colors') >= 256: formatter = pygments.formatters.Terminal256Formatter(style=style, linenos=args.numbers) else: formatter = pygments.formatters.TerminalFormatter(linenos=args.numbers) return self.process(self.content, lexer=lexer, formatter=formatter)
Example #23
Source File: main.py From pyformat.info with MIT License | 5 votes |
def highlight(value): return pygments.highlight(value, pygments.lexers.PythonLexer(), pygments.formatters.HtmlFormatter())
Example #24
Source File: library.py From opsbro with MIT License | 5 votes |
def get_pygments(self): if self.__pygments is not None: return self.__pygments # try pygments for pretty printing if available try: import pygments import pygments.lexers import pygments.formatters except ImportError: pygments = None self.__pygments = pygments return self.__pygments
Example #25
Source File: __init__.py From hase with BSD 2-Clause "Simplified" License | 5 votes |
def fill_file_cache(self, filename: str, line: int) -> None: lexer, content, is_largefile = self.file_read_cache[filename] if lexer and content: try: if not is_largefile: formatter_opts = dict( linenos="inline", linespans="line", hl_lines=[line] ) html_formatter = pygments.formatters.get_formatter_by_name( "html", **formatter_opts ) css = html_formatter.get_style_defs(".highlight") source = pygments.format(lexer.get_tokens(content), html_formatter) self.file_cache[filename][line] = (css, source) else: minl = max(0, line - 30) maxl = min(len(content), line + 30) formatter_opts = dict( linenos="inline", linespans="line", hl_lines=[line - minl] ) html_formatter = pygments.formatters.get_formatter_by_name( "html", **formatter_opts ) css = html_formatter.get_style_defs(".highlight") source = pygments.format( lexer.get_tokens("".join(content[minl:maxl])), html_formatter ) self.file_cache[filename][line] = (css, source) except Exception as e: l.exception(e) self.file_cache[filename][line] = (None, None) else: self.file_cache[filename][line] = (None, None)
Example #26
Source File: __init__.py From hase with BSD 2-Clause "Simplified" License | 5 votes |
def fill_read_cache(self, filename: str, line: int) -> None: try: lexer = pygments.lexers.get_lexer_for_filename( str(filename) ) # type: RegexLexer formatter_opts = dict(linenos="inline", linespans="line", hl_lines=[line]) html_formatter = pygments.formatters.get_formatter_by_name( "html", **formatter_opts ) css = html_formatter.get_style_defs(".highlight") with open(str(filename)) as f: lines = f.readlines() if len(lines) < 1000: content = "".join(lines) tokens = lexer.get_tokens(content) source = pygments.format(tokens, html_formatter) self.file_cache[filename][line] = (css, source) self.file_read_cache[filename] = (lexer, content, False) else: minl = max(0, line - 30) maxl = min(len(lines), line + 30) formatter_opts = dict( linenos="inline", linespans="line", hl_lines=[line] ) html_formatter = pygments.formatters.get_formatter_by_name( "html", **formatter_opts ) css = html_formatter.get_style_defs(".highlight") source = pygments.format( lexer.get_tokens("".join(lines[minl:maxl])), html_formatter ) self.file_cache[filename][line] = (css, source) self.file_read_cache[filename] = (lexer, lines, True) except Exception as e: l.exception(e) self.file_cache[filename][line] = (None, None) self.file_read_cache[filename] = (None, None, False)
Example #27
Source File: protocol2xml.py From net.tcp-proxy with GNU General Public License v3.0 | 5 votes |
def parse(data, key): fp = BytesIO(data) build_dictionary(fp, key) records = Record.parse(fp) out = StringIO() print_records(records, fp=out) out.seek(0) if pygments is not None: print(pygments.highlight(out.read(), pygments.lexers.get_lexer_by_name('XML'), pygments.formatters.get_formatter_by_name('terminal'))) else: print(out.read())
Example #28
Source File: Code.py From phpsploit with GNU General Public License v3.0 | 5 votes |
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): string = self._code_value() if not USE_PYGMENTS: return string return pygments.highlight(string, self.lexer, self.formatter).strip() return ColoredCode
Example #29
Source File: prettier.py From python-devtools with MIT License | 5 votes |
def get_pygments(): try: import pygments from pygments.lexers import PythonLexer from pygments.formatters import Terminal256Formatter except ImportError: # pragma: no cover return None, None, None else: return pygments, PythonLexer(), Terminal256Formatter(style='vim')
Example #30
Source File: deps_tree.py From dephell with MIT License | 5 votes |
def _colorize(content: str) -> str: try: import pygments import pygments.lexers import pygments.formatters except ImportError: return content content = pygments.highlight( code=content, lexer=pygments.lexers.MarkdownLexer(), formatter=pygments.formatters.TerminalFormatter(), ) return content.strip()