SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygments
  2. import pygments.lexers
  3. import pygments.formatters
  4.  
  5. from re import sub
  6.  
  7. import jinja2
  8. from jinja2 import Template, Environment
  9.  
  10. tmpl_="""
  11. <!DOCTYPE html>
  12. <html lang="en">
  13.   <head>
  14.     <meta charset="utf-8">
  15.     <title>Test</title>
  16.     <script>
  17.     </script>
  18.   </head>
  19.   <body>
  20.     {{ main }}
  21.   </body>
  22. </html>
  23. """
  24.  
  25. env = jinja2.Environment()
  26. template = env.from_string(tmpl_)
  27.  
  28. lexer = pygments.lexers.get_lexer_by_name ('text')
  29. lexer.stripnl = False
  30. formatter = pygments.formatters.HtmlFormatter (linenos=True, anchorlinenos=True)
  31.  
  32. code="""
  33. abcd efgh ijk
  34. addas ada aclksadjfl
  35. afdkajfds sadjf asf
  36. dsfjlkdsajf kadjfa
  37. """
  38.  
  39. source=""
  40. version=""
  41. path=""
  42.  
  43.  
  44. result = pygments.highlight (code, lexer, formatter)
  45. result = sub ('href="#-(\d+)', 'name="L\\1" id="L\\1" href="' + version + source + path + '#L\\1', result)
  46.  
  47. # This does not work. Dunno why.
  48. # template.render(main=result)
  49. template.stream(main=result).dump('index.html')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top