shorthandimgs.py 913 B

12345678910111213141516171819202122232425262728
  1. #-*- coding: utf-8 -*-
  2. import re
  3. import markdown
  4. from markdown.inlinepatterns import LinkPattern
  5. from misago.utils.strings import html_escape
  6. from misago.utils.urls import is_inner, clean_inner
  7. from markdown.util import etree
  8. IMAGES_RE = r'\!(\s?)\((<.*?>|([^\)]*))\)'
  9. class ShorthandImagesExtension(markdown.Extension):
  10. def extendMarkdown(self, md):
  11. md.registerExtension(self)
  12. md.inlinePatterns.add('mi_shorthand_imgs',
  13. ShorthandImagePattern(IMAGES_RE, md),
  14. '_end')
  15. class ShorthandImagePattern(LinkPattern):
  16. def handleMatch(self, m):
  17. img_src = m.groups()[2].strip()
  18. if is_inner(img_src):
  19. img_src = clean_inner(img_src)
  20. if img_src:
  21. el = etree.Element("img")
  22. el.set('alt', img_src)
  23. el.set('src', img_src)
  24. return el