factory.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 self.lookback[-1] in ('strong', 'em'):
  27. self.clean_text += data
  28. elif not (data == 'Quote' and self.lookback[-1] == 'h3' and self.lookback[-2] == 'blockquote'):
  29. self.clean_text += ' %s' % data
  30. except IndexError:
  31. self.clean_text += ' %s' % data
  32. def clear_markdown(text):
  33. parser = ClearHTMLParser()
  34. parser.feed(text)
  35. return parser.clean_text
  36. def remove_unsupported(md):
  37. # References are evil, we dont support them
  38. del md.preprocessors['reference']
  39. del md.inlinePatterns['reference']
  40. del md.inlinePatterns['image_reference']
  41. del md.inlinePatterns['short_reference']
  42. def signature_markdown(acl, text):
  43. md = markdown.Markdown(
  44. safe_mode='escape',
  45. output_format=settings.OUTPUT_FORMAT,
  46. extensions=['nl2br'])
  47. remove_unsupported(md)
  48. if not acl.usercp.allow_signature_links():
  49. del md.inlinePatterns['link']
  50. del md.inlinePatterns['autolink']
  51. if not acl.usercp.allow_signature_images():
  52. del md.inlinePatterns['image_link']
  53. del md.parser.blockprocessors['hashheader']
  54. del md.parser.blockprocessors['setextheader']
  55. del md.parser.blockprocessors['code']
  56. del md.parser.blockprocessors['quote']
  57. del md.parser.blockprocessors['hr']
  58. del md.parser.blockprocessors['olist']
  59. del md.parser.blockprocessors['ulist']
  60. return md.convert(text)
  61. def post_markdown(request, text):
  62. md = markdown.Markdown(
  63. safe_mode='escape',
  64. output_format=settings.OUTPUT_FORMAT,
  65. extensions=['nl2br', 'fenced_code'])
  66. remove_unsupported(md)
  67. md.mi_token = get_random_string(16)
  68. for extension in settings.MARKDOWN_EXTENSIONS:
  69. module = '.'.join(extension.split('.')[:-1])
  70. extension = extension.split('.')[-1]
  71. module = import_module(module)
  72. attr = getattr(module, extension)
  73. ext = attr()
  74. ext.extendMarkdown(md)
  75. text = md.convert(text)
  76. return tidy_markdown(md, text)
  77. def tidy_markdown(md, text):
  78. text = text.replace('<p><h3><quotetitle>', '<h3><quotetitle>')
  79. text = text.replace('</quotetitle></h3></p>', '</quotetitle></h3>')
  80. text = text.replace('</quotetitle></h3><br>\r\n', '</quotetitle></h3>\r\n<p>')
  81. text = text.replace('\r\n<p></p>', '')
  82. return md, text
  83. def finalize_markdown(text):
  84. def trans_quotetitle(match):
  85. return _("Posted by %(user)s") % {'user': match.group('content')}
  86. text = re.sub(r'<quotetitle>(?P<content>.+)</quotetitle>', trans_quotetitle, text)
  87. text = re.sub(r'<quotesingletitle>', _("Quote"), text)
  88. return text