update_pygments.py 4.6 KB

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