forked from rennat/python-markdown-oembed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinlinepatterns.py
42 lines (31 loc) · 1.18 KB
/
inlinepatterns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
import logging
import re
from markdown.inlinepatterns import Pattern
import oembed
LOG = logging.getLogger(__name__)
OEMBED_LINK_RE = r'\!\[([^\]]*)\]\((https?://[^\)]*)' \
r'(?<!png)(?<!jpg)(?<!jpeg)(?<!gif)\)'
SRC_SCHEME_RE = re.compile(r'src="https?:')
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
# make src urls protocol-relative
html = SRC_SCHEME_RE.sub('src="', html)
placeholder = self.markdown.htmlStash.store(html, safe=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
except:
return u"[Couldn't fetch content for {0} - Is the URL correct?]".format(url)
else:
return response['html']