shortimgs.py 708 B

1234567891011121314151617181920212223242526
  1. from xml.etree.ElementTree import Element
  2. import markdown
  3. from markdown.inlinepatterns import LinkInlineProcessor
  4. IMAGES_RE = r"\!\((<.*?>|([^\)]*))\)"
  5. class ShortImagesExtension(markdown.Extension):
  6. def extendMarkdown(self, md):
  7. md.registerExtension(self)
  8. md.inlinePatterns.register(
  9. ShortImagePattern(IMAGES_RE, md), "misago_short_images", 200
  10. )
  11. class ShortImagePattern(LinkInlineProcessor):
  12. def handleMatch(self, m, _):
  13. img_src = m.groups()[1].strip()
  14. if not img_src:
  15. return None, None, None
  16. img = Element("img")
  17. img.set("src", img_src)
  18. img.set("alt", img_src)
  19. return img, m.start(0), m.end(0)