update_pygments.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. """Update pygments style
  4. Call this script after each upgrade of pygments
  5. """
  6. # pylint: disable=too-few-public-methods
  7. from pathlib import Path
  8. import pygments
  9. from pygments.formatters.html import HtmlFormatter
  10. from searx import searx_dir
  11. LESS_FILE = Path(searx_dir) / 'static/themes/simple/src/generated/pygments.less'
  12. HEADER = f"""\
  13. /*
  14. this file is generated automatically by searxng_extra/update/update_pygments.py
  15. using pygments version {pygments.__version__}
  16. */
  17. """
  18. START_LIGHT_THEME = """
  19. .code-highlight {
  20. """
  21. END_LIGHT_THEME = """
  22. }
  23. """
  24. START_DARK_THEME = """
  25. .code-highlight-dark(){
  26. .code-highlight {
  27. """
  28. END_DARK_THEME = """
  29. }
  30. }
  31. """
  32. class Formatter(HtmlFormatter): # pylint: disable=missing-class-docstring
  33. @property
  34. def _pre_style(self):
  35. return 'line-height: 100%;'
  36. def get_style_lines(self, arg=None):
  37. style_lines = []
  38. style_lines.extend(self.get_linenos_style_defs())
  39. style_lines.extend(self.get_background_style_defs(arg))
  40. style_lines.extend(self.get_token_style_defs(arg))
  41. return style_lines
  42. def generat_css(light_style, dark_style) -> str:
  43. css = HEADER + START_LIGHT_THEME
  44. for line in Formatter(style=light_style).get_style_lines():
  45. css += '\n ' + line
  46. css += END_LIGHT_THEME + START_DARK_THEME
  47. for line in Formatter(style=dark_style).get_style_lines():
  48. css += '\n ' + line
  49. css += END_DARK_THEME
  50. return css
  51. if __name__ == '__main__':
  52. print("update: %s" % LESS_FILE)
  53. with LESS_FILE.open('w', encoding='utf8') as f:
  54. f.write(generat_css('default', 'lightbulb'))