python-markdown-oembed/mdx_oembed/inlinepatterns.py
Sami Turcotte 639d4a6dec Change div wrapper to a figure wrapper
The reasoning is that the translation of Markdown into HTML should be as
concise as possible. The semantics of the figure element seem to fit the
embedded nature of OEmbed content moreso than the div element which is
quite generic.
2015-04-20 00:42:50 -04:00

36 lines
1 KiB
Python

# -*- coding: utf-8 -*-
import logging
from markdown.inlinepatterns import Pattern
import oembed
LOG = logging.getLogger(__name__)
OEMBED_LINK_RE = r'\!\[([^\]]*)\]\(((?:https?:)?//[^\)]*)' \
r'(?<!png)(?<!jpg)(?<!jpeg)(?<!gif)\)'
class OEmbedLinkPattern(Pattern):
def __init__(self, pattern, markdown_instance=None, oembed_consumer=None):
Pattern.__init__(self, pattern, markdown_instance)
self.consumer = oembed_consumer
def handleMatch(self, match):
html = self.get_oembed_html_for_match(match)
if html is None:
return None
else:
html = "<figure class=\"oembed\">%s</figure>" % html
placeholder = self.markdown.htmlStash.store(html, True)
return placeholder
def get_oembed_html_for_match(self, match):
url = match.group(3).strip()
try:
response = self.consumer.embed(url)
except oembed.OEmbedNoEndpoint:
return None
else:
return response['html']