Skip to content

Commit 216d3c8

Browse files
Merge pull request #32 from HugoGranstrom/fixDraculaPlusUseSets
Fix dracula + Use sets for `animateCode`
2 parents 778a197 + 8069399 commit 216d3c8

File tree

4 files changed

+54
-51
lines changed

4 files changed

+54
-51
lines changed

docsrc/nimconf2022.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ slide:
639639
nbText: "## Using Templates"
640640
nimConfAutoSlide:
641641
nbText: "## Using Templates"
642-
nbCodeDontRunAnimate([@[1..1, 3..3, 6..6]]):
642+
nbCodeDontRunAnimate({1, 3, 6}):
643643
slide(slideOptions(autoAnimate=true)):
644644
nbText: "## Animate header"
645645
slide(slideOptions(autoAnimate=true)):

docsrc/tutorials/code_block.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ Poof! The output is gone!
5050
One cool feature of Reveal.js is the animated code blocks.
5151
It allows you to create a code block like the ones we have created above, but highlight specific lines.
5252
This is done using the `animateCode` template by providing which lines to highlight and in which order.
53-
The lines can be specified either as a slice `x..y` or a single line number `x`.
53+
The lines can be specified either as a slice `x..y`, a single line number `x`, or a set of lines `{x, y}`.
5454
Note that the first line has line number 1 (and not 0).
5555
"""
5656

5757

5858
codeAndSlides:
5959
slide:
60-
animateCode(1, 2..3, 5, 3..4):
60+
animateCode(1, 2..3, 5, {2, 4}):
6161
let x1 = 1
6262
let x2 = 2
6363
let x3 = 3
6464
let x4 = 4
6565
let x5 = 5
6666

6767
nbText: hlMd"""
68-
As you can see, first line 1 is highlighted, then lines 2 through 3, then line 5 and lastly line 3 through 4. Neat!
68+
As you can see, first line 1 is highlighted, then lines 2 through 3, then line 5 and lastly line 2 through 4. Neat!
6969
Note that there exists no `animateCodeInBlock` currently, but you can always wrap your `animateCode` inside a `block:` to get the same effect.
7070
The animated code block is also useful if you have a very long code snippet that you want to show as it will auto-scroll for you:
7171
"""

nimiSlides.nimble

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.2.2"
3+
version = "0.2.3"
44
author = "Hugo Granström"
55
description = "Reveal.js theme for nimib"
66
license = "MIT"
@@ -14,22 +14,21 @@ requires "nimib >= 0.3.9"
1414
import os
1515

1616
task docsDeps, "install dependencies required to build docs":
17-
exec "nimble -y install [email protected] karax numericalnim nimibook"
17+
exec "nimble -y install [email protected] karax numericalnim nimibook@#head"
1818

1919
task buildDocs, "build all .nim files in docsrc/":
20-
for (kind, path) in walkDir("docsrc/"):
21-
if path.endsWith(".nim"):
22-
if "index" in path: continue
23-
echo "Building: " & path
24-
let buildCommand = "nim r " & path
20+
for path in ["showcase.nim", "nimconf2022.nim", "miscSlides.nim", "index_old.nim", "fragments.nim"]:
21+
let path = "docsrc" / path
22+
echo "Building: " & path
23+
let buildCommand = "nim r " & path
24+
exec buildCommand
25+
if "showcase" in path:
26+
let buildCommand = "nim r -d:themeWhite " & path
2527
exec buildCommand
26-
if "showcase" in path:
27-
let buildCommand = "nim r -d:themeWhite " & path
28-
exec buildCommand
2928

3029
task buildBook, "Builds the nimiBook docs":
31-
selfExec(" r -d:release nbook.nim init")
32-
selfExec(" r -d:release -f -d:nimibMaxProcesses=1 -d:nimibParallelBuild=false nbook.nim build")
30+
selfExec(" r nbook.nim init")
31+
selfExec(" r nbook.nim build")
3332

3433
task docs, "Generate automatic docs":
3534
exec "nim doc --project --index:on --git.url:https://github.com/HugoGranstrom/nimiSlides --git.commit:main --outdir:docs/docs src/nimiSlides.nim"

src/nimiSlides.nim

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ template showSlideNumber*() =
159159
template disableVerticalCentering*() =
160160
nb.context["disableCentering"] = true
161161

162+
proc addStyle*(doc: NbDoc, style: string) =
163+
doc.context["nb_style"] = doc.context["nb_style"].vString & "\n" & style
164+
162165
proc revealTheme*(doc: var NbDoc) =
163166
doc.partials["document"] = document
164167
doc.partials["head"] = head
@@ -190,6 +193,15 @@ proc revealTheme*(doc: var NbDoc) =
190193
doc.context["slidesTheme"] = "black"
191194
doc.context["nb_style"] = ""
192195

196+
doc.addStyle: """
197+
.nimislides-li {
198+
position: relative;
199+
}
200+
201+
.nimislides-li::before {
202+
position: absolute;
203+
}
204+
"""
193205

194206
try:
195207
let slidesConfig = loadTomlSection(doc.rawCfg, "nimislides", NimiSlidesConfig)
@@ -199,9 +211,6 @@ proc revealTheme*(doc: var NbDoc) =
199211
except CatchableError:
200212
discard # if it doesn't exists, just let it be
201213

202-
proc addStyle*(doc: NbDoc, style: string) =
203-
doc.context["nb_style"] = doc.context["nb_style"].vString & "\n" & style
204-
205214
var currentFragment*, currentSlideNumber*: int
206215

207216
proc slideOptionsToAttributes*(options: SlideOptions): string =
@@ -378,7 +387,7 @@ template listItem*(animation: seq[FragmentAnimation], body: untyped) =
378387
for an in animation:
379388
classString &= $an & " "
380389
fadeInNext:
381-
nbRawHtml: """<li class="fragment $1" data-fragment-index="$2" data-fragment-index-nimib="$2">""" % [classString, $currentFragment]
390+
nbRawHtml: """<li class="fragment $1 nimislides-li" data-fragment-index="$2" data-fragment-index-nimib="$2">""" % [classString, $currentFragment]
382391
currentFragment += 1
383392
body
384393
nbRawHtml: "</li>"
@@ -393,72 +402,67 @@ template listItem*(body: untyped) =
393402
proc toHSlice*(h: HSlice[int, int]): HSlice[int, int] = h
394403
proc toHSlice*(h: int): HSlice[int, int] = h .. h
395404

405+
406+
proc toSet*(x: set[range[0..65535]]): set[range[0..65535]] = x
407+
proc toSet*(x: int): set[range[0..65535]] = {x}
408+
proc toSet*(x: Slice[int]): set[range[0..65535]] =
409+
for y in x:
410+
result.incl y
411+
proc toSet*(x: seq[Slice[int]]): set[range[0..65535]] =
412+
for s in x:
413+
result.incl s.toSet()
414+
415+
396416
template animateCode*(lines: string, body: untyped) =
397417
newNbCodeBlock("animateCode", body):
398418
nb.blk.context["highlightLines"] = lines
399419
captureStdout(nb.blk.output):
400420
body
401421

402-
template animateCode*(lines: varargs[seq[HSlice[int, int]]], body: untyped) =
422+
template animateCode*(lines: varargs[set[range[0..65535]], toSet], body: untyped) =
403423
## Shows code and its output just like nbCode, but highlights different lines of the code in the order specified in `lines`.
404-
## lines: Specify which lines to highlight and in which order. (Must be specified as a seq[HSlice])
424+
## lines: Specify which lines to highlight and in which order. The lines can be specified using either:
425+
## - An `int` (highlight single line)
426+
## - A slice `x..y` (highlight a range of consequative lines)
427+
## - A set {x, y..z} (highlight any combination of lines)
405428
## Ex:
406429
## ```nim
407-
## animateCode(@[1..1], @[3..4, 6..6]): body
430+
## animateCode(1, 2..3, {4, 6}): body
408431
## ```
409-
## This will first highlight line 1, then lines 3, 4 and 6.
432+
## This will first highlight line 1, then lines 2 and 3, and lastly line 4 and 6.
410433
newNbCodeBlock("animateCode", body):
411434
var linesString: string
412435
if lines.len > 0:
413436
linesString &= "|"
414437
for lineBundle in lines:
415438
for line in lineBundle:
416-
linesString &= $line.a & "-" & $line.b & ","
439+
linesString &= $line & ","
417440
linesString &= "|"
418441
if lines.len > 0:
419442
linesString = linesString[0 .. ^3]
420443
nb.blk.context["highlightLines"] = linesString
421444
captureStdout(nb.blk.output):
422445
body
423446

424-
template animateCode*(lines: varargs[HSlice[int, int], toHSlice], body: untyped) =
425-
## Shows code and its output just like nbCode, but highlights different lines of the code in the order specified in `lines`.
426-
## lines: Specify which lines to highlight and in which order. (Must be specified as a HSlice)
427-
## Ex:
428-
## ```nim
429-
## animateCode(1..1, 2..3, 5..5, 4..4): body
430-
## ```
431-
## This will first highlight line 1, then lines 2 and 3, then line 5 and last line 4.
432-
var s: seq[seq[HSlice[int, int]]]
433-
for line in lines:
434-
s.add @[line]
435-
animateCode(s):
436-
body
437-
438447
template newAnimateCodeBlock*(cmd: untyped, impl: untyped) =
439-
template `cmd`*(lines: varargs[seq[HSlice[int, int]]], body: untyped) =
440-
newNbCodeBlock(cmd.astToStr, body):
448+
const cmdStr = astToStr(cmd)
449+
450+
template `cmd`*(lines: varargs[set[range[0..65535]], toSet], body: untyped) =
451+
newNbCodeBlock(cmdStr, body):
441452
var linesString: string
442453
if lines.len > 0:
443454
linesString &= "|"
444455
for lineBundle in lines:
445456
for line in lineBundle:
446-
linesString &= $line.a & "-" & $line.b & ","
457+
linesString &= $line & ","
447458
linesString &= "|"
448459
if lines.len > 0:
449460
linesString = linesString[0 .. ^3]
450461
nb.blk.context["highlightLines"] = linesString
451462
impl(body)
452463

453-
template `cmd`*(lines: varargs[HSlice[int, int], toHSlice], body: untyped) =
454-
var s: seq[seq[HSlice[int, int]]]
455-
for line in lines:
456-
s.add @[line]
457-
`cmd`(s):
458-
body
459-
460-
nb.partials[cmd.astToStr] = nb.partials["animateCode"]
461-
nb.renderPlans[cmd.astToStr] = nb.renderPlans["animateCode"]
464+
nb.partials[cmdStr] = nb.partials["animateCode"]
465+
nb.renderPlans[cmdStr] = nb.renderPlans["animateCode"]
462466

463467
template typewriter*(textMessage: string, typeSpeed = 50, alignment = "center") =
464468
let localText = textMessage

0 commit comments

Comments
 (0)