update_pygments.py 1.7 KB

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