|
@@ -1,9 +1,17 @@
|
|
|
+from importlib import import_module
|
|
|
+
|
|
|
+from bs4 import BeautifulSoup
|
|
|
+from django.conf import settings
|
|
|
import markdown
|
|
|
+
|
|
|
from misago.markup.bbcode import inline, blocks
|
|
|
|
|
|
|
|
|
+__all__ = ['parse_text']
|
|
|
+
|
|
|
+
|
|
|
def parse_text(text, author=None, allow_mentions=True, allow_links=True,
|
|
|
- allow_images=True, allow_blocks=True):
|
|
|
+ allow_images=True, allow_blocks=True):
|
|
|
"""
|
|
|
Message parser
|
|
|
|
|
@@ -25,17 +33,18 @@ def parse_text(text, author=None, allow_mentions=True, allow_links=True,
|
|
|
}
|
|
|
|
|
|
# Parse text
|
|
|
- parsed_text = md.convert(text).strip()
|
|
|
+ parsed_text = md.convert(text)
|
|
|
|
|
|
- # Store parsed text on object and return it
|
|
|
- parsing_result['parsed_text'] = parsed_text
|
|
|
+ # Clean and store parsed text
|
|
|
+ parsing_result['parsed_text'] = parsed_text.strip()
|
|
|
+ parsing_result = pipeline.process_result(parsing_result)
|
|
|
return parsing_result
|
|
|
|
|
|
|
|
|
def md_factory(author=None, allow_mentions=True, allow_links=True,
|
|
|
allow_images=True, allow_blocks=True):
|
|
|
"""
|
|
|
- Create and confifure markdown object
|
|
|
+ Create and configure markdown object
|
|
|
"""
|
|
|
md = markdown.Markdown(safe_mode='escape',
|
|
|
extensions=['nl2br'])
|
|
@@ -86,4 +95,31 @@ def md_factory(author=None, allow_mentions=True, allow_links=True,
|
|
|
del md.parser.blockprocessors['olist']
|
|
|
del md.parser.blockprocessors['ulist']
|
|
|
|
|
|
- return md
|
|
|
+ return pipeline.extend_markdown(md)
|
|
|
+
|
|
|
+
|
|
|
+class MarkupPipeline(object):
|
|
|
+ """
|
|
|
+ Small framework for extending parser
|
|
|
+ """
|
|
|
+ def extend_markdown(self, md):
|
|
|
+ for extension in settings.MISAGO_MARKUP_EXTENSIONS:
|
|
|
+ module = import_module(extension)
|
|
|
+ if hasattr(module, 'extend_markdown'):
|
|
|
+ hook = getattr(module, 'extend_markdown')
|
|
|
+ hook.extend_markdown(md)
|
|
|
+ return md
|
|
|
+
|
|
|
+ def process_result(self, result):
|
|
|
+ soup = BeautifulSoup(result['parsed_text'])
|
|
|
+ for extension in settings.MISAGO_MARKUP_EXTENSIONS:
|
|
|
+ module = import_module(extension)
|
|
|
+ if hasattr(module, 'clean_parsed'):
|
|
|
+ hook = getattr(module, 'clean_parsed')
|
|
|
+ hook.process_result(result, soup)
|
|
|
+
|
|
|
+ souped_text = unicode(soup.body).strip()[6:-7]
|
|
|
+ result['parsed_text'] = souped_text
|
|
|
+ return result
|
|
|
+
|
|
|
+pipeline = MarkupPipeline()
|