2012-11-13 21:28:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
import logging
|
|
|
|
|
from markdown.inlinepatterns import Pattern
|
2012-11-13 23:28:04 +00:00
|
|
|
import oembed
|
2012-11-13 21:28:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
2012-11-13 23:28:04 +00:00
|
|
|
try:
|
|
|
|
|
response = self.consumer.embed(url)
|
|
|
|
|
except oembed.OEmbedNoEndpoint:
|
|
|
|
|
return None
|
2012-11-13 21:28:10 +00:00
|
|
|
placeholder = self.markdown.htmlStash.store(response['html'])
|
|
|
|
|
return placeholder
|