shortimgs.py 691 B

1234567891011121314151617181920212223242526
  1. import re
  2. import markdown
  3. from markdown.inlinepatterns import LinkPattern
  4. from markdown.util import etree
  5. IMAGES_RE = r'\!(\s?)\((<.*?>|([^\)]*))\)'
  6. class ShortImagesExtension(markdown.Extension):
  7. def extendMarkdown(self, md):
  8. md.registerExtension(self)
  9. md.inlinePatterns.add('misago_short_images',
  10. ShortImagePattern(IMAGES_RE, md),
  11. '_end')
  12. class ShortImagePattern(LinkPattern):
  13. def handleMatch(self, m):
  14. img_src = m.groups()[2].strip()
  15. if img_src:
  16. img = etree.Element("img")
  17. img.set('src', img_src)
  18. img.set('alt', img_src)
  19. return img