Guest User

Untitled

a guest
Jun 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import re
  2. import pygments
  3. from django import template
  4. from django.conf import settings
  5. from pygments import lexers
  6. from pygments import formatters
  7. from smileys import parse_smileys
  8.  
  9. register = template.Library()
  10. regex = re.compile(r'<code(.*?)>(.*?)</code>', re.DOTALL)
  11.  
  12. @register.filter(name='pygmentize')
  13. def pygmentize(value):
  14. last_end = 0
  15. to_return = ''
  16. found = 0
  17. for match_obj in regex.finditer(value):
  18. code_class = match_obj.group(1)
  19. code_string = match_obj.group(2)
  20. if code_class.find('class'):
  21. language = re.split(r'"|\'', code_class)[1]
  22. lexer = lexers.get_lexer_by_name(language)
  23. else:
  24. try:
  25. lexer = lexers.guess_lexer(str(code))
  26. except ValueError:
  27. lexer = lexers.PythonLexer()
  28. pygmented_string = pygments.highlight(code_string, lexer, formatters.HtmlFormatter())
  29. to_return = to_return + value[last_end:match_obj.start(0)] + pygmented_string
  30. last_end = match_obj.end(2)
  31. found = found + 1
  32. to_return = to_return + value[last_end:]
  33. return to_return
  34.  
  35. @register.filter(name='smileys')
  36. def smileys(str):
  37. return parse_smileys(str, settings.SMILEY_URL, True)
Add Comment
Please, Sign In to add comment