Esempio n. 1
0
def add_pygments_style(theme_meta: Dict[str, Any],
                       urwid_theme: ThemeSpec) -> None:
    """
    This function adds pygments styles for use in syntax
    highlighting of code blocks and inline code.
    pygments["styles"]:
        one of those available in pygments/styles.
    pygments["background"]:
        used to set a different background for codeblocks instead of the
        one used in the syntax style, if it doesn't match with
        the overall zt theme.
        The default is available as Eg: MaterialStyle.background_color
    pygments["overrides"]:
        used to override certain pygments styles to match to urwid format.
        It can also be used to customize the syntax style.
    """
    pygments = theme_meta["pygments"]
    pygments_styles = pygments["styles"]
    pygments_bg = pygments["background"]
    pygments_overrides = pygments["overrides"]

    term16_styles = term16.styles
    term16_bg = term16.background_color

    for token, css_class in STANDARD_TYPES.items():
        if css_class in pygments_overrides:
            pygments_styles[token] = pygments_overrides[css_class]

        # Inherit parent pygments style if not defined.
        # Eg: Use `String` if `String.Double` is not present.
        if pygments_styles[token] == "":
            try:
                t = [k for k, v in STANDARD_TYPES.items() if v == css_class[0]]
                pygments_styles[token] = pygments_styles[t[0]]
            except IndexError:
                pass

        if term16_styles[token] == "":
            try:
                t = [k for k, v in STANDARD_TYPES.items() if v == css_class[0]]
                term16_styles[token] = term16_styles[t[0]]
            except IndexError:
                pass

        new_style = (
            f"pygments:{css_class}",
            term16_styles[token],
            term16_bg,
            "bold",  # Mono style
            pygments_styles[token],
            pygments_bg,
        )
        urwid_theme.append(new_style)
'''

CLASS_START = '''class %sStyle(Style):

    """Pygments style %sStyle."""

    background_color = "%s"  %s
    highlight_color = "%s"   %s

    styles = {
%s
    }
'''

tokens = {"hll": "highlight_color"}
for k, v in ST.items():
    if v == "":
        continue
    tokens[v] = str(k).replace("Token.", '')


class Comments(object):

    """Comment strip class."""

    re_line_preserve = re.compile(r"\r?\n", re.MULTILINE)
    re_css_comment = re.compile(
        r'''(?x)
            (?P<comments>
                /\*[^*]*\*+(?:[^/*][^*]*\*+)*/  # multi-line comments
              | \s*//(?:[^\r\n])*               # single line comments
'''

CLASS_START = '''class %sStyle(Style):

    """Pygments style %sStyle."""

    background_color = "%s"  %s
    highlight_color = "%s"   %s

    styles = {
%s
    }
'''

tokens = {"hll": "highlight_color"}
for k, v in ST.items():
    if v == "":
        continue
    tokens[v] = str(k).replace("Token.", '')


class Comments(object):
    """Comment strip class."""

    re_line_preserve = re.compile(r"\r?\n", re.MULTILINE)
    re_css_comment = re.compile(
        r'''(?x)
            (?P<comments>
                /\*[^*]*\*+(?:[^/*][^*]*\*+)*/  # multi-line comments
              | \s*//(?:[^\r\n])*               # single line comments
            )
Esempio n. 4
0
#!/usr/bin/env
# -*- coding: utf-8 -*-
# filename = javascript
# author=KGerring
# date = 7/13/17
from startups import *
import sys, os
from pygments.lexers.javascript import JavascriptLexer
import re, regex
import pygments.token
from pygments.token import string_to_tokentype, Token, is_token_subtype, STANDARD_TYPES

STANDARD = {v: k for k, v in STANDARD_TYPES.items()}


class PeekableIterator(object):
    'Iterator that supports peeking at the next item in the iterable.'

    def __init__(self, iterable):
        self.iterator = iter(iterable)
        self.item = None

    def peek(self):
        'Get the next item in the iterable without advancing our position.'
        if (not self.item):
            try:
                self.item = next(self.iterator)
            except StopIteration:
                return None
        return self.item