|
@@ -1,4 +1,8 @@
|
|
|
|
+import bleach
|
|
|
|
+from bs4 import BeautifulSoup
|
|
|
|
+from htmlmin.minify import html_minify
|
|
import markdown
|
|
import markdown
|
|
|
|
+
|
|
from misago.markup.bbcode import inline, blocks
|
|
from misago.markup.bbcode import inline, blocks
|
|
from misago.markup.pipeline import pipeline
|
|
from misago.markup.pipeline import pipeline
|
|
|
|
|
|
@@ -6,8 +10,8 @@ from misago.markup.pipeline import pipeline
|
|
__all__ = ['parse']
|
|
__all__ = ['parse']
|
|
|
|
|
|
|
|
|
|
-def parse(text, author=None, allow_mentions=True, allow_links=True,
|
|
|
|
- allow_images=True, allow_blocks=True):
|
|
|
|
|
|
+def parse(text, request, poster, allow_mentions=True, allow_links=True,
|
|
|
|
+ allow_images=True, allow_blocks=True, minify=True):
|
|
"""
|
|
"""
|
|
Message parser
|
|
Message parser
|
|
|
|
|
|
@@ -18,8 +22,7 @@ def parse(text, author=None, allow_mentions=True, allow_links=True,
|
|
|
|
|
|
Returns dict object
|
|
Returns dict object
|
|
"""
|
|
"""
|
|
- md = md_factory(author=author, allow_mentions=allow_mentions,
|
|
|
|
- allow_links=allow_links, allow_images=allow_images,
|
|
|
|
|
|
+ md = md_factory(allow_links=allow_links, allow_images=allow_images,
|
|
allow_blocks=allow_blocks)
|
|
allow_blocks=allow_blocks)
|
|
|
|
|
|
parsing_result = {
|
|
parsing_result = {
|
|
@@ -37,12 +40,35 @@ def parse(text, author=None, allow_mentions=True, allow_links=True,
|
|
|
|
|
|
# Clean and store parsed text
|
|
# Clean and store parsed text
|
|
parsing_result['parsed_text'] = parsed_text.strip()
|
|
parsing_result['parsed_text'] = parsed_text.strip()
|
|
|
|
+
|
|
|
|
+ if allow_links:
|
|
|
|
+ linkify_paragraphs(parsing_result)
|
|
|
|
+
|
|
|
|
+ if allow_links or allow_images:
|
|
|
|
+ make_absolute_links_relative(parsing_result, request)
|
|
|
|
+
|
|
parsing_result = pipeline.process_result(parsing_result)
|
|
parsing_result = pipeline.process_result(parsing_result)
|
|
|
|
+
|
|
|
|
+ if minify:
|
|
|
|
+ minify_result(parsing_result)
|
|
return parsing_result
|
|
return parsing_result
|
|
|
|
|
|
|
|
|
|
-def md_factory(author=None, allow_mentions=True, allow_links=True,
|
|
|
|
- allow_images=True, allow_blocks=True):
|
|
|
|
|
|
+def linkify_paragraphs(result):
|
|
|
|
+ result['parsed_text'] = bleach.linkify(
|
|
|
|
+ result['parsed_text'], skip_pre=True, parse_email=True)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def make_absolute_links_relative(result, request):
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def minify_result(result):
|
|
|
|
+ # [25:-14] trims <html><head></head><body> and </body></html>
|
|
|
|
+ result['parsed_text'] = html_minify(result['parsed_text'])[25:-14]
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def md_factory(allow_links=True, allow_images=True, allow_blocks=True):
|
|
"""
|
|
"""
|
|
Create and configure markdown object
|
|
Create and configure markdown object
|
|
"""
|
|
"""
|
|
@@ -60,10 +86,6 @@ def md_factory(author=None, allow_mentions=True, allow_links=True,
|
|
md.inlinePatterns.add('bb_i', inline.italics, '<emphasis')
|
|
md.inlinePatterns.add('bb_i', inline.italics, '<emphasis')
|
|
md.inlinePatterns.add('bb_u', inline.underline, '<emphasis2')
|
|
md.inlinePatterns.add('bb_u', inline.underline, '<emphasis2')
|
|
|
|
|
|
- if allow_mentions:
|
|
|
|
- # Register mentions
|
|
|
|
- pass
|
|
|
|
-
|
|
|
|
if allow_links:
|
|
if allow_links:
|
|
# Add [url]
|
|
# Add [url]
|
|
pass
|
|
pass
|