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