python-markdown-oembed/mdx_oembed/inlinepatterns.py
2012-11-13 17:28:04 -06:00

27 lines
756 B
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):
url = match.group(3).strip()
try:
response = self.consumer.embed(url)
except oembed.OEmbedNoEndpoint:
return None
placeholder = self.markdown.htmlStash.store(response['html'])
return placeholder