Created
August 19, 2013 09:21
-
-
Save dmitric/6267253 to your computer and use it in GitHub Desktop.
Misaka based HtmlRenderer that lets you embed youtube and vimeo videos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from misaka import HtmlRenderer | |
import re | |
SOURCES = { | |
"youtube": { | |
"re": r'https?://(www\.|)youtube\.com/watch\?\S*v=(?P<youtube>[A-Za-z0-9_&=-]+)\S*', | |
"embed": u"//www.youtube.com/embed/%s" | |
}, | |
"vimeo": { | |
"re": r'https?://(www\.|)vimeo\.com/(?P<vimeo>\d+)\S*', | |
"embed": u"//player.vimeo.com/video/%s" | |
} | |
} | |
class VideoLinkRenderer(HtmlRenderer): | |
def setup(self): | |
super(VideoLinkRenderer, self).setup() | |
def image(self, link, title, alt): | |
for key, val in SOURCES.items(): | |
match = re.match(val["re"], link) | |
if match and match.group(key): | |
video_id = match.group(key) | |
return self.make_iframe(video_id.strip(), key, alt, title) | |
return u"<img src='{0}' alt='{1}' title='{2}'/>".format(link, alt, title) | |
def make_iframe(self, id, video_type, alt, title): | |
url = SOURCES[video_type]["embed"] % id | |
return u"<iframe class='{2}' src='{0}' alt='{1}' title='{3}' allowfullscreen></iframe>" \ | |
.format(url, alt, video_type, title) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment