figureAltCaption.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """
  2. Generates a Caption for Figures for each Image which stands alone in a paragraph,
  3. similar to pandoc#s handling of images/figures
  4. --------------------------------------------
  5. Licensed under the GPL 2 (see LICENSE.md)
  6. Copyright 2015 - Jan Dittrich by
  7. building upon the markdown-figures Plugin by
  8. Copyright 2013 - [Helder Correia](http://heldercorreia.com) (GPL2)
  9. --------------------------------------------
  10. Examples:
  11. Bla bla bla
  12. ![this is the caption](http://lorempixel.com/400/200/)
  13. Next paragraph starts here
  14. would generate a figure like this:
  15. <figure>
  16. <img src="http://lorempixel.com/400/200/">
  17. <figcaption>this is the caption</figcaption>
  18. </figure>
  19. """
  20. from __future__ import unicode_literals
  21. from markdown import Extension
  22. from markdown.inlinepatterns import IMAGE_LINK_RE, IMAGE_REFERENCE_RE, NOBRACKET, BRK
  23. from markdown.blockprocessors import BlockProcessor
  24. from markdown.util import etree
  25. import re #regex
  26. import logging
  27. logger = logging.getLogger('MARKDOWN')
  28. FIGURES = [u'^\s*'+IMAGE_LINK_RE+u'\s*$', u'^\s*'+IMAGE_REFERENCE_RE+u'\s*$'] #is: linestart, any whitespace (even none), image, any whitespace (even none), line ends.
  29. # This is the core part of the extension
  30. class FigureCaptionProcessor(BlockProcessor):
  31. FIGURES_RE = re.compile('|'.join(f for f in FIGURES))
  32. def test(self, parent, block): # is the block relevant
  33. # Wenn es ein Bild gibt und das Bild alleine im paragraph ist, und das Bild nicht schon einen figure parent hat, returne True
  34. isImage = bool(self.FIGURES_RE.search(block))
  35. isOnlyOneLine = (len(block.splitlines())== 1)
  36. isInFigure = (parent.tag == 'figure')
  37. # print(block, isImage, isOnlyOneLine, isInFigure, "T,T,F")
  38. if (isImage and isOnlyOneLine and not isInFigure):
  39. return True
  40. else:
  41. return False
  42. def run(self, parent, blocks): # how to process the block?
  43. raw_block = blocks.pop(0)
  44. captionText = self.FIGURES_RE.search(raw_block).group(1)
  45. # create figure
  46. figure = etree.SubElement(parent, 'figure')
  47. # render image in figure
  48. figure.text = raw_block
  49. # create caption
  50. figcaptionElem = etree.SubElement(figure,'figcaption')
  51. figcaptionElem.text = captionText #no clue why the text itself turns out as html again and not raw. Anyhow, it suits me, the blockparsers annoyingly wrapped everything into <p>.
  52. class FigureCaptionExtension(Extension):
  53. def extendMarkdown(self, md, md_globals):
  54. """ Add an instance of FigcaptionProcessor to BlockParser. """
  55. md.parser.blockprocessors.add('figureAltcaption',
  56. FigureCaptionProcessor(md.parser),
  57. '<ulist')
  58. def makeExtension(configs={}):
  59. return FigureCaptionExtension(configs=configs)