A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

47 lines
1.2 KiB

  1. """
  2. :synopsis: Helper functions for use inside the project's Jinja templates.
  3. """
  4. import re
  5. from flask import Markup
  6. ASSET_HTML_TEMPLATES = {
  7. 'css': "<link rel='stylesheet' type='text/css' href='/css/%s'>",
  8. 'js': "<script src='/js/%s'></script>"
  9. }
  10. def tag(filename):
  11. """
  12. Generate an HTML tag for a CSS/JS asset, based on its file extension.
  13. :param filename: The filename of the asset to create a tag for.
  14. :type filename: str
  15. :return: A string containing a `<source>` tag for JS files, and a `<link>`
  16. for CSS files.
  17. :rtype: str
  18. """
  19. file_ext = filename.split(".")[-1]
  20. return Markup(ASSET_HTML_TEMPLATES[file_ext] % filename)
  21. def syntax_highlight(msg):
  22. """
  23. Inserts HTML `<span>` elements into a string, for symbol/word styling.
  24. Args:
  25. msg : (str) A message.
  26. """
  27. msg.replace("<", "&;lt")
  28. msg.replace(">", "&;gt")
  29. font_size = 16.0 / len(msg)
  30. msg = re.sub('([!()"%])', '<span class="dark">\\1</span>', msg)
  31. msg = re.sub('([:.;,])', '<span class="red">\\1</span>', msg)
  32. msg = msg.replace("404", '<span class="red">404</span>')
  33. return "<span class='light' style='font-size: %fem'>%s</span>" % (
  34. font_size, msg)