Skip to content

Commit

Permalink
Fix toc render bug, miss closing tags, fix #73 (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
pietroppeter authored Feb 24, 2023
1 parent d9080d4 commit 090c8c5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
13 changes: 10 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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_*
4 changes: 2 additions & 2 deletions src/nimibook.nim
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/nimibook/themes.nim
Original file line number Diff line number Diff line change
@@ -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"""
<!DOCTYPE HTML>
Expand Down
2 changes: 1 addition & 1 deletion src/nimibook/tocs.nim → src/nimibook/toc_dsl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/nimibook/renders.nim → src/nimibook/toc_render.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions tests/ttocs.nim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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("<ol") == renderedToc.count("</ol>")
check renderedToc.count("<li") == renderedToc.count("</li>")

0 comments on commit 090c8c5

Please sign in to comment.