Skip to content

Instantly share code, notes, and snippets.

@glombard
Created May 4, 2016 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glombard/37f4936910f76dcd0d7ef52177be4691 to your computer and use it in GitHub Desktop.
Save glombard/37f4936910f76dcd0d7ef52177be4691 to your computer and use it in GitHub Desktop.
Parse a Markdown doc using mistune
from __future__ import print_function
from collections import namedtuple
from mistune import Markdown
text = """"
# Sample using mistune to parse images from Markdown pages.
* This is a sample image: ![Alt text](/path/to/img.jpg "Optional title")
* and here's another one: ![foo](http://foo/img.png)
"""
ImageInfo = namedtuple('ImageInfo', ['src', 'title', 'text'])
class ImageCaptureRenderer(object):
"""Implement a partial renderer that only unders image(...) and handles all other methods with a no_op."""
def __init__(self, **kwargs):
self.options = kwargs
self.no_op = lambda *args, **kwargs: ''
self.clear()
def clear(self):
self.images = []
def image(self, src, title, text):
self.images.append(ImageInfo(src, title, text))
def __getattr__(self, name):
return self.no_op
image_capture = ImageCaptureRenderer()
md = Markdown(renderer=image_capture)
md.parse(text)
for image in image_capture.images:
print('{}, {}, {}'.format(image.src, image.title, image.text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment