|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import markdown |
| 4 | +from markdown.util import etree |
| 5 | + |
| 6 | + |
| 7 | +class VideoExtension(markdown.Extension): |
| 8 | + |
| 9 | + def __init__(self, **kwargs): |
| 10 | + self.config = { |
| 11 | + 'dailymotion_width': ['480', 'Width for Dailymotion videos'], |
| 12 | + 'dailymotion_height': ['270', 'Height for Dailymotion videos'], |
| 13 | + 'metacafe_width': ['440', 'Width for Metacafe videos'], |
| 14 | + 'metacafe_height': ['248', 'Height for Metacafe videos'], |
| 15 | + 'veoh_width': ['410', 'Width for Veoh videos'], |
| 16 | + 'veoh_height': ['341', 'Height for Veoh videos'], |
| 17 | + 'vimeo_width': ['500', 'Width for Vimeo videos'], |
| 18 | + 'vimeo_height': ['281', 'Height for Vimeo videos'], |
| 19 | + 'yahoo_width': ['624', 'Width for Yahoo! videos'], |
| 20 | + 'yahoo_height': ['351', 'Height for Yahoo! videos'], |
| 21 | + 'youtube_width': ['560', 'Width for Youtube videos'], |
| 22 | + 'youtube_height': ['315', 'Height for Youtube videos'], |
| 23 | + } |
| 24 | + |
| 25 | + # Override defaults with user settings |
| 26 | + for key, value in kwargs.items(): |
| 27 | + self.setConfig(key, str(value)) |
| 28 | + |
| 29 | + def add_inline(self, md, name, klass, re): |
| 30 | + pattern = klass(re) |
| 31 | + pattern.md = md |
| 32 | + pattern.ext = self |
| 33 | + md.inlinePatterns.add(name, pattern, "<reference") |
| 34 | + |
| 35 | + def extendMarkdown(self, md, md_globals): |
| 36 | + self.add_inline(md, 'dailymotion', Dailymotion, |
| 37 | + r'([^(]|^)https?://www\.dailymotion\.com/video/(?P<dailymotionid>[a-zA-Z0-9]+)(_[\w\-]*)?') |
| 38 | + self.add_inline(md, 'metacafe', Metacafe, |
| 39 | + r'([^(]|^)http://www\.metacafe\.com/watch/(?P<metacafeid>\d+)/?(:?.+/?)') |
| 40 | + self.add_inline(md, 'veoh', Veoh, |
| 41 | + r'([^(]|^)http://www\.veoh\.com/\S*(#watch%3D|watch/)(?P<veohid>\w+)') |
| 42 | + self.add_inline(md, 'vimeo', Vimeo, |
| 43 | + r'([^(]|^)http://(www.|)vimeo\.com/(?P<vimeoid>\d+)\S*') |
| 44 | + self.add_inline(md, 'yahoo', Yahoo, |
| 45 | + r'([^(]|^)http://screen\.yahoo\.com/.+/?') |
| 46 | + self.add_inline(md, 'youtube', Youtube, |
| 47 | + r'([^(]|^)https?://www\.youtube\.com/watch\?\S*v=(?P<youtubeid>\S[^&/]+)') |
| 48 | + self.add_inline(md, 'youtube_short', Youtube, |
| 49 | + r'([^(]|^)https?://youtu\.be/(?P<youtubeid>\S[^?&/]+)?') |
| 50 | + |
| 51 | + |
| 52 | +class Dailymotion(markdown.inlinepatterns.Pattern): |
| 53 | + |
| 54 | + def handleMatch(self, m): |
| 55 | + url = '//www.dailymotion.com/embed/video/%s' % m.group('dailymotionid') |
| 56 | + width = self.ext.config['dailymotion_width'][0] |
| 57 | + height = self.ext.config['dailymotion_height'][0] |
| 58 | + return render_iframe(url, width, height) |
| 59 | + |
| 60 | + |
| 61 | +class Metacafe(markdown.inlinepatterns.Pattern): |
| 62 | + |
| 63 | + def handleMatch(self, m): |
| 64 | + url = '//www.metacafe.com/embed/%s/' % m.group('metacafeid') |
| 65 | + width = self.ext.config['metacafe_width'][0] |
| 66 | + height = self.ext.config['metacafe_height'][0] |
| 67 | + return render_iframe(url, width, height) |
| 68 | + |
| 69 | + |
| 70 | +class Veoh(markdown.inlinepatterns.Pattern): |
| 71 | + |
| 72 | + def handleMatch(self, m): |
| 73 | + url = '//www.veoh.com/videodetails2.swf?permalinkId=%s' % m.group('veohid') |
| 74 | + width = self.ext.config['veoh_width'][0] |
| 75 | + height = self.ext.config['veoh_height'][0] |
| 76 | + return flash_object(url, width, height) |
| 77 | + |
| 78 | + |
| 79 | +class Vimeo(markdown.inlinepatterns.Pattern): |
| 80 | + |
| 81 | + def handleMatch(self, m): |
| 82 | + url = '//player.vimeo.com/video/%s' % m.group('vimeoid') |
| 83 | + width = self.ext.config['vimeo_width'][0] |
| 84 | + height = self.ext.config['vimeo_height'][0] |
| 85 | + return render_iframe(url, width, height) |
| 86 | + |
| 87 | + |
| 88 | +class Yahoo(markdown.inlinepatterns.Pattern): |
| 89 | + |
| 90 | + def handleMatch(self, m): |
| 91 | + url = m.string + '?format=embed&player_autoplay=false' |
| 92 | + width = self.ext.config['yahoo_width'][0] |
| 93 | + height = self.ext.config['yahoo_height'][0] |
| 94 | + return render_iframe(url, width, height) |
| 95 | + |
| 96 | + |
| 97 | +class Youtube(markdown.inlinepatterns.Pattern): |
| 98 | + |
| 99 | + def handleMatch(self, m): |
| 100 | + url = '//www.youtube.com/embed/%s' % m.group('youtubeid') |
| 101 | + width = self.ext.config['youtube_width'][0] |
| 102 | + height = self.ext.config['youtube_height'][0] |
| 103 | + return render_iframe(url, width, height) |
| 104 | + |
| 105 | + |
| 106 | +def render_iframe(url, width, height): |
| 107 | + iframe = etree.Element('iframe') |
| 108 | + iframe.set('width', width) |
| 109 | + iframe.set('height', height) |
| 110 | + iframe.set('src', url) |
| 111 | + iframe.set('allowfullscreen', 'true') |
| 112 | + iframe.set('frameborder', '0') |
| 113 | + return iframe |
| 114 | + |
| 115 | + |
| 116 | +def flash_object(url, width, height): |
| 117 | + obj = etree.Element('object') |
| 118 | + obj.set('type', 'application/x-shockwave-flash') |
| 119 | + obj.set('width', width) |
| 120 | + obj.set('height', height) |
| 121 | + obj.set('data', url) |
| 122 | + param = etree.Element('param') |
| 123 | + param.set('name', 'movie') |
| 124 | + param.set('value', url) |
| 125 | + obj.append(param) |
| 126 | + param = etree.Element('param') |
| 127 | + param.set('name', 'allowFullScreen') |
| 128 | + param.set('value', 'true') |
| 129 | + obj.append(param) |
| 130 | + return obj |
| 131 | + |
| 132 | + |
| 133 | +def makeExtension(**kwargs): |
| 134 | + return VideoExtension(**kwargs) |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + import doctest |
| 139 | + doctest.testmod() |
0 commit comments