update_pygments.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. """
  4. Update pygments style
  5. Call this script after each upgrade of pygments
  6. """
  7. # pylint: disable=C0116
  8. # set path
  9. from os.path import join
  10. import pygments
  11. from pygments.formatters import HtmlFormatter # pylint: disable=E0611
  12. from pygments.style import Style
  13. from pygments.token import Comment, Error, Generic, Keyword, Literal, Name, Operator, Text
  14. from searx import searx_dir
  15. CSSCLASS = '.code-highlight'
  16. RULE_CODE_LINENOS = """ .linenos {
  17. -webkit-touch-callout: none;
  18. -webkit-user-select: none;
  19. -khtml-user-select: none;
  20. -moz-user-select: none;
  21. -ms-user-select: none;
  22. user-select: none;
  23. cursor: default;
  24. &::selection {
  25. background: transparent; /* WebKit/Blink Browsers */
  26. }
  27. &::-moz-selection {
  28. background: transparent; /* Gecko Browsers */
  29. }
  30. margin-right: 8px;
  31. text-align: right;
  32. }"""
  33. def get_output_filename(relative_name):
  34. return join(searx_dir, relative_name)
  35. def get_css(cssclass, style):
  36. result = f"""/*
  37. this file is generated automatically by searxng_extra/update/update_pygments.py
  38. using pygments version {pygments.__version__}
  39. */\n\n"""
  40. css_text = HtmlFormatter(style=style).get_style_defs(cssclass)
  41. result += cssclass + RULE_CODE_LINENOS + '\n\n'
  42. for line in css_text.splitlines():
  43. if ' ' in line and not line.startswith(cssclass):
  44. line = cssclass + ' ' + line
  45. result += line + '\n'
  46. return result
  47. def main():
  48. fname = 'static/themes/simple/src/generated/pygments.less'
  49. print("update: %s" % fname)
  50. with open(get_output_filename(fname), 'w') as f:
  51. f.write(get_css(CSSCLASS, 'default'))
  52. if __name__ == '__main__':
  53. main()