/diamandas/myghtyboard/templatetags/fbc.py

http://diamanda.googlecode.com/ · Python · 110 lines · 79 code · 20 blank · 11 comment · 14 complexity · 03b1a55b2655127ed57d856a37bbdeb4 MD5 · raw file

  1. #!/usr/bin/python
  2. # Diamanda Application Set
  3. # myghtyboard forum
  4. from re import findall
  5. import base64
  6. from pygments import highlight
  7. from pygments.lexers import get_lexer_by_name,HtmlLexer
  8. from pygments.formatters import HtmlFormatter
  9. from django import template
  10. from django.conf import settings
  11. from diamandas.myghtyboard.utils import *
  12. register = template.Library()
  13. def fbc(value):
  14. """
  15. Parse emotes, BBcode and format [code] blocks
  16. """
  17. tags = findall( r'(?xs)\[code\](.*?)\[/code\]''', value)
  18. for i in tags:
  19. j = base64.b64encode(i.encode('utf-8'))
  20. high = '[code]%s[/code]' % j
  21. value = value.replace('[code]%s[/code]' % i, high)
  22. tags = findall( r'(?xs)\[python\](.*?)\[/python\]''', value)
  23. for i in tags:
  24. j = base64.b64encode(i.encode('utf-8'))
  25. high = '[python]%s[/python]' % j
  26. value = value.replace('[python]%s[/python]' % i, high)
  27. tags = findall( r'(?xs)\[php\](.*?)\[/php\]''', value)
  28. for i in tags:
  29. j = base64.b64encode(i.encode('utf-8'))
  30. high = '[php]%s[/php]' % j
  31. value = value.replace('[php]%s[/php]' % i, high)
  32. stripper = Stripper()
  33. value = stripper.strip(value)
  34. value = value.replace('\n', '<br />')
  35. value = value.replace("'", '&#39;').replace('"', '&#34;')
  36. tags = findall( r'(?xs)\[url=(.*?)\](.*?)\[/url]''', value)
  37. for i in tags:
  38. value = value.replace('[url=%s]%s[/url]' % (i[0], i[1]), '<a href="%s">%s</a>' % (i[0].replace('"', ''), i[1]))
  39. value = value.replace('[b]', '<b>')
  40. value = value.replace('[/b]', '</b>')
  41. value = value.replace('[i]', '<i>')
  42. value = value.replace('[/i]', '</i>')
  43. value = value.replace('[u]', '<u>')
  44. value = value.replace('[/u]', '</u>')
  45. value = value.replace('[quote]', '<blockquote>')
  46. value = value.replace('[/quote]', '</blockquote>')
  47. value = value.replace('[url]', '')
  48. value = value.replace('[/url]', '')
  49. tags = findall( r'(?xs)\[img\](.*?)\[/img]''', value)
  50. for i in tags:
  51. if not len(i) < 3 and i[0:4] == 'http':
  52. value = value.replace('[img]%s[/img]' % i, '<img src="%s" alt="" />' % i)
  53. pygments_formatter = HtmlFormatter()
  54. lexer = get_lexer_by_name('html')
  55. tags = findall( r'(?xs)\[code\](.*?)\[/code\]''', value)
  56. for i in tags:
  57. try:
  58. j = base64.b64decode(i).replace('<br />', '\n')
  59. except:
  60. j = ''
  61. high = '<div class="box" style="width:90%%;margin-left:auto;margin-right:auto;">%s</div>' % (highlight(j, lexer, pygments_formatter))
  62. value = value.replace('[code]%s[/code]' % i, high)
  63. lexer = get_lexer_by_name('python')
  64. tags = findall( r'(?xs)\[python\](.*?)\[/python\]''', value)
  65. for i in tags:
  66. try:
  67. j = base64.b64decode(i).replace('<br />', '\n')
  68. except:
  69. j = ''
  70. high = '<div class="box" style="width:90%%;margin-left:auto;margin-right:auto;">%s</div>' % (highlight(j, lexer, pygments_formatter))
  71. value = value.replace('[python]%s[/python]' % i, high)
  72. lexer = get_lexer_by_name('php')
  73. HtmlFormatter().get_style_defs('.highlight_php')
  74. tags = findall( r'(?xs)\[php\](.*?)\[/php\]''', value)
  75. for i in tags:
  76. try:
  77. j = base64.b64decode(i).replace('<br />', '\n')
  78. except:
  79. j = ''
  80. if j.find('<?php') < 1:
  81. j = '<?php\n%s' % j
  82. high = '<div class="box" style="width:90%%;margin-left:auto;margin-right:auto;">%s</div>' % (highlight(j, lexer, pygments_formatter))
  83. value = value.replace('[php]%s[/php]' % i, high)
  84. #value = value.replace(' :( ', '<img src="/site_media/layout/markitup/sets/bbcode/images/emoticon-unhappy.png" alt="" />')
  85. #value = value.replace(' :o ', '<img src="/site_media/layout/markitup/sets/bbcode/images/emoticon-surprised.png" alt="" />')
  86. #value = value.replace(' :p ', '<img src="/site_media/layout/markitup/sets/bbcode/images/emoticon-tongue.png" alt="" />')
  87. #value = value.replace(' ;) ', '<img src="/site_media/layout/markitup/sets/bbcode/images/emoticon-wink.png" alt="" />')
  88. #value = value.replace(' :D ', '<img src="/site_media/layout/markitup/sets/bbcode/images/emoticon-smile.png" alt="" />')
  89. return value
  90. register.filter('fbc', fbc)