summaryrefslogtreecommitdiff
path: root/searxng_extra/update/update_pygments.py
blob: 69a8ee2db108611ed92e3610607420ddee6e1c3e (plain)
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
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Update pygments style

Call this script after each upgrade of pygments

"""

from pathlib import Path
import pygments
from pygments.formatters import HtmlFormatter

from searx import searx_dir

LESS_FILE = Path(searx_dir) / 'static/themes/simple/src/generated/pygments.less'

HEADER = f"""\
/*
   this file is generated automatically by searxng_extra/update/update_pygments.py
   using pygments version {pygments.__version__}
*/

"""

START_LIGHT_THEME = """
.code-highlight {
"""

END_LIGHT_THEME = """
}
"""

START_DARK_THEME = """
.code-highlight-dark(){
  .code-highlight {
"""

END_DARK_THEME = """
  }
}
"""


class Formatter(HtmlFormatter):
    @property
    def _pre_style(self):
        return 'line-height: 100%;'

    def get_style_lines(self, arg=None):
        style_lines = []
        style_lines.extend(self.get_linenos_style_defs())
        style_lines.extend(self.get_background_style_defs(arg))
        style_lines.extend(self.get_token_style_defs(arg))
        return style_lines


def generat_css(light_style, dark_style) -> str:
    css = HEADER + START_LIGHT_THEME
    for line in Formatter(style=light_style).get_style_lines():
        css += '\n  ' + line
    css += END_LIGHT_THEME + START_DARK_THEME
    for line in Formatter(style=dark_style).get_style_lines():
        css += '\n    ' + line
    css += END_DARK_THEME
    return css


if __name__ == '__main__':
    print("update: %s" % LESS_FILE)
    with open(LESS_FILE, 'w') as f:
        f.write(generat_css('default', 'lightbulb'))