Allow arbitrary oEmbed endpoints

Before this change, only three hardcoded oEmbed endpoints were available
for users of the extension; youtube, flickr and vimeo. In the
configuration, it was only possible to pass a subset of those 3 oEmbed
endpoints. It was done by passing the names of the allowed endpoints.

With this change, the API has changed. The allowed_endpoints kwarg now
expects to be passed a list of OEmbedEndpoint objects (as opposed to
names). This means the responsibility of creating OEmbedEndpoint objects
has been shifted to the user of the extension. If the allowed_endpoints
kwarg is omitted, the default oEmbed endpoints used will be the same as
before; youtube, flickr and vimeo.

The motivation is to allow arbitrary oEmbed endpoints, without
necessitating anyone to maintain the list of all possible oEmbed
endpoints out there.
This commit is contained in:
Sami Turcotte 2015-04-21 05:18:42 -04:00
parent 350d90b6b9
commit e2e29489d7
2 changed files with 19 additions and 16 deletions

View file

@ -2,15 +2,20 @@
import oembed
ENDPOINTS = {
'youtube': oembed.OEmbedEndpoint('http://www.youtube.com/oembed', [
DEFAULT_ENDPOINTS = [
# Youtube
oembed.OEmbedEndpoint('http://www.youtube.com/oembed', [
'https?://(*.)?youtube.com/*',
'https?://youtu.be/*',
]),
'flickr': oembed.OEmbedEndpoint('http://www.flickr.com/services/oembed/', [
# Flickr
oembed.OEmbedEndpoint('http://www.flickr.com/services/oembed/', [
'https?://*.flickr.com/*',
]),
'vimeo': oembed.OEmbedEndpoint('http://vimeo.com/api/oembed.json', [
# Vimeo
oembed.OEmbedEndpoint('http://vimeo.com/api/oembed.json', [
'https?://vimeo.com/*',
]),
}

View file

@ -1,21 +1,18 @@
# -*- coding: utf-8 -*-
from markdown import Extension
import oembed
from mdx_oembed.endpoints import ENDPOINTS
from mdx_oembed.endpoints import DEFAULT_ENDPOINTS
from mdx_oembed.inlinepatterns import OEmbedLinkPattern, OEMBED_LINK_RE
AVAILABLE_ENDPOINTS = ENDPOINTS.keys()
class OEmbedExtension(Extension):
def __init__(self, **kwargs):
self.config = {
'allowed_endpoints': [
AVAILABLE_ENDPOINTS,
"A list of oEmbed endpoints to allow. Possible values are "
"{}.".format(', '.join(AVAILABLE_ENDPOINTS)),
DEFAULT_ENDPOINTS,
"A list of oEmbed endpoints to allow. Defaults to "
"endpoints.DEFAULT_ENDPOINTS"
],
}
super(OEmbedExtension, self).__init__(**kwargs)
@ -27,10 +24,11 @@ class OEmbedExtension(Extension):
md.inlinePatterns.add('oembed_link', link_pattern, '<image_link')
def prepare_oembed_consumer(self):
allowed_endpoints = self.getConfig('allowed_endpoints',
AVAILABLE_ENDPOINTS)
allowed_endpoints = self.getConfig('allowed_endpoints', DEFAULT_ENDPOINTS)
consumer = oembed.OEmbedConsumer()
[consumer.addEndpoint(v)
for k,v in ENDPOINTS.items()
if k in allowed_endpoints]
if allowed_endpoints:
for endpoint in allowed_endpoints:
consumer.addEndpoint(endpoint)
return consumer