forked from getodk/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.py
164 lines (119 loc) · 4.85 KB
/
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import shutil
import sphinx.environment
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.util.compat import Directive
def yes_no(name, arg):
if arg == 'yes' or arg == 'true':
return name
elif arg == 'no' or arg == 'false':
return None
else:
raise Exception("Value for {} attribute can only be a boolean" .format(name))
def preload_choice(arg):
if arg == 'auto' or arg == 'metadata' or arg == 'none':
return arg
else:
raise Exception("Value for preload attribute can only be auto, metadata or none")
class video_node(nodes.General, nodes.Element): pass
def visit_video_html(self, node):
srcPath = node["source_dir"].split('incl',1)[0] + "%s"
# .split() removes everything after 'incl'
# so video files will only be looked for in the root dir
vsrc = node["uri"]
spth = srcPath % vsrc
if "tmp1-src" not in spth:
if os.path.exists("./odk2-build/_videos"):
pass
else:
os.makedirs("./odk2-build/_videos/")
dpth = "./odk2-build/_videos/%s" %vsrc[vsrc.rfind('/')+1:]
else:
if os.path.exists("./odk1-build/_videos"):
pass
else:
os.makedirs("./odk1-build/_videos/")
dpth = "./odk1-build/_videos/%s" %vsrc[vsrc.rfind('/')+1:]
shutil.copyfile(spth, dpth)
src = "../_videos/%s" % vsrc[vsrc.rfind('/')+1:]
attrs = {
"src":"%s" %src,
"style":"max-width:100%",
}
if node["poster"] is not None:
psrc = node["poster"]
p_spth = "./src%s" % psrc
p_dpth = "./build/_videos/%s" %psrc[psrc.rfind('/')+1:]
shutil.copyfile(p_spth, p_dpth)
psrc = "../_videos/%s" % psrc[psrc.rfind('/')+1:]
attrs["poster"] = "%s" % psrc
if node["autoplay"] == "autoplay":
attrs["autoplay"] = "autoplay"
if node["controls"] == "controls":
attrs["controls"] = "controls"
if node["loop"] == "loop":
attrs["loop"] = "loop"
if node["muted"] == "muted":
attrs["muted"] = "muted"
if node["preload"] is not None:
attrs["preload"] = "%s" % node["preload"]
if node["cl"] is not None:
attrs["class"] = "%s" % node["cl"]
self.body.append(self.starttag(node, "video", **attrs))
def depart_video_html(self, node):
self.body.append("</video>")
def visit_video_nonhtml(self, node):
pass
def depart_video_nonhtml(self,node):
pass
class Video(Directive):
has_content = True
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
option_spec = {
'autoplay' : directives.unchanged,
'controls' : directives.unchanged,
'loop' : directives.unchanged,
'muted' : directives.unchanged,
'poster' : directives.unchanged,
'preload' : directives.unchanged,
'class' : directives.unchanged,
}
def run(self):
autoplay = None
controls = "controls"
loop = None
muted = "muted"
poster = None
preload = None
cl = None
if "autoplay" in self.options:
autoplay = yes_no("autoplay",self.options["autoplay"])
if "controls" in self.options:
controls = yes_no("controls",self.options["controls"])
if "loop" in self.options:
loop = yes_no("loop",self.options["loop"])
if "muted" in self.options:
muted = yes_no("muted",self.options["muted"])
if "poster" in self.options:
poster = directives.uri(self.options["poster"])
if "preload" in self.options:
preload = preload_choice(self.options["preload"])
if "class" in self.options:
cl = self.options["class"]
uri = directives.uri(self.arguments[0])
source_dir = os.path.dirname(os.path.abspath(self.state.document.current_source))
vid = video_node(uri = uri, source_dir=source_dir, autoplay = autoplay, controls = controls,
loop = loop, muted = muted, poster = poster,
preload = preload, cl = cl)
self.state.nested_parse(self.content, self.content_offset, vid)
return [vid]
def setup(app):
app.add_node(video_node, html = (visit_video_html, depart_video_html),
latex = (visit_video_nonhtml, depart_video_nonhtml),
epub = (visit_video_nonhtml, depart_video_nonhtml),
text = (visit_video_nonhtml, depart_video_nonhtml),
)
app.add_directive("video", Video)