-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathvideo.lua
More file actions
367 lines (310 loc) · 11.9 KB
/
video.lua
File metadata and controls
367 lines (310 loc) · 11.9 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
-- === Utils ===
-- from http://lua-users.org/wiki/StringInterpolation
local interpolate = function(str, vars)
-- Allow replace_vars{str, vars} syntax as well as replace_vars(str, {vars})
if not vars then
vars = str
str = vars[1]
end
return (string.gsub(str, "({([^}]+)})",
function(whole, i)
return vars[i] or whole
end))
end
local function splitString (toSplit, delimiter)
delimiter = delimiter or "%s"
local t={}
for str in string.gmatch(toSplit, "([^".. delimiter .."]+)") do
table.insert(t, str)
end
return t
end
local function isEmpty(s)
return s == nil or s == ''
end
local isResponsive = function(width, height)
return isEmpty(height) and isEmpty(width)
end
VIDEO_SHORTCODE_NUM_VIDEOJS = 0
local VIDEO_TYPES = {
YOUTUBE = "YOUTUBE",
BRIGHTCOVE = "BRIGHTCOVE",
VIMEO = "VIMEO",
VIDEOJS = "VIDEOJS"
}
local ASPECT_RATIOS = {
["1x1"] = "ratio-1x1",
["4x3"] = "ratio-4x3",
["16x9"] = "ratio-16x9",
["21x9"] = "ratio-21x9"
}
local DEFAULT_ASPECT_RATIO = ASPECT_RATIOS["16x9"]
local wrapWithDiv = function(toWrap, aspectRatio, shouldAddResponsiveClasses)
local ratioClass = aspectRatio and ASPECT_RATIOS[aspectRatio] or DEFAULT_ASPECT_RATIO
local responsiveClasses = shouldAddResponsiveClasses and ' ratio ' .. ratioClass
wrapper = [[<div class="quarto-video{responsiveClasses}">{toWrap}</div>]]
return interpolate {
wrapper,
toWrap = toWrap,
responsiveClasses = responsiveClasses or '' }
end
local replaceCommonAttributes = function(snippet, params)
result = interpolate {
snippet,
src = params.src,
height = params.height and ' height="' .. params.height .. '"' or '',
width = params.width and ' width="' .. params.width .. '"' or '',
title = params.title or '',
ariaLabel = params.ariaLabel and ' aria-label="' .. params.ariaLabel .. '"' or '',
}
return result
end
local checkMatchStart = function(value, matcherFront)
return string.match(value, '^' .. matcherFront .. '(.-)$')
end
local youTubeBuilder = function(params)
if not (params and params.src) then return nil end
local src = params.src
match = checkMatchStart(src, 'https://www.youtube.com/embed/')
match = match or checkMatchStart(src, 'https://www.youtube.com/shorts/')
match = match or checkMatchStart(src, 'https://www.youtube%-nocookie.com/embed/')
match = match or checkMatchStart(src, 'https://youtu.be/')
match = match or string.match(src, '%?v=(.-)&')
match = match or string.match(src, '%?v=(.-)$')
if not match then return nil end
local YOUTUBE_EMBED = 'https://www.youtube.com/embed/'
params.src = YOUTUBE_EMBED .. match
local SNIPPET = [[<iframe data-external="1" src="{src}{start}"{width}{height} title="{title}"{ariaLabel} frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>]]
snippet = replaceCommonAttributes(SNIPPET, params)
local result = {}
result.snippet = interpolate {
snippet,
start = params.start and '?start=' .. params.start or ''
}
result.type = VIDEO_TYPES.YOUTUBE
result.src = params.src
result.videoId = match
return result
end
local brightcoveBuilder = function(params)
if not (params and params.src) then return nil end
local src = params.src
local isBrightcove = function()
return string.find(src, 'https://players.brightcove.net')
end
if not isBrightcove() then return nil end
local result = {}
local SNIPPET = [[<iframe data-external="1" src="{src}"{width}{height} allowfullscreen="" title="{title}"{ariaLabel} allow="encrypted-media"></iframe>]]
result.snippet = replaceCommonAttributes(SNIPPET, params)
result.type = VIDEO_TYPES.BRIGHTCOVE
result.src = params.src
return result
end
local vimeoBuilder = function(params)
if not (params and params.src) then return nil end
local VIMEO_STANDARD = 'https://vimeo.com/'
local match = checkMatchStart(params.src, VIMEO_STANDARD)
if not match then return nil end
-- Internal Links
-- bug/5390-vimeo-newlink
if string.find(match, '/') then
local internalMatch = string.gsub(match, "?(.*)", "" )
videoId = splitString(internalMatch, '/')[1]
privacyHash = splitString(internalMatch, '/')[2]
params.src = 'https://player.vimeo.com/video/' .. videoId .. '?h=' .. privacyHash
else
videoId = match
params.src = 'https://player.vimeo.com/video/' .. videoId
end
local SNIPPET = [[<iframe data-external="1" src="{src}"{width}{height} frameborder="0" title="{title}"{ariaLabel} allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>]]
local result = {}
result.snippet = replaceCommonAttributes(SNIPPET, params)
result.type = VIDEO_TYPES.VIMEO
result.src = params.src
result.videoId = videoId
return result
end
local videoJSBuilder = function(params)
if not (params and params.src) then return nil end
VIDEO_SHORTCODE_NUM_VIDEOJS = VIDEO_SHORTCODE_NUM_VIDEOJS + 1
local id = "video_shortcode_videojs_video" .. VIDEO_SHORTCODE_NUM_VIDEOJS
local SNIPPET = [[<video id="{id}"{width}{height} class="video-js vjs-default-skin vjs-big-play-centered {fluid}" controls preload="auto" data-setup='{}' title="{title}"{ariaLabel}><source src="{src}"></video>]]
local snippet = params.snippet or SNIPPET
snippet = replaceCommonAttributes(snippet, params)
snippet = interpolate {
snippet,
id = id,
fluid = isResponsive(params.width, params.height) and 'vjs-fluid' or ''
}
local result = {}
result.snippet = snippet
result.type = VIDEO_TYPES.VIDEOJS
result.src = params.src
result.id = id
return result
end
local getSnippetFromBuilders = function(src, height, width, title, start, ariaLabel)
local builderList = {
youTubeBuilder,
brightcoveBuilder,
vimeoBuilder,
videoJSBuilder}
local params = { src = src, height = height, width = width, title = title, start = start, ariaLabel = ariaLabel }
for i = 1, #builderList do
local builtSnippet = builderList[i](params)
if (builtSnippet) then
return builtSnippet
end
end
end
local helpers = {
["checkMatchStart"] = checkMatchStart,
["youTubeBuilder"] = youTubeBuilder,
["brightcoveBuilder"] = brightcoveBuilder,
["vimeoBuilder"] = vimeoBuilder,
["videoJSBuilder"] = videoJSBuilder,
["wrapWithDiv"] = wrapWithDiv,
["VIDEO_TYPES"] = VIDEO_TYPES,
["VIDEO_SHORTCODE_NUM_VIDEOJS"] = VIDEO_SHORTCODE_NUM_VIDEOJS,
["getSnippetFromBuilders"] = getSnippetFromBuilders
}
-- makes an asciidoc video raw block
-- see https://docs.asciidoctor.org/asciidoc/latest/macros/audio-and-video/
function formatAsciiDocVideo(src, type)
return 'video::' .. src .. '[' .. type .. ']'
end
local function asciidocVideo(src, height, width, title, start, _aspectRatio, ariaLabel)
local asciiDocVideoRawBlock = function(src, type)
return pandoc.RawBlock("asciidoc", formatAsciiDocVideo(src, type) .. '\n\n')
end
local videoSnippetAndType = getSnippetFromBuilders(src, height, width, title, start, ariaLabel)
if videoSnippetAndType.type == VIDEO_TYPES.YOUTUBE then
-- Use the video id to form an asciidoc video
if videoSnippetAndType.videoId ~= nil then
return asciiDocVideoRawBlock(videoSnippetAndType.videoId, 'youtube');
end
elseif videoSnippetAndType.type == VIDEO_TYPES.VIMEO then
return asciiDocVideoRawBlock(videoSnippetAndType.videoId, 'vimeo');
elseif videoSnippetAndType.type == VIDEO_TYPES.VIDEOJS then
return asciiDocVideoRawBlock(videoSnippetAndType.src, '');
else
-- this is not a local or supported video type for asciidoc
-- we should just emit a hyper link
end
end
function htmlVideo(src, height, width, title, start, aspectRatio, ariaLabel)
-- https://github.com/quarto-dev/quarto-cli/issues/6833
-- handle partially-specified width, height, and aspectRatio
if aspectRatio then
-- https://github.com/quarto-dev/quarto-cli/issues/11699#issuecomment-2549219533
-- we remove quotes as a
-- local workaround for inconsistent shortcode argument parsing on our end.
--
-- removing quotes in general is not a good idea, but the
-- only acceptable values for aspectRatio are 4x3, 16x9, 21x9, 1x1
-- and so we can safely remove quotes in this context.
local strs = splitString(aspectRatio:gsub('"', ''):gsub("'", ''), 'x')
local wr = tonumber(strs[1])
local hr = tonumber(strs[2])
local aspectRatioNum = wr / hr
if height and not width then
width = math.floor(height * aspectRatioNum + 0.5)
elseif width and not height then
height = math.floor(width / aspectRatioNum + 0.5)
end
end
local videoSnippetAndType = getSnippetFromBuilders(src, height, width, title, start, ariaLabel)
local videoSnippet
videoSnippet = videoSnippetAndType.snippet
if (videoSnippetAndType.type == VIDEO_TYPES.VIDEOJS) then
-- Can this be bundled with the VideoJS dependency
-- Avoid disjointed combination?
quarto.doc.add_html_dependency({
name = 'videojs',
scripts = { 'resources/videojs/video.min.js' },
stylesheets = { 'resources/videojs/video-js.css' }
})
local id = videoSnippetAndType.id or ''
local scriptTag = "<script>videojs(" .. id .. ");</script>"
quarto.doc.include_text("after-body", scriptTag)
end
local isVideoJS = function()
return videoSnippetAndType.type == VIDEO_TYPES.VIDEOJS
end
local isRevealJS = function()
return quarto.doc.is_format('revealjs')
end
local shouldAddResponsiveClasses = false
if isResponsive(width, height)
and not isRevealJS()
and not isVideoJS() then
if (not quarto.doc.has_bootstrap()) then
quarto.doc.add_html_dependency({
name = 'bootstrap-responsive',
stylesheets = { 'resources/bootstrap/bootstrap-responsive-ratio.css' }
})
end
shouldAddResponsiveClasses = true
end
if not isRevealJS() then
videoSnippet = wrapWithDiv(
videoSnippet,
aspectRatio,
shouldAddResponsiveClasses
)
end
-- inject the rendering code
return pandoc.RawBlock('html', videoSnippet)
end
-- return a table containing shortcode definitions
-- defining shortcodes this way allows us to create helper
-- functions that are not themselves considered shortcodes
return {
["video"] = function(args, kwargs, _meta, raw_args)
checkArg = function(toCheck, key)
value = pandoc.utils.stringify(toCheck[key])
if not isEmpty(value) then
return value
else
return nil
end
end
local srcValue = checkArg(kwargs, 'src')
local titleValue = checkArg(kwargs, 'title')
local startValue = checkArg(kwargs, 'start')
local heightValue = checkArg(kwargs, 'height')
local widthValue = checkArg(kwargs, 'width')
local aspectRatio = checkArg(kwargs, 'aspectRatio')
local ariaLabelValue = checkArg(kwargs, 'aria-label')
if isEmpty(aspectRatio) then
aspectRatio = checkArg(kwargs, 'aspect-ratio')
end
if isEmpty(srcValue) then
if #raw_args > 0 then
srcValue = pandoc.utils.stringify(raw_args[1])
else
-- luacov: disable
fail("No video source specified for video shortcode")
-- luacov: enable
end
end
if quarto.doc.is_format("html:js") then
return htmlVideo(srcValue, heightValue, widthValue, titleValue, startValue, aspectRatio, ariaLabelValue)
elseif quarto.doc.is_format("asciidoc") then
return asciidocVideo(srcValue, heightValue, widthValue, titleValue, startValue, aspectRatio, ariaLabelValue)
elseif quarto.doc.is_format("markdown") then
if srcValue:sub(1, 4) == "http" then
-- For remote videos, we can emit a link
return pandoc.Link(srcValue, titleValue or srcValue)
else
-- For local
-- use an image to allow markdown previewers to show video
return pandoc.Image(quarto.utils.as_inlines(titleValue), srcValue)
end
else
-- Fall-back to a link of the source
return pandoc.Link(srcValue, srcValue)
end
end,
["video-helpers"] = helpers,
}