update_pygments.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. class LogicodevStyle(Style): # pylint: disable=R0903
  16. """Logicodev style
  17. based on https://github.com/searx/searx/blob/2a5c39e33c3306ca17e09211fbf5a0f785cb10c8/searx/static/themes/oscar/less/logicodev/code.less
  18. """ # pylint: disable=C0301
  19. background_color = '#282C34'
  20. styles = {
  21. Comment: "#556366 italic",
  22. Comment.Multiline: "#556366 italic",
  23. Comment.Preproc: "#BC7A00",
  24. Comment.Single: "#556366 italic",
  25. Comment.Special: "#556366 italic",
  26. Error: "border:#ff0000",
  27. Generic.Deleted: "#A00000",
  28. Generic.Emph: "italic",
  29. Generic.Error: "#FF0000",
  30. Generic.Heading: "#000080 bold",
  31. Generic.Inserted: "#00A000",
  32. Generic.Output: "#888888",
  33. Generic.Prompt: "#000080 bold",
  34. Generic.Strong: "bold",
  35. Generic.Subheading: "#800080 bold",
  36. Generic.Traceback: "#0044DD",
  37. Keyword: "#BE74D5 bold",
  38. Keyword.Constant: "#BE74D5 bold",
  39. Keyword.Declaration: "#BE74D5 bold",
  40. Keyword.Namespace: "#BE74D5 bold",
  41. Keyword.Pseudo: "#BE74D5",
  42. Keyword.Reserved: "#BE74D5 bold",
  43. Keyword.Type: "#D46C72",
  44. Literal.Number: "#D19A66",
  45. Literal.String: "#86C372",
  46. Literal.String.Backtick:"#86C372",
  47. Literal.String.Char: "#86C372",
  48. Literal.String.Doc: "#86C372 italic",
  49. Literal.String.Double: "#86C372",
  50. Literal.String.Escape: "#BB6622 bold",
  51. Literal.String.Heredoc: "#86C372",
  52. Literal.String.Interpol:"#BB6688 bold",
  53. Literal.String.Other: "#BE74D5",
  54. Literal.String.Regex: "#BB6688",
  55. Literal.String.Single: "#86C372",
  56. Literal.String.Symbol: "#DFC06F",
  57. Name.Attribute: "#7D9029",
  58. Name.Builtin: "#BE74D5",
  59. Name.Builtin.Pseudo: "#BE74D5",
  60. Name.Class: "#61AFEF bold",
  61. Name.Constant: "#D19A66",
  62. Name.Decorator: "#AA22FF",
  63. Name.Entity: "#999999 bold",
  64. Name.Exception: "#D2413A bold",
  65. Name.Function: "#61AFEF",
  66. Name.Label: "#A0A000",
  67. Name.Namespace: "#61AFEF bold",
  68. Name.Tag: "#BE74D5 bold",
  69. Name.Variable: "#DFC06F",
  70. Name.Variable.Class: "#DFC06F",
  71. Name.Variable.Global: "#DFC06F",
  72. Name.Variable.Instance: "#DFC06F",
  73. Operator: "#D19A66",
  74. Operator.Word: "#AA22FF bold",
  75. Text.Whitespace: "#D7DAE0",
  76. }
  77. CSSCLASS = '.code-highlight'
  78. RULE_CODE_LINENOS = """ .linenos {
  79. -webkit-touch-callout: none;
  80. -webkit-user-select: none;
  81. -khtml-user-select: none;
  82. -moz-user-select: none;
  83. -ms-user-select: none;
  84. user-select: none;
  85. cursor: default;
  86. &::selection {
  87. background: transparent; /* WebKit/Blink Browsers */
  88. }
  89. &::-moz-selection {
  90. background: transparent; /* Gecko Browsers */
  91. }
  92. margin-right: 8px;
  93. text-align: right;
  94. }"""
  95. def get_output_filename(relative_name):
  96. return join(searx_dir, relative_name)
  97. def get_css(cssclass, style):
  98. result = f"""/*
  99. this file is generated automatically by searxng_extra/update/update_pygments.py
  100. using pygments version {pygments.__version__}
  101. */\n\n"""
  102. css_text = HtmlFormatter(style=style).get_style_defs(cssclass)
  103. result += cssclass + RULE_CODE_LINENOS + '\n\n'
  104. for line in css_text.splitlines():
  105. if ' ' in line and not line.startswith(cssclass):
  106. line = cssclass + ' ' + line
  107. result += line + '\n'
  108. return result
  109. def main():
  110. fname = 'static/themes/oscar/src/generated/pygments-logicodev.less'
  111. print("update: %s" % fname)
  112. with open(get_output_filename(fname), 'w') as f:
  113. f.write(get_css(CSSCLASS, LogicodevStyle))
  114. fname = 'static/themes/oscar/src/generated/pygments-pointhi.less'
  115. print("update: %s" % fname)
  116. with open(get_output_filename(fname), 'w') as f:
  117. f.write(get_css(CSSCLASS, 'default'))
  118. fname = 'static/themes/simple/src/generated/pygments.less'
  119. print("update: %s" % fname)
  120. with open(get_output_filename(fname), 'w') as f:
  121. f.write(get_css(CSSCLASS, 'default'))
  122. if __name__ == '__main__':
  123. main()