pygsgallery.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. # Render sample code with all Pygments styles
  3. # Print it to stdout
  4. #
  5. # If the file __file__.css (pygsgallery.py.css) exists,
  6. # its content will be prepended to the result, in <style> tags
  7. import re
  8. from pygments import __version__,highlight
  9. from pygments.formatters import HtmlFormatter
  10. from pygments.lexers import CssLexer,PythonLexer
  11. from pygments.styles import get_all_styles
  12. from sys import stderr
  13. def print_err(*args, **kwargs):
  14. print(*args, file=stderr, **kwargs)
  15. def complementaryColor(my_hex):
  16. """Returns complementary RGB color
  17. Example:
  18. >>>complementaryColor('FFFFFF')
  19. '000000'
  20. """
  21. rgb = (my_hex[0:2], my_hex[2:4], my_hex[4:6])
  22. comp = ['%02X' % (255 - int(a, 16)) for a in rgb]
  23. return ''.join(comp)
  24. raw_code_css = """a:hover {
  25. \tbackground: url("https://x.co/1.jpg");
  26. \ttransform: skew(-8deg) translate(0,0);
  27. }"""
  28. raw_code_py = """#!/usr/bin/python
  29. from sys import __version__,path
  30. def get_syntax():
  31. \tlx = ed.get_prop(CARET)
  32. \tif lx in lexers:
  33. \t\tprint('V: %s' % sys.__version__)"""
  34. out='<div class="all">'
  35. toc='<input id="dd" type="checkbox"/><label class=boxed for="dd">TOC</label><div class="boxed ddc">'
  36. post='</div>'+'<p>Pygments version: '+__version__+'</p>'
  37. count=0
  38. for style in sorted(list(get_all_styles())): # one <div> with header & code block for each style
  39. count+=1
  40. print_err("Processing style "+style)
  41. f_html = HtmlFormatter(nowrap=True,style=style,noclasses=True)
  42. css=f_html.get_style_defs('.' + style)
  43. bg_only=re.findall('\n\.'+style+' +\{ *background: *#(......); *}',css) # find a rule that defines default background only, but not foreground?
  44. if bg_only: # If yes, we add the foreground as a complementary color of the background:
  45. comp=complementaryColor(bg_only[0])
  46. css=re.sub('\n\.'+style+' +\{ *background: *#'+bg_only[0]+'; *}','\n.'+style+' { background: #'+bg_only[0]+'; color: #'+comp+' /*gen*/ }',css,1)
  47. bg=re.findall('\n\.'+style+' +\{ *background: *#(......); color: *#......;?',css) # finding the default background
  48. fg=re.findall('\n\.'+style+' +\{ *background: *#......; color: *#(......);?',css) # finding the default color
  49. css=css.splitlines(keepends=True)[6] # just 1 line from the style's css
  50. css=css.replace("; color:",";\n\tcolor:")
  51. css=css.replace("{ background:","{\n\tbackground:")
  52. css=css.replace(" }","\n}")
  53. css='/* Just some CSS */\n'+css+raw_code_css
  54. code=highlight(css,CssLexer(),f_html)+highlight(raw_code_py,PythonLexer(),f_html)
  55. out=out+'<div><h2 id="%s-%03d">%s (%03d)<a href="#"></a></h2>' % (style,count,style,count)
  56. toc=toc+'<a href="#%s-%03d">%s (%03d)</a>' % (style,count,style,count)
  57. out=out+'<pre style="background:#'+bg[0]+';color:#'+fg[0]+'">'+code+'</pre></div>' # closing <div class="w">
  58. toc=toc+'</div>'
  59. out=toc+out+post
  60. with open(__file__+'.css',mode='r',encoding="utf-8") as f:
  61. css=f.read();
  62. out='<style>'+css+'</style>'+out
  63. try:
  64. # not strictly necessary, but saves a few %
  65. # pip install minify-html
  66. import minify_html
  67. print_err("Minifying output... ")
  68. print(minify_html.minify(out,minify_css=True,remove_bangs=True,remove_processing_instructions=True))
  69. except:
  70. print(out)