From 090c8c50aa79b32013a8790c676067105a55fbe0 Mon Sep 17 00:00:00 2001 From: Pietro Peterlongo Date: Fri, 24 Feb 2023 17:04:09 +0100 Subject: [PATCH] Fix toc render bug, miss closing tags, fix #73 (#75) --- .gitignore | 13 ++++++++++--- src/nimibook.nim | 4 ++-- src/nimibook/themes.nim | 4 ++-- src/nimibook/{tocs.nim => toc_dsl.nim} | 2 +- src/nimibook/{renders.nim => toc_render.nim} | 3 ++- src/readme.md | 4 ++-- tests/ttocs.nim | 17 +++++++++++++++-- 7 files changed, 34 insertions(+), 13 deletions(-) rename src/nimibook/{tocs.nim => toc_dsl.nim} (97%) rename src/nimibook/{renders.nim => toc_render.nim} (94%) diff --git a/.gitignore b/.gitignore index 917ceae..80f3556 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,13 @@ +# ignore all executables (both windows and linux): +* +!/**/ +!*.* +*.exe + +# ignore build outputs docs/* examplebook/my* -nbook -nbook.exe -x_* *.log + +# useful to create invisible stuff and play around (for x_devnotes.md for x_issue73.nim, ...) +x_* \ No newline at end of file diff --git a/src/nimibook.nim b/src/nimibook.nim index e07e15f..d29c45a 100644 --- a/src/nimibook.nim +++ b/src/nimibook.nim @@ -1,8 +1,8 @@ import std / [os, parseopt, sequtils] export os, parseopt -import nimibook / [types, renders, tocs, builds, defaults, themes, commands, configs] -export types, renders, tocs, builds, defaults, themes, commands +import nimibook / [types, toc_render, toc_dsl, builds, defaults, themes, commands, configs] +export types, toc_render, toc_dsl, builds, defaults, themes, commands import nimib / paths diff --git a/src/nimibook/themes.nim b/src/nimibook/themes.nim index 587231c..791f634 100644 --- a/src/nimibook/themes.nim +++ b/src/nimibook/themes.nim @@ -1,6 +1,6 @@ import std / [strutils, os, enumerate, pathnorm] -import nimib, nimib / [themes] -import nimibook / [types, commands, entries, renders] +import nimib, nimib / themes +import nimibook / [types, commands, entries, toc_render] const document* = hlHtml""" diff --git a/src/nimibook/tocs.nim b/src/nimibook/toc_dsl.nim similarity index 97% rename from src/nimibook/tocs.nim rename to src/nimibook/toc_dsl.nim index 1c9b74d..a67b41a 100644 --- a/src/nimibook/tocs.nim +++ b/src/nimibook/toc_dsl.nim @@ -37,7 +37,7 @@ template section*(label, rfile: string, sectionBody: untyped) = discard pop folders inc levels -proc renderToc*(book: Book): string = +proc showToc*(book: Book): string = result.add "S O == Table Of Contents == (S: Source, O: Output)\n" for e in book.toc.entries: result.add book.renderLine(e) & "\n" diff --git a/src/nimibook/renders.nim b/src/nimibook/toc_render.nim similarity index 94% rename from src/nimibook/renders.nim rename to src/nimibook/toc_render.nim index 4ceebeb..5583ee4 100644 --- a/src/nimibook/renders.nim +++ b/src/nimibook/toc_render.nim @@ -55,7 +55,8 @@ proc render*(toc: Toc): string = elif len(e.levels) > previousLevel: result.add openSection() else: - result.add closeSection() + for _ in 1 .. (previousLevel - len(e.levels)): + result.add closeSection() result.add addEntryImpl(e) previousLevel = len(e.levels) diff --git a/src/readme.md b/src/readme.md index 61e8554..f21dab4 100644 --- a/src/readme.md +++ b/src/readme.md @@ -23,9 +23,9 @@ Summary content of the various files: - imports and exports for public api - CLI parser - `src\nimibook\types.nim`: the types (`Book`, `Toc`, `Entry`) -- `src\nimibook\tocs.nim`: template DSL to specify a TOC (and generate a `Book` object) +- `src\nimibook\toc_dsl.nim`: template DSL to specify a TOC (and generate a `Book` object) - `src\nimibook\entries.nim`: utility functions for `Entry` object (an entry is a element of the Toc, aka chapter) -- `src\nimibook\renders.nim`: function to render `Toc` object as a mustache partial (to be used in every page) +- `src\nimibook\toc_render.nim`: function to render `Toc` object as a mustache partial (to be used in every page) - `src\nimibook\theme.nim`: - implements `useNimibook` theme function to be used in every page - contains `document` partial (the mustache template for every page) diff --git a/tests/ttocs.nim b/tests/ttocs.nim index e0c7cd5..dda92fb 100644 --- a/tests/ttocs.nim +++ b/tests/ttocs.nim @@ -1,5 +1,6 @@ import unittest -import nimibook / [tocs, entries, types] +import std / strutils +import nimibook / [toc_dsl, toc_render, entries, types] test "toc dsl": var book: Book @@ -16,8 +17,20 @@ test "toc dsl": draft("and I have not written this yet") entry("Appendix", "appendix.md", numbered = false) - echo book.renderToc + echo book.showToc check len(book.toc.entries) == 11 check book.toc.entries[0].url == "index.html" check book.toc.entries[1].url == "part1/index.html" check book.toc.entries[2].url == "part1/important.html" + +test "toc render": # issue 73 + let myToc = initToc: + entry("Should Be 1.", "book/bla.nim") + section("Should be 2", "book/internals.nim"): + section("Should be 2.1", "internals/adders.nim"): + entry("Should be 2.1.1", "adders/one_adder.nim") + + entry("Should be 3.", "CONTRIBUTING.md") + let renderedToc = myToc.render + check renderedToc.count("") + check renderedToc.count("") \ No newline at end of file