diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index da89ee31..fcf52092 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,4 +13,5 @@ jobs: with: nim-version: '1.6.x' - run: nimble -y install + - run: nimble docsdeps - run: nimble test diff --git a/docsrc/caesar.nim b/docsrc/caesar.nim new file mode 100644 index 00000000..f824ef10 --- /dev/null +++ b/docsrc/caesar.nim @@ -0,0 +1,61 @@ +import nimib + +nbInit + + +nbKaraxCode: + import std / [strutils, math] + + proc encryptChar(c: char, shift: int): char = + let c_normalized = c.ord - 'a'.ord # a is 0, z is 25 + var c_encrypted = euclMod(c_normalized + shift, 26) + 'a'.ord + result = c_encrypted.char + + proc encryptString(s: string, shift: int): string = + for c in s: + if c in 'a' .. 'z': + result.add encryptChar(c, shift) + else: + result.add c + + var cipherText, plainText: string + let ciphertextId = "ciphertext" + let plaintextId = "plaintext" + let shiftSliderId = "shiftSlider" + let encodeButtonId = "encodeButton" + let decodeButtonId = "decodeButton" + var shiftString = "3" + karaxHtml: + label: + text "Plaintext" + textarea(id = plaintextId, placeholder = "You can encrypt this message or you can try to decrypt the message below...") + hr() + label: + text "Ciphertext" + textarea(id = ciphertextId): + text "oek vekdt jxu iushuj auo! weet meha! dem oek sqd uqj q squiqh iqbqt qi q fhypu, okcco!" + hr() + label: + text "Shift/Key: " & shiftString + input(`type` = "range", min = "0", max = "25", value = "3", id = shiftSliderId): + proc oninput() = + let slider = getVNodeById(shiftSliderId) + shiftString = $slider.getInputText + button(id = encodeButtonId): + text "Encrypt" + proc onClick() = + let shift = ($getVNodeById(shiftSliderId).getInputText).parseInt + let plaintext = ($getVNodeById(plaintextId).getInputText).toLower + let ciphertext = encryptString(plaintext, shift) + getVNodeById(ciphertextId).setInputText ciphertext + button(id = decodeButtonId): + text "Decrypt" + proc onClick() = + let shift = ($getVNodeById(shiftSliderId).getInputText).parseInt + let ciphertext = ($getVNodeById(ciphertextId).getInputText).toLower + let plaintext = encryptString(ciphertext, -shift) # encrypt with -shift to decrypt + getVNodeById(plaintextId).setInputText plainText + + + +nbSave \ No newline at end of file diff --git a/docsrc/counters.nim b/docsrc/counters.nim new file mode 100644 index 00000000..c3a74db7 --- /dev/null +++ b/docsrc/counters.nim @@ -0,0 +1,125 @@ +import std / [strutils] +import nimib + +nbInit + +nbText: hlMd""" +# Counters - Creating reusable widgets + +This document will show you how to create reusable widgets using `nbCodeToJs`. Specifically we will make a counter: +A button which increases a counter each time you click it. We will do this in two different ways, using `std/dom` and `karax`. +## std/dom + +The first method is to use Nim like you would have used Javascript using `getElementById` and `addEventListener`: +""" +nbCode: + ## 1: + template counterButton(id: string) = + let labelId = "label-" & id + let buttonId = "button-" & id + ## 2: + nbRawOutput: """ + + +""" % [labelId, buttonId] + ## 3: + nbCodeToJs(labelId, buttonId): + import std/dom + ## 4: + var label = getElementById(labelId.cstring) + var button = getElementById(buttonId.cstring) + ## 5: + var counter: int = 0 + button.addEventListener("click", + proc (ev: Event) = + counter += 1 + label.innerHtml = ($counter).cstring + ) + +nbText: hlMd""" +Let's explain each part of the code: +1. We define a template called `counterButton` which will create a new counter button. So if you call it somewhere it will +place the widget there, that's the reusable part done. But it also takes an input `id: string`. This is to solve the problem of each widget needing unique ids. It can also be done with `nb.newId` as will be used in the Karax example. +2. Here we emit the `