allow configuring which endpoints to allow

This commit is contained in:
J. Tanner Netterville 2012-11-13 17:28:04 -06:00
parent 685bfd4ba7
commit 2f56c1c11a
4 changed files with 61 additions and 10 deletions

View file

@ -3,4 +3,6 @@ from mdx_oembed.extension import OEmbedExtension
def makeExtension(configs=None):
if isinstance(configs, list):
configs = dict(configs)
return OEmbedExtension(configs=configs)

View file

@ -5,16 +5,28 @@ from mdx_oembed.endpoints import ENDPOINTS
from mdx_oembed.inlinepatterns import OEmbedLinkPattern, OEMBED_LINK_RE
AVAILABLE_ENDPOINTS = ENDPOINTS.keys()
class OEmbedExtension(Extension):
config = {
'allowed_endpoints': [
AVAILABLE_ENDPOINTS,
"A list of oEmbed endpoints to allow. Possible values are "
"{}.".format(', '.join(AVAILABLE_ENDPOINTS)),
],
}
def extendMarkdown(self, md, md_globals):
self.oembed_consumer = self.prepare_oembed_consumer()
pattern = OEmbedLinkPattern(OEMBED_LINK_RE, md, self.oembed_consumer)
md.inlinePatterns.add('oembed_link', pattern, '<image_link')
link_pattern = OEmbedLinkPattern(OEMBED_LINK_RE, md,
self.oembed_consumer)
md.inlinePatterns.add('oembed_link', link_pattern, '<image_link')
def prepare_oembed_consumer(self):
allowed_endpoints = self.getConfig('allowed_endpoints',
ENDPOINTS.keys())
AVAILABLE_ENDPOINTS)
consumer = oembed.OEmbedConsumer()
[consumer.addEndpoint(v)
for k,v in ENDPOINTS.items()

View file

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import logging
from markdown.inlinepatterns import Pattern
import oembed
LOG = logging.getLogger(__name__)
@ -18,6 +19,9 @@ class OEmbedLinkPattern(Pattern):
def handleMatch(self, match):
url = match.group(3).strip()
response = self.consumer.embed(url)
try:
response = self.consumer.embed(url)
except oembed.OEmbedNoEndpoint:
return None
placeholder = self.markdown.htmlStash.store(response['html'])
return placeholder

View file

@ -47,12 +47,7 @@ class OEmbedPatternRegexTestCase(unittest.TestCase):
class OEmbedExtensionTestCase(unittest.TestCase):
def setUp(self):
md_params = {
'extensions': [
'oembed',
],
}
self.markdown = markdown.Markdown(**md_params)
self.markdown = markdown.Markdown(extensions=['oembed'])
class IgnoredTestCase(OEmbedExtensionTestCase):
@ -112,3 +107,41 @@ class YoutubeTestCase(OEmbedExtensionTestCase):
output = self.markdown.convert(text)
self.assertEqual(output, expected)
class LimitedOEmbedExtensionTestCase(unittest.TestCase):
def setUp(self):
self.markdown = markdown.Markdown(
extensions=['oembed'],
extension_configs={
'oembed': {
'allowed_endpoints': ['youtube',],
}
})
def test_youtube_link(self):
"""
YouTube video link.
"""
text = '![video](http://www.youtube.com/watch?v=zqnh_YJBvOI)'
expected = '<iframe width="459" height="344" src="http://www.youtube.com/embed/zqnh_YJBvOI?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>'
output = self.markdown.convert(text)
self.assertEqual(output, expected)
def test_youtube_short_link(self):
"""
Short format YouTube video link.
"""
text = '![video](http://youtu.be/zqnh_YJBvOI)'
expected = '<iframe width="459" height="344" src="http://www.youtube.com/embed/zqnh_YJBvOI?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>'
output = self.markdown.convert(text)
self.assertEqual(output, expected)
def test_vimeio_link(self):
"""
Vimeo video link.
"""
text = '![link](http://vimeo.com/52970271)'
expected = '<p><img alt="link" src="http://vimeo.com/52970271" /></p>'
output = self.markdown.convert(text)
self.assertEqual(output, expected)