update_pygments.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. from pathlib import Path
  7. import pygments
  8. from pygments.formatters import HtmlFormatter
  9. from searx import searx_dir
  10. LESS_FILE = Path(searx_dir) / 'static/themes/simple/src/generated/pygments.less'
  11. HEADER = f"""\
  12. /*
  13. this file is generated automatically by searxng_extra/update/update_pygments.py
  14. using pygments version {pygments.__version__}
  15. */
  16. """
  17. START_LIGHT_THEME = """
  18. .code-highlight {
  19. """
  20. END_LIGHT_THEME = """
  21. }
  22. """
  23. START_DARK_THEME = """
  24. .code-highlight-dark(){
  25. .code-highlight {
  26. """
  27. END_DARK_THEME = """
  28. }
  29. }
  30. """
  31. class Formatter(HtmlFormatter):
  32. @property
  33. def _pre_style(self):
  34. return 'line-height: 100%;'
  35. def get_style_lines(self, arg=None):
  36. style_lines = []
  37. style_lines.extend(self.get_linenos_style_defs())
  38. style_lines.extend(self.get_background_style_defs(arg))
  39. style_lines.extend(self.get_token_style_defs(arg))
  40. return style_lines
  41. def generat_css(light_style, dark_style) -> str:
  42. css = HEADER + START_LIGHT_THEME
  43. for line in Formatter(style=light_style).get_style_lines():
  44. css += '\n ' + line
  45. css += END_LIGHT_THEME + START_DARK_THEME
  46. for line in Formatter(style=dark_style).get_style_lines():
  47. css += '\n ' + line
  48. css += END_DARK_THEME
  49. return css
  50. if __name__ == '__main__':
  51. print("update: %s" % LESS_FILE)
  52. with open(LESS_FILE, 'w') as f:
  53. f.write(generat_css('default', 'lightbulb'))