...

Text file src/github.com/alecthomas/chroma/v2/_tools/style.py

Documentation: github.com/alecthomas/chroma/v2/_tools

     1import importlib
     2import sys
     3
     4import pystache
     5from pygments.style import Style
     6from pygments.token import Token
     7
     8
     9TEMPLATE = r'''
    10package styles
    11
    12import (
    13    "github.com/alecthomas/chroma/v2"
    14)
    15
    16// {{upper_name}} style.
    17var {{upper_name}} = Register(chroma.MustNewStyle("{{name}}", chroma.StyleEntries{
    18{{#styles}}
    19    chroma.{{type}}: "{{style}}",
    20{{/styles}}
    21}))
    22'''
    23
    24
    25def to_camel_case(snake_str):
    26    components = snake_str.split('_')
    27    return ''.join(x.title() for x in components)
    28
    29
    30def translate_token_type(t):
    31    if t == Token:
    32        t = Token.Background
    33    return "".join(map(str, t))
    34
    35
    36def main():
    37    name = sys.argv[1]
    38    package_name, symbol_name = sys.argv[2].rsplit(sep=".", maxsplit=1)
    39
    40    package = importlib.import_module(package_name)
    41
    42    style_cls = getattr(package, symbol_name)
    43
    44    assert issubclass(style_cls, Style), 'can only translate from Style subclass'
    45
    46    styles = dict(style_cls.styles)
    47    bg = "bg:" + style_cls.background_color
    48    if Token in styles:
    49        styles[Token] += " " + bg
    50    else:
    51        styles[Token] = bg
    52    context = {
    53        'upper_name': style_cls.__name__[:-5],
    54        'name': name,
    55        'styles': [{'type': translate_token_type(t), 'style': s}
    56                   for t, s in styles.items() if s],
    57    }
    58    print(pystache.render(TEMPLATE, context))
    59
    60
    61if __name__ == '__main__':
    62    main()

View as plain text