File: style.py

package info (click to toggle)
golang-github-alecthomas-chroma-v2 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 9,472 kB
  • sloc: xml: 39,589; python: 422; javascript: 357; sh: 37; makefile: 36
file content (63 lines) | stat: -rwxr-xr-x 1,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import importlib
import sys

import pystache
from pygments.style import Style
from pygments.token import Token


TEMPLATE = r'''
package styles

import (
    "github.com/alecthomas/chroma/v2"
)

// {{upper_name}} style.
var {{upper_name}} = Register(chroma.MustNewStyle("{{name}}", chroma.StyleEntries{
{{#styles}}
    chroma.{{type}}: "{{style}}",
{{/styles}}
}))
'''


def to_camel_case(snake_str):
    components = snake_str.split('_')
    return ''.join(x.title() for x in components)


def translate_token_type(t):
    if t == Token:
        t = Token.Background
    return "".join(map(str, t))


def main():
    name = sys.argv[1]
    package_name, symbol_name = sys.argv[2].rsplit(sep=".", maxsplit=1)

    package = importlib.import_module(package_name)

    style_cls = getattr(package, symbol_name)

    assert issubclass(style_cls, Style), 'can only translate from Style subclass'

    styles = dict(style_cls.styles)
    bg = "bg:" + style_cls.background_color
    if Token in styles:
        styles[Token] += " " + bg
    else:
        styles[Token] = bg
    context = {
        'upper_name': style_cls.__name__[:-5],
        'name': name,
        'styles': [{'type': translate_token_type(t), 'style': s}
                   for t, s in styles.items() if s],
    }
    print(pystache.render(TEMPLATE, context))


if __name__ == '__main__':
    main()