parser.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import bleach
  2. import markdown
  3. from bs4 import BeautifulSoup
  4. from django.http import Http404
  5. from django.urls import resolve
  6. from htmlmin.minify import html_minify
  7. from markdown.extensions.fenced_code import FencedCodeExtension
  8. from ..conf import settings
  9. from .bbcode.code import CodeBlockExtension
  10. from .bbcode.hr import BBCodeHRProcessor
  11. from .bbcode.inline import bold, image, italics, underline, url
  12. from .bbcode.quote import QuoteExtension
  13. from .bbcode.spoiler import SpoilerExtension
  14. from .md.shortimgs import ShortImagesExtension
  15. from .md.strikethrough import StrikethroughExtension
  16. from .mentions import add_mentions
  17. from .pipeline import pipeline
  18. MISAGO_ATTACHMENT_VIEWS = ("misago:attachment", "misago:attachment-thumbnail")
  19. def parse(
  20. text,
  21. request,
  22. poster,
  23. allow_mentions=True,
  24. allow_links=True,
  25. allow_images=True,
  26. allow_blocks=True,
  27. force_shva=False,
  28. minify=True,
  29. ):
  30. """
  31. Message parser
  32. Utility for flavours to call
  33. Breaks text into paragraphs, supports code, spoiler and quote blocks,
  34. headers, lists, images, spoilers, text styles
  35. Returns dict object
  36. """
  37. md = md_factory(
  38. allow_links=allow_links, allow_images=allow_images, allow_blocks=allow_blocks
  39. )
  40. parsing_result = {
  41. "original_text": text,
  42. "parsed_text": "",
  43. "markdown": md,
  44. "mentions": [],
  45. "images": [],
  46. "internal_links": [],
  47. "outgoing_links": [],
  48. }
  49. # Parse text
  50. parsed_text = md.convert(text)
  51. # Clean and store parsed text
  52. parsing_result["parsed_text"] = parsed_text.strip()
  53. if allow_links:
  54. linkify_paragraphs(parsing_result)
  55. parsing_result = pipeline.process_result(parsing_result)
  56. if allow_mentions:
  57. add_mentions(request, parsing_result)
  58. if allow_links or allow_images:
  59. clean_links(request, parsing_result, force_shva)
  60. if minify:
  61. minify_result(parsing_result)
  62. return parsing_result
  63. def md_factory(allow_links=True, allow_images=True, allow_blocks=True):
  64. """creates and configures markdown object"""
  65. md = markdown.Markdown(extensions=["markdown.extensions.nl2br"])
  66. # Remove HTML allowances
  67. del md.preprocessors["html_block"]
  68. del md.inlinePatterns["html"]
  69. # Remove references
  70. del md.preprocessors["reference"]
  71. del md.inlinePatterns["reference"]
  72. del md.inlinePatterns["image_reference"]
  73. del md.inlinePatterns["short_reference"]
  74. # Add [b], [i], [u]
  75. md.inlinePatterns.add("bb_b", bold, "<strong")
  76. md.inlinePatterns.add("bb_i", italics, "<emphasis")
  77. md.inlinePatterns.add("bb_u", underline, "<emphasis2")
  78. # Add ~~deleted~~
  79. strikethrough_md = StrikethroughExtension()
  80. strikethrough_md.extendMarkdown(md)
  81. if allow_links:
  82. # Add [url]
  83. md.inlinePatterns.add("bb_url", url(md), "<link")
  84. else:
  85. # Remove links
  86. del md.inlinePatterns["link"]
  87. del md.inlinePatterns["autolink"]
  88. del md.inlinePatterns["automail"]
  89. if allow_images:
  90. # Add [img]
  91. md.inlinePatterns.add("bb_img", image(md), "<image_link")
  92. short_images_md = ShortImagesExtension()
  93. short_images_md.extendMarkdown(md)
  94. else:
  95. # Remove images
  96. del md.inlinePatterns["image_link"]
  97. if allow_blocks:
  98. # Add [hr] and [quote] blocks
  99. md.parser.blockprocessors.add("bb_hr", BBCodeHRProcessor(md.parser), ">hr")
  100. fenced_code = FencedCodeExtension()
  101. fenced_code.extendMarkdown(md, None)
  102. code_bbcode = CodeBlockExtension()
  103. code_bbcode.extendMarkdown(md)
  104. quote_bbcode = QuoteExtension()
  105. quote_bbcode.extendMarkdown(md)
  106. spoiler_bbcode = SpoilerExtension()
  107. spoiler_bbcode.extendMarkdown(md)
  108. else:
  109. # Remove blocks
  110. del md.parser.blockprocessors["hashheader"]
  111. del md.parser.blockprocessors["setextheader"]
  112. del md.parser.blockprocessors["code"]
  113. del md.parser.blockprocessors["quote"]
  114. del md.parser.blockprocessors["hr"]
  115. del md.parser.blockprocessors["olist"]
  116. del md.parser.blockprocessors["ulist"]
  117. return pipeline.extend_markdown(md)
  118. def linkify_paragraphs(result):
  119. result["parsed_text"] = bleach.linkify(
  120. result["parsed_text"],
  121. callbacks=settings.MISAGO_BLEACH_CALLBACKS,
  122. skip_tags=["a", "code", "pre"],
  123. parse_email=True,
  124. )
  125. def clean_links(request, result, force_shva=False):
  126. host = request.get_host()
  127. soup = BeautifulSoup(result["parsed_text"], "html5lib")
  128. for link in soup.find_all("a"):
  129. if is_internal_link(link["href"], host):
  130. link["href"] = clean_internal_link(link["href"], host)
  131. result["internal_links"].append(link["href"])
  132. link["href"] = clean_attachment_link(link["href"], force_shva)
  133. else:
  134. result["outgoing_links"].append(clean_link_prefix(link["href"]))
  135. link["href"] = assert_link_prefix(link["href"])
  136. link["rel"] = "nofollow noopener"
  137. if link.string:
  138. link.string = clean_link_prefix(link.string)
  139. for img in soup.find_all("img"):
  140. img["alt"] = clean_link_prefix(img["alt"])
  141. if is_internal_link(img["src"], host):
  142. img["src"] = clean_internal_link(img["src"], host)
  143. result["images"].append(img["src"])
  144. img["src"] = clean_attachment_link(img["src"], force_shva)
  145. else:
  146. result["images"].append(clean_link_prefix(img["src"]))
  147. img["src"] = assert_link_prefix(img["src"])
  148. # [6:-7] trims <body></body> wrap
  149. result["parsed_text"] = str(soup.body)[6:-7]
  150. def is_internal_link(link, host):
  151. if link.startswith("/") and not link.startswith("//"):
  152. return True
  153. link = clean_link_prefix(link).lstrip("www.").lower()
  154. return link.lower().startswith(host.lstrip("www."))
  155. def clean_link_prefix(link):
  156. if link.lower().startswith("https:"):
  157. link = link[6:]
  158. if link.lower().startswith("http:"):
  159. link = link[5:]
  160. if link.startswith("//"):
  161. link = link[2:]
  162. return link
  163. def assert_link_prefix(link):
  164. if link.lower().startswith("https:"):
  165. return link
  166. if link.lower().startswith("http:"):
  167. return link
  168. if link.startswith("//"):
  169. return "http:%s" % link
  170. return "http://%s" % link
  171. def clean_internal_link(link, host):
  172. link = clean_link_prefix(link)
  173. if link.lower().startswith("www."):
  174. link = link[4:]
  175. if host.lower().startswith("www."):
  176. host = host[4:]
  177. if link.lower().startswith(host):
  178. link = link[len(host) :]
  179. return link or "/"
  180. def clean_attachment_link(link, force_shva=False):
  181. try:
  182. resolution = resolve(link)
  183. if not resolution.namespaces:
  184. return link
  185. url_name = ":".join(resolution.namespaces + [resolution.url_name])
  186. except (Http404, ValueError):
  187. return link
  188. if url_name in MISAGO_ATTACHMENT_VIEWS:
  189. if force_shva:
  190. link = "%s?shva=1" % link
  191. elif link.endswith("?shva=1"):
  192. link = link[:-7]
  193. return link
  194. def minify_result(result):
  195. result["parsed_text"] = html_minify(result["parsed_text"])
  196. result["parsed_text"] = strip_html_head_body(result["parsed_text"])
  197. def strip_html_head_body(parsed_text):
  198. # [25:-14] trims <html><head></head><body> and </body></html>
  199. return parsed_text[25:-14]