2012-11-13 21:28:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from markdown import Extension
|
|
|
|
|
import oembed
|
2015-04-21 09:18:42 +00:00
|
|
|
from mdx_oembed.endpoints import DEFAULT_ENDPOINTS
|
2012-11-13 21:28:10 +00:00
|
|
|
from mdx_oembed.inlinepatterns import OEmbedLinkPattern, OEMBED_LINK_RE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OEmbedExtension(Extension):
|
|
|
|
|
|
2015-04-02 07:47:33 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
self.config = {
|
|
|
|
|
'allowed_endpoints': [
|
2015-04-21 09:18:42 +00:00
|
|
|
DEFAULT_ENDPOINTS,
|
|
|
|
|
"A list of oEmbed endpoints to allow. Defaults to "
|
|
|
|
|
"endpoints.DEFAULT_ENDPOINTS"
|
2015-04-02 07:47:33 +00:00
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
super(OEmbedExtension, self).__init__(**kwargs)
|
2012-11-13 23:28:04 +00:00
|
|
|
|
2022-01-17 15:02:18 +00:00
|
|
|
def extendMarkdown(self, md):
|
2012-11-13 21:28:10 +00:00
|
|
|
self.oembed_consumer = self.prepare_oembed_consumer()
|
2012-11-13 23:28:04 +00:00
|
|
|
link_pattern = OEmbedLinkPattern(OEMBED_LINK_RE, md,
|
|
|
|
|
self.oembed_consumer)
|
2022-01-17 15:02:18 +00:00
|
|
|
md.inlinePatterns.register(link_pattern, 'oembed_link', 75)
|
|
|
|
|
|
2012-11-13 21:28:10 +00:00
|
|
|
|
|
|
|
|
def prepare_oembed_consumer(self):
|
2015-04-21 09:18:42 +00:00
|
|
|
allowed_endpoints = self.getConfig('allowed_endpoints', DEFAULT_ENDPOINTS)
|
2012-11-13 21:28:10 +00:00
|
|
|
consumer = oembed.OEmbedConsumer()
|
2015-04-21 09:18:42 +00:00
|
|
|
|
|
|
|
|
if allowed_endpoints:
|
|
|
|
|
for endpoint in allowed_endpoints:
|
|
|
|
|
consumer.addEndpoint(endpoint)
|
|
|
|
|
|
2012-11-13 21:28:10 +00:00
|
|
|
return consumer
|