-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_blocks.nim
316 lines (271 loc) · 9.46 KB
/
custom_blocks.nim
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
import nimib, nimiSlides
import nimib / blocks
import std / [strutils, strformat]
# titles
let
title_source* = "📋`CodeAsInSource`"
title_block* = "make your own `NbBlock`👷"
title_howto* = "Fancy blocks 🖼️"
title_python* = "🐍`nbPython`"
title_js* = "🤯 `nbJsFromCode`, `nbKaraxCode`"
title_plant* = "A mathy plant app 🌱"
title_contribute* = "Contributing🤲"
title_roadmap* = "Roadmap🗺️"
# custom init
template myInit*(sourceFileRel = "") =
when defined(noReveal):
nbInit(thisFileRel=sourceFileRel)
else:
nbInit(theme = revealTheme, thisFileRel=sourceFileRel)
when not defined(useNimConfSlide):
setSlidesTheme(Moon)
else:
nimConfTheme()
when not defined(skipNimLogo):
addNimLogo
addStuff
template useSource*(filename: string) =
nb.source = read(filename.RelativeFile)
template addStuff* =
addNbTextSmall
addNbCodeDontRun
addNewAnimateCodeBlocks
addNimibCodeBlocks
addNbCodeNoLineNumbers
addNbCodeDontRunNoLineNumbers
addBigButtons
optionalInitPython
# nim logo static
template addNimLogo* =
## use outside of slides
# adapted from https://stackoverflow.com/questions/22033474/place-background-image-in-bottom-corner-of-every-slide-of-reveal-js
# background did not work on our conf theme (maybe because of some other settings?)
# trick was to use img (and src) instead of div (and background)
# had to manually adjust pixels to have it almost the same as corner image, probably because they refer to some other box (not great)
nbRawHtml: """
<img src="https://github.com/nim-lang/assets/raw/master/Art/logo-crown.png" style="position: absolute;
top: -20px;
right: -170px;
width: 90px;
height: 70px;"></img>
"""
# nim conf slides
template nimConfSlide*(body: untyped) =
slide:
when defined(useCornerImage):
cornerImage("https://github.com/nim-lang/assets/raw/master/Art/logo-crown.png", UpperRight, size=100, animate=false)
body
template nimConfSlide*(options: SlideOptions, body: untyped) =
slide(options):
when defined(useCornerImage):
cornerImage("https://github.com/nim-lang/assets/raw/master/Art/logo-crown.png", UpperRight, size=100, animate=false)
body
# nimconf theme
template nimConfTheme*() =
setSlidesTheme(Black)
let nimYellow = "#FFE953"
nb.addStyle: """
:root {
--r-background-color: #181922;
--r-heading-color: $1;
--r-link-color: $1;
--r-selection-color: $1;
--r-link-color-dark: darken($1 , 15%)
}
.reveal ul, .reveal ol {
display: block;
text-align: left;
}
li::marker {
color: $1;
content: "»";
}
li {
padding-left: 12px;
}
""" % [nimYellow]
# mySlide: pick between nimconf or other
template mySlide*(body: untyped) =
when defined(useNimConfSlide):
nimConfSlide:
body
else:
slide:
body
template mySlide*(options: SlideOptions, body: untyped) =
when defined(useNimConfSlide):
nimConfSlide(options):
body
else:
slide(options):
body
# slides that use mySlide
template slideText*(text: string) =
mySlide:
nbText: text
template slideAutoAnimate*(body: untyped) =
mySlide(slideOptions(autoAnimate=true)):
body
# small text
template addNbTextSmall* =
nb.partials["nbTextSmall"] = "<small>" & nb.partials["nbText"] & "</small>"
nb.renderPlans["nbTextSmall"] = nb.renderPlans["nbText"]
template nbTextSmall*(text: string) =
nbText: text
nb.blk.command = "nbTextSmall"
# modified code blocks
template nbCodeDontRun*(body: untyped) = # from hugos_slides
newNbCodeBlock("nbCodeDontRun", body):
discard
template addNbCodeDontRun* =
nb.partials["nbCodeDontRun"] = nb.partials["nbCode"]
nb.renderPlans["nbCodeDontRun"] = nb.renderPlans["nbCode"]
template nbCodeNoLineNumbers*(body: untyped) =
nbCode:
body
nb.blk.command = "nbCodeNoLineNumbers"
template nbCodeDontRunNoLineNumbers*(body: untyped) = # from hugos_slides
newNbCodeBlock("nbCodeDontRunNoLineNumbers", body):
discard
template addNbCodeDontRunNoLineNumbers* =
nb.partials["addNbCodeDontRunNoLineNumbers"] = nb.partials["nbCodeNoLineNumbers"]
nb.renderPlans["addNbCodeDontRunNoLineNumbers"] = nb.renderPlans["nbCodeNoLineNumbers"]
template addNbCodeNoLineNumbers* =
nb.partials["nbCodeNoLineNumbers"] =
nb.partials["nbCode"].replace("nbCodeSource", "nbCodeSourceNoLineNumbers")
nb.renderPlans["nbCodeNoLineNumbers"] = nb.renderPlans["nbCode"]
nb.partials["nbCodeSourceNoLineNumbers"] =
nb.partials["nbCodeSource"].replace(" data-line-numbers", "")
template nbCodeDontRunAnimateImpl*(body: untyped) =
discard
template nbCodeBeforeImpl*(body: untyped) = # use to show how CodeFromAst would have looked like
nb.blk.code = toStr(body)
template addNewAnimateCodeBlocks* =
newAnimateCodeBlock(nbCodeDontRunAnimate, nbCodeDontRunAnimateImpl)
newAnimateCodeBlock(nbCodeBeforeAnimate, nbCodeBeforeImpl)
template nbCodeDontRunBefore*(body: untyped) = # from hugos_slides
newNbCodeBlock("nbCodeDontRun", body):
discard
nb.blk.code = toStr(body)
template nimibCode*(body: untyped) =
newNbCodeBlock("nimibCode", body):
discard
fragmentFadeIn:
nbRawHtml: "<hr/>"
body
template nimibCodeAnimate*(lines: varargs[seq[HSlice[int, int]]], body: untyped) =
## Shows code and its output just like nbCode, but highlights different lines of the code in the order specified in `lines`.
## lines: Specify which lines to highlight and in which order. (Must be specified as a seq[HSlice])
## Ex:
## ```nim
## animateCode(@[1..1], @[3..4, 6..6]): body
## ```
## This will first highlight line 1, then lines 3, 4 and 6.
newNbCodeBlock("nimibCodeAnimate", body):
var linesString: string
if lines.len > 0:
linesString &= "|"
for lineBundle in lines:
for line in lineBundle:
linesString &= $line.a & "-" & $line.b & ","
linesString &= "|"
if lines.len > 0:
linesString = linesString[0 .. ^3]
nb.blk.context["highlightLines"] = linesString
fragmentFadeIn:
nbRawHtml: "<hr/>"
body
template nimibCodeAnimate*(lines: varargs[HSlice[int, int], toHSlice], body: untyped) =
## Shows code and its output just like nbCode, but highlights different lines of the code in the order specified in `lines`.
## lines: Specify which lines to highlight and in which order. (Must be specified as a HSlice)
## Ex:
## ```nim
## animateCode(1..1, 2..3, 5..5, 4..4): body
## ```
## This will first highlight line 1, then lines 2 and 3, then line 5 and last line 4.
var s: seq[seq[HSlice[int, int]]]
for line in lines:
s.add @[line]
nimibCodeAnimate(s):
body
template addNimibCodeBlocks* =
nb.partials["nimibCode"] = nb.partials["nbCode"]
nb.renderPlans["nimibCode"] = nb.renderPlans["nbCode"]
nb.partials["nimibCodeAnimate"] = nb.partials["animateCode"]
nb.renderPlans["nimibCodeAnimate"] = nb.renderPlans["animateCode"]
# needed only for python section, allows to be skipped
template optionalInitPython* =
when not defined(skipPython):
when defined(hugo):
import nimpy/py_lib
pyInitLibPath("/usr/lib/x86_64-linux-gnu/libpython3.10.so.1.0")
nbInitPython()
# Add CSS to make buttons bigger
template addBigButtons* =
nb.addStyle: """
button {
font-size: 30px;
padding: 10px 24px;
border-radius: 4px;
}
"""
# used in thank you slide
template textSwitcher*(texts: seq[string], duration: float = 2) =
var cssString = "<style>\n"
for i, phrase in texts:
let id = "textSwitcher-" & $nb.newId()
let animationName = "animation-" & id
nbRawHtml: """<div id="$1" style="position: absolute; width: 100%; opacity: 0; text-align: center;">$2</div>""" % [id, phrase]
cssString.add "#$1 { animation: $2 $3s infinite }\n" % [id, animationName, $(texts.len.float * duration)]
let middlePercent = 100 * (1 / (2 * texts.len) + i * 1 / texts.len)
let lowerPercent = middlePercent - 100 * (1 / (2 * texts.len))
let upperPercent = middlePercent + 100 * (1 / (2 * texts.len))
cssString.add """
@keyframes $1 {
$2% {
opacity: 0;
}
$3% {
opacity: 1;
}
$4% {
opacity: 0;
}
}
""" % [animationName, $lowerPercent, $middlePercent, $upperPercent]
cssString.add "\n</style>"
nbRawHtml: cssString
# slide stuff
template fadeInText*(text: string) =
fragment(fadeInThenSemiOut):
nbText: text
#[ this embed worked in nimconf2021 nimib slides, it does not work anymore
template nbEmbed(url: string) =
nbRawHtml: "<iframe src=\"" & url & "\" class=\"fullframe\"></iframe>"
]#
template nbEmbedFromNblog*(filename: string) =
nbEmbed("https://pietroppeter.github.io/nblog/drafts/" & filename & ".html")
# instead I will use full interactive iframes
template slideIframe*(url: string) =
nbRawHtml: "<section data-background-iframe=\"" & url & "\" data-background-interactive></section>"
template slideIframeFromNblog*(filename: string) =
slideIframe("https://pietroppeter.github.io/nblog/drafts/" & filename & ".html")
proc nimibIssue*(num: int): string =
return fmt"[#{$num}](https://github.com/pietroppeter/nimib/issues/{$num})"
proc findNextGenSym(code: string): string =
var i = find(code, "`gensym")
if i < 0:
return ""
result = "`gensym"
i += len(result)
while i < len(code) and code[i].isDigit:
result &= code[i]
inc i
proc cleanupGensym*(code: string): string =
result = code
var gensym = findNextGenSym(result)
while len(gensym) > 0:
result = result.replace(gensym, "")
gensym = findNextGenSym(result)
template nbCleanupGensym* =
nb.blk.code = cleanupGensym(nb.blk.code)