factory.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import re
  2. import markdown
  3. from HTMLParser import HTMLParser
  4. from django.conf import settings
  5. from django.utils.importlib import import_module
  6. from django.utils.translation import ugettext_lazy as _
  7. from misago.utils import get_random_string
  8. class ClearHTMLParser(HTMLParser):
  9. def __init__(self):
  10. HTMLParser.__init__(self)
  11. self.clean_text = ''
  12. self.lookback = []
  13. def handle_starttag(self, tag, attrs):
  14. self.lookback.append(tag)
  15. def handle_endtag(self, tag):
  16. try:
  17. if self.lookback[-1] == tag:
  18. self.lookback.pop()
  19. except IndexError:
  20. pass
  21. def handle_data(self, data):
  22. # String does not repeat itself
  23. if self.clean_text[-len(data):] != data:
  24. # String is not "QUOTE"
  25. try:
  26. if not (data == 'Quote' and self.lookback[-1] == 'h3' and self.lookback[-2] == 'blockquote'):
  27. self.clean_text += ' %s' % data
  28. except IndexError:
  29. self.clean_text += ' %s' % data
  30. def clear_markdown(text):
  31. parser = ClearHTMLParser()
  32. parser.feed(text)
  33. return parser.clean_text
  34. def remove_unsupported(md):
  35. # References are evil, we dont support them
  36. del md.preprocessors['reference']
  37. del md.inlinePatterns['reference']
  38. del md.inlinePatterns['image_reference']
  39. del md.inlinePatterns['short_reference']
  40. def signature_markdown(acl, text):
  41. md = markdown.Markdown(
  42. safe_mode='escape',
  43. output_format=settings.OUTPUT_FORMAT,
  44. extensions=['nl2br'])
  45. remove_unsupported(md)
  46. if not acl.usercp.allow_signature_links():
  47. del md.inlinePatterns['link']
  48. del md.inlinePatterns['autolink']
  49. if not acl.usercp.allow_signature_images():
  50. del md.inlinePatterns['image_link']
  51. del md.parser.blockprocessors['hashheader']
  52. del md.parser.blockprocessors['setextheader']
  53. del md.parser.blockprocessors['code']
  54. del md.parser.blockprocessors['quote']
  55. del md.parser.blockprocessors['hr']
  56. del md.parser.blockprocessors['olist']
  57. del md.parser.blockprocessors['ulist']
  58. return md.convert(text)
  59. def post_markdown(request, text):
  60. md = markdown.Markdown(
  61. safe_mode='escape',
  62. output_format=settings.OUTPUT_FORMAT,
  63. extensions=['nl2br', 'fenced_code'])
  64. remove_unsupported(md)
  65. md.mi_token = get_random_string(16)
  66. for extension in settings.MARKDOWN_EXTENSIONS:
  67. module = '.'.join(extension.split('.')[:-1])
  68. extension = extension.split('.')[-1]
  69. module = import_module(module)
  70. attr = getattr(module, extension)
  71. ext = attr()
  72. ext.extendMarkdown(md)
  73. text = md.convert(text)
  74. return tidy_markdown(md, text)
  75. def tidy_markdown(md, text):
  76. text = text.replace('<p><h3><quotetitle>', '<h3><quotetitle>')
  77. text = text.replace('</quotetitle></h3></p>', '</quotetitle></h3>')
  78. text = text.replace('</quotetitle></h3><br>\r\n', '</quotetitle></h3>\r\n<p>')
  79. text = text.replace('\r\n<p></p>', '')
  80. return md, text
  81. def finalize_markdown(text):
  82. def trans_quotetitle(match):
  83. return _("Posted by %(user)s") % {'user': match.group('content')}
  84. text = re.sub(r'<quotetitle>(?P<content>.+)</quotetitle>', trans_quotetitle, text)
  85. text = re.sub(r'<quotesingletitle>', _("Quote"), text)
  86. return text