parser.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import markdown
  2. import bleach
  3. from bs4 import BeautifulSoup
  4. from django.utils import six
  5. from htmlmin.minify import html_minify
  6. from .bbcode import blocks, inline
  7. from .md.shortimgs import ShortImagesExtension
  8. from .mentions import add_mentions
  9. from .pipeline import pipeline
  10. __all__ = ['parse']
  11. def parse(text, request, poster, allow_mentions=True, allow_links=True,
  12. allow_images=True, allow_blocks=True, minify=True):
  13. """
  14. Message parser
  15. Utility for flavours to call
  16. Breaks text into paragraphs, supports code, spoiler and quote blocks,
  17. headers, lists, images, spoilers, text styles
  18. Returns dict object
  19. """
  20. md = md_factory(
  21. allow_links=allow_links,
  22. allow_images=allow_images,
  23. allow_blocks=allow_blocks,
  24. )
  25. parsing_result = {
  26. 'original_text': text,
  27. 'parsed_text': '',
  28. 'markdown': md,
  29. 'mentions': [],
  30. 'images': [],
  31. 'outgoing_links': [],
  32. 'inside_links': []
  33. }
  34. # Parse text
  35. parsed_text = md.convert(text)
  36. # Clean and store parsed text
  37. parsing_result['parsed_text'] = parsed_text.strip()
  38. if allow_links:
  39. linkify_paragraphs(parsing_result)
  40. parsing_result = pipeline.process_result(parsing_result)
  41. if allow_mentions:
  42. add_mentions(request, parsing_result)
  43. if allow_links or allow_images:
  44. clean_links(request, parsing_result)
  45. if minify:
  46. minify_result(parsing_result)
  47. return parsing_result
  48. def md_factory(allow_links=True, allow_images=True, allow_blocks=True):
  49. """
  50. Create and configure markdown object
  51. """
  52. md = markdown.Markdown(safe_mode='escape', extensions=['nl2br'])
  53. # Remove references
  54. del md.preprocessors['reference']
  55. del md.inlinePatterns['reference']
  56. del md.inlinePatterns['image_reference']
  57. del md.inlinePatterns['short_reference']
  58. # Add [b], [i], [u]
  59. md.inlinePatterns.add('bb_b', inline.bold, '<strong')
  60. md.inlinePatterns.add('bb_i', inline.italics, '<emphasis')
  61. md.inlinePatterns.add('bb_u', inline.underline, '<emphasis2')
  62. if allow_links:
  63. # Add [url]
  64. pass
  65. else:
  66. # Remove links
  67. del md.inlinePatterns['link']
  68. del md.inlinePatterns['autolink']
  69. del md.inlinePatterns['automail']
  70. if allow_images:
  71. # Add [img]
  72. short_images_md = ShortImagesExtension()
  73. short_images_md.extendMarkdown(md)
  74. else:
  75. # Remove images
  76. del md.inlinePatterns['image_link']
  77. if allow_blocks:
  78. # Add [hr] [quote], [spoiler], [list] and [code] blocks
  79. md.parser.blockprocessors.add('bb_hr', blocks.BBCodeHRProcessor(md.parser), '>hr')
  80. else:
  81. # Remove blocks
  82. del md.parser.blockprocessors['hashheader']
  83. del md.parser.blockprocessors['setextheader']
  84. del md.parser.blockprocessors['code']
  85. del md.parser.blockprocessors['quote']
  86. del md.parser.blockprocessors['hr']
  87. del md.parser.blockprocessors['olist']
  88. del md.parser.blockprocessors['ulist']
  89. return pipeline.extend_markdown(md)
  90. def linkify_paragraphs(result):
  91. result['parsed_text'] = bleach.linkify(result['parsed_text'], skip_pre=True, parse_email=True)
  92. def clean_links(request, result):
  93. site_address = '%s://%s' % (request.scheme, request.get_host())
  94. soup = BeautifulSoup(result['parsed_text'], 'html5lib')
  95. for link in soup.find_all('a'):
  96. if link['href'].lower().startswith(site_address):
  97. result['inside_links'].append(link['href'])
  98. if link['href'].lower() == site_address:
  99. link['href'] = '/'
  100. else:
  101. link['href'] = link['href'][len(site_address):]
  102. else:
  103. result['outgoing_links'].append(link['href'])
  104. if link.string.startswith('http://'):
  105. link.string.replace_with(link.string[7:].strip())
  106. if link.string.startswith('https://'):
  107. link.string.replace_with(link.string[8:].strip())
  108. for img in soup.find_all('img'):
  109. result['images'].append(img['src'])
  110. if img['src'].lower().startswith(site_address):
  111. if img['src'].lower() == site_address:
  112. img['src'] = '/'
  113. else:
  114. img['src'] = img['src'][len(site_address):]
  115. if img['alt'].startswith('http://'):
  116. img['alt'] = img['alt'][7:].strip()
  117. if img['alt'].startswith('https://'):
  118. img['alt'] = img['alt'][8:].strip()
  119. # [6:-7] trims <body></body> wrap
  120. result['parsed_text'] = six.text_type(soup.body)[6:-7]
  121. def minify_result(result):
  122. # [25:-14] trims <html><head></head><body> and </body></html>
  123. result['parsed_text'] = html_minify(result['parsed_text'].encode('utf-8'))
  124. result['parsed_text'] = result['parsed_text'][25:-14]