Skip to content

Commit 4af79ac

Browse files
authored
Add nbVideo & nbAudio (revive #153) (#244)
* add nbVideo & nbAudio + `relToRoot()` * add nbVideo & nbAudio to all_blocks.nim * add bad apple explainer to `nbVideo` section
1 parent 6daf489 commit 4af79ac

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

Diff for: docs/media/bad_apple!!.mp4

7.46 MB
Binary file not shown.

Diff for: docs/media/bad_apple!!.webm

1.22 MB
Binary file not shown.

Diff for: docsrc/allblocks.nim

+25
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,31 @@ Most formats (.jpg, .png) are accepted. The caption is optional!
108108
nimibCode:
109109
nbImage(url="images/todd-cravens-nimib-unsplash.jpg", caption="Blue Whale (photograph by Todd Cravens)")
110110

111+
nbCodeBlock: "nbVideo"
112+
nbText: """
113+
`nbVideo` allows you to display videos within your nimib document. You may choose if the
114+
video autoplays, loops, and/or is muted.
115+
116+
Pictured below is Bad Apple!! If you aren't sure what it is, or are curious about its
117+
origins, here's [an explainer](https://www.youtube.com/watch?v=6QY4ekac1_Q).
118+
119+
The `typ` parameter specifies the video's MIME type, and is optional!
120+
"""
121+
122+
nimibCode:
123+
nbVideo(url="media/bad_apple!!.mp4", typ="video/mp4")
124+
125+
nbCodeBlock: "nbAudio"
126+
nbText: """
127+
`nbAudio` enables you to play audio in nimib. You may choose if the audio autoplays,
128+
loops, and/or is muted.
129+
130+
The `typ` parameter is similar to `nbVideo`'s.
131+
"""
132+
133+
nimibCode:
134+
nbAudio(url="media/bad_apple!!.webm", loop=true)
135+
111136
nbCodeBlock: "nbFile"
112137
nbText: """
113138
`nbFile` can save the contents of block into a file or display the contents of a file.

Diff for: src/nimib.nim

+24-6
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,7 @@ template nbTextWithCode*(body: untyped) =
112112

113113
template nbImage*(url: string, caption = "", alt = "") =
114114
newNbSlimBlock("nbImage"):
115-
nb.blk.context["url"] =
116-
if isAbsolute(url) or url[0..3] == "http":
117-
url
118-
else:
119-
nb.context["path_to_root"].vString / url
120-
115+
nb.blk.context["url"] = nb.relToRoot(url)
121116
nb.blk.context["alt_text"] =
122117
if alt == "":
123118
caption
@@ -126,6 +121,29 @@ template nbImage*(url: string, caption = "", alt = "") =
126121

127122
nb.blk.context["caption"] = caption
128123

124+
# todo captions and subtitles support maybe?
125+
template nbVideo*(url: string, typ: string = "", autoplay = false, muted = false, loop: bool = false) =
126+
newNbSlimBlock("nbVideo"):
127+
nb.blk.context["url"] = nb.relToRoot(url)
128+
nb.blk.context["type"] =
129+
if typ == "": "video/" & url.splitFile.ext[1..^1] # remove the leading dot
130+
else: typ
131+
132+
if autoplay: nb.blk.context["autoplay"] = "autoplay"
133+
if muted: nb.blk.context["muted"] = "muted"
134+
if loop: nb.blk.context["loop"] = "loop"
135+
136+
template nbAudio*(url: string, typ: string = "", autoplay = false, muted = false, loop: bool = false) =
137+
newNbSlimBlock("nbAudio"):
138+
nb.blk.context["url"] = nb.relToRoot(url)
139+
nb.blk.context["type"] =
140+
if typ == "": "audio/" & url.splitFile.ext[1..^1]
141+
else: typ
142+
143+
if autoplay: nb.blk.context["autoplay"] = "autoplay"
144+
if muted: nb.blk.context["muted"] = "muted"
145+
if loop: nb.blk.context["loop"] = "loop"
146+
129147
template nbFile*(name: string, content: string) =
130148
## Generic string file
131149
newNbSlimBlock("nbFile"):

Diff for: src/nimib/docs.nim

+9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import std/os
2+
import std/uri
23
import browsers
34
import types, logging, renders
45

6+
proc relToRoot*(doc: NbDoc, url: string): string =
7+
## if url is relative, it is assumed as relative to root and adjusted for current document
8+
9+
if isAbsolute(url) or isAbsolute(parseUri(url)):
10+
url
11+
else:
12+
doc.context["path_to_root"].vString / url
13+
514
proc write*(doc: var NbDoc) =
615
log "current directory: " & getCurrentDir()
716
let dir = doc.filename.splitFile().dir

Diff for: src/nimib/renders.nim

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ proc useHtmlBackend*(doc: var NbDoc) =
2525
<img src="{{url}}" alt="{{alt_text}}">
2626
<figcaption>{{caption}}</figcaption>
2727
</figure>"""
28+
doc.partials["nbVideo"] = """<video controls {{loop}} {{autoplay}} {{muted}}>
29+
<source src="{{url}}" {{#type}}type="{{type}}"{{/type}}>
30+
Your browser does not support the video element.
31+
</video>"""
32+
doc.partials["nbAudio"] = """<audio controls {{loop}} {{autoplay}} {{muted}}>
33+
<source src="{{url}}" {{#type}}type="{{type}}"{{/type}}>
34+
Your browser does not support the audio element.
35+
</audio>"""
2836
doc.partials["nbRawHtml"] = "{{&output}}"
2937
doc.partials["nbPython"] = """
3038
<pre><code class="python hljs">{{&code}}</code></pre>

0 commit comments

Comments
 (0)