-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nimibook docs + 2 fixes + 1 feature (cfg) + 1 refactor #18
Changes from all commits
d38b3a1
5354f4b
f178707
5efe688
e3e6bf1
7cd7af7
461d9ed
5a20022
99cdfbb
254e86a
663e4a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
!docs/fonts | ||
!docs/js | ||
docs/ | ||
book/toc.json | ||
book/book.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,52 @@ | ||
# nimibook | ||
|
||
A port of [mdbook](https://rust-lang.github.io/mdBook/index.html) | ||
to [nim](https://nim-lang.org/), | ||
powered by [nimib](https://pietroppeter.github.io/nimib/). | ||
|
||
Currently a PoC with minimal functionality hacked together to support [sciNim/getting-started](https://github.com/SciNim/getting-started). | ||
|
||
# Usage | ||
|
||
* 1) Write your content using [nimib](https://pietroppeter.github.io/nimib/) in the ``book`` folder. | ||
|
||
* 2) Use the Toc DSL to link chapters to Nim file in ``genbook.nim``. | ||
|
||
* 3) Generate your books in the ``docs`` folder using ``nimble genbook``. | ||
|
||
<!-- index.hbs adapted from mdbook to mustache | ||
required fields: | ||
- language | ||
- default_theme | ||
- description | ||
- path_to_root | ||
- preferred_dark_theme | ||
- theme_option (light, rust, coal, navy, ayu) | ||
- book_title | ||
optional stuff: | ||
- is_print | ||
- base_url | ||
- favicon_svg/favicon_png | ||
- print_enable | ||
- additional_css | ||
- mathjax_support | ||
- search_enable | ||
- git_repository_url (git_repository_icon) | ||
- git_repository_edit_url | ||
- previous (link) | ||
- next (link) | ||
- livereload | ||
- google_analytics | ||
- playground_line_numbers | ||
- playground_copyable | ||
- playground_js | ||
- search_js | ||
- additional_js | ||
partials: | ||
- head | ||
- header | ||
- toc | ||
assets required: | ||
- css/variables.css | ||
- css/general.css | ||
- css/chrome.css | ||
- FontAwesome/css/font-awesome.css | ||
- highlight.css | ||
- tomorrow-night.css | ||
- ayu-highlight.css | ||
- clipboard.min.js | ||
- highlight.js | ||
- book.js | ||
**nimibook** is a port of [mdbook] to [Nim], powered by [nimib]. | ||
Nimibook allows to create a nice looking book from nim code and markdown, | ||
making sure that nim code is running correctly and being able to incorporate code outputs in the final book. | ||
An example book is [nimibook documentation][nimibook] itself. | ||
|
||
[mdBook] is a command line tool and Rust crate to create books | ||
using Markdown (as by the CommonMark specification) files. | ||
It's very similar to [Gitbook], which is a command line tool (and Node.js library) | ||
for building beautiful books using GitHub/Git and Markdown (or AsciiDoc). | ||
|
||
[nimib] is a Nim library to convert your Nim code and its outputs to html documents. | ||
The html output can be easily customized thanks to [nim-mustache], | ||
and this is what allows it to use it to build nimibook. | ||
_nimib documents_ are normal nim files which use nimib library to produce an output document. | ||
|
||
The Markdown dialect supported by both nimib and nimibook is the subset of [Github Flavored Markdown][GFM] | ||
provided by [nim-markdown]. For a quick reference of supported syntax see the [cheatsheet]. | ||
|
||
### Status | ||
|
||
nimibook currently provides minimal functionality to create a book, notably missing is | ||
a command line interface to automate common task (e.g. initialization of a new nimibook). | ||
|
||
## Installation | ||
|
||
To be able to use nimibook, currently the only way is to recreate the same content of the repository | ||
and adjust it to your content. You can do that using the template feature from github | ||
or with a checkout of the repo and copying and pasting. | ||
|
||
Discussion of further options is ongoing in [issue #12](https://github.com/pietroppeter/nimibook/issues/12). | ||
|
||
## Usage | ||
|
||
1. Write your content using [nimib] or simple markdown files in the ``book`` folder. | ||
2. Use the Toc DSL to link chapters to content in ``genbook.nim``. | ||
3. Generate your books in the ``docs`` folder using ``nimble genbook``. | ||
|
||
<!--refs--> | ||
[mdbook]: https://rust-lang.github.io/mdBook/index.html | ||
[Nim]: https://nim-lang.org/ | ||
[nimib]: https://pietroppeter.github.io/nimib/ | ||
[Gitbook]: https://github.com/GitbookIO/gitbook | ||
[nim-mustache]: https://github.com/soasme/nim-mustache | ||
[nimibook]: https://pietroppeter.github.io/nimibook/ | ||
[GFM]: https://github.github.com/gfm/ | ||
[nim-markdown]: https://github.com/soasme/nim-markdown | ||
|
||
<!--SKIP | ||
All content before this sign is replicated in the Introduction chapter of nimibook documentation | ||
--> |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import nimib, nimibook | ||
import std / [os, strutils, strscans, strformat] | ||
|
||
nbInit | ||
nbUseNimibook | ||
|
||
# the following is very dependent on how source is written | ||
proc readBookConfigFields: seq[string] = | ||
# current folder is docs | ||
let src = "../src/nimibook/types.nim" | ||
var process = false | ||
var field, typ, description: string | ||
for line in src.lines: | ||
# debugEcho line | ||
if line.strip.startsWith("Book"): | ||
# debugEcho ">start processing" | ||
process = true | ||
continue | ||
if line.strip.startsWith("#"): | ||
# debugEcho ">skip" | ||
continue | ||
if line.strip.startsWith("toc"): | ||
# debugEcho ">stop processing" | ||
break | ||
if process and line.contains("##"): | ||
# debugEcho ">match" | ||
description = line.split("##")[1].strip | ||
field = line.split("##")[0].split("*:")[0].strip | ||
typ = line.split("##")[0].split("*:")[1].strip | ||
result.add fmt"* **{field}** (`{typ}`): {description}" | ||
# else: debugEcho ">noMatch" | ||
|
||
let fieldList = readBookConfigFields().join("\n") | ||
|
||
nbText: fmt""" | ||
# Configuration | ||
|
||
Book configuration is done with the `Book` object that has been created with `newBookFromToc`. | ||
|
||
The `Book` object has the following fields which are relevant for the documentation: | ||
|
||
{fieldList} | ||
|
||
Notes: | ||
|
||
* for consistency with template values, we use snake case for fields of this object. | ||
* the book object replicates functionalities available in mdbook | ||
* relevant documentation for mdbook is in this two pages: | ||
- <https://rust-lang.github.io/mdBook/format/theme/index-hbs.html> | ||
- <https://rust-lang.github.io/mdBook/format/config.html> | ||
* if not indicated otherwise, these fields are present in `document.mustache` and they are directly adapted from `index.hbs` | ||
* documentation comments above come directly from mdbook documentation (unless otherwise stated) | ||
""" | ||
nbSave |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import nimib, nimibook | ||
nbInit | ||
nbUseNimibook | ||
|
||
nbText: """ | ||
# Content | ||
|
||
The content of a chapter of a book can be a standard Markdown file or a nim file. | ||
|
||
For the nim file the structure is the following: | ||
|
||
```nim | ||
import nimib, nimibook | ||
... # other imports | ||
|
||
nbInit # initializes a nimib document | ||
nbUseNimibook # overrides nimib defaults with nimibooks and loads book configuration | ||
|
||
... # content of chapter using nbText, nbCode and other nimib functionalities | ||
|
||
nbSave # save the document to html output | ||
``` | ||
""" # todo: highlight nim code | ||
nbSave |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
include nimibook / prelude | ||
nbText: """# Introduction | ||
import nimib, nimibook | ||
import strutils | ||
nbInit | ||
nbUseNimibook # overrides nimib defaults with nimibooks and loads book configuration | ||
|
||
I should say something very wise about this book. | ||
proc readFileUntil(filename: string, text: string): string = | ||
for line in filename.lines: | ||
if line.startsWith(text): | ||
return result | ||
result &= line & '\n' | ||
|
||
Instead I will just post some nim code: | ||
""" | ||
nbCode: | ||
echo "hello mdbook!" | ||
nbText: "../README.md".readFileUntil("<!--SKIP") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it a good idea to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see remarks above, but also reading README I think is rather harmless. I was planning to have distinct contents only for README.md and index.nim but in the end I did not use that feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, alright; for a static Readme file it's fair game. |
||
nbSave |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import nimib, nimibook | ||
import osproc, sugar, strutils, strformat | ||
|
||
nbInit | ||
nbUseNimibook | ||
|
||
var tasks: string | ||
withDir("..".AbsoluteDir): | ||
tasks = execProcess("nimble", args=["tasks"], options={poUsePath}) | ||
|
||
let taskList = collect(newSeq): | ||
for line in tasks.strip.splitLines: | ||
"* `nimble " & line.replace(" ", "`: ") | ||
|
||
nbText: fmt""" | ||
# Tasks | ||
|
||
{taskList.join("\n")} | ||
""" | ||
nbSave |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import nimib, nimibook | ||
nbInit | ||
nbUseNimibook | ||
|
||
nbText: """ | ||
# Table of Contents DSL | ||
|
||
The table of content supports (numbered) chapters (`entry`), | ||
sections (`section`) and draft chapters (`draft`, not numbered). | ||
|
||
Here is an example on how it looks like | ||
```nim | ||
import nimibook, os # os is used inside Toc DSL | ||
|
||
let book = newBookFromToc("Example book", "book"): | ||
entry("Introduction", "index") # .nim extension is optional | ||
entry("Pure Markdown", "pure.md") # for md files you need to use extension | ||
# every entry in this section will have a path relative to basics | ||
section("Basics", "basics/index.nim"): | ||
section("Plotting", "plotting.nim"): # nested sections | ||
entry("Data Manipulation", "data_manipulation") | ||
entry("Models", "models") | ||
draft("Contributors", "misc/but/very/far/contributors") | ||
``` | ||
""" # todo: highlight nim code | ||
nbSave |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Back to parent section |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Draft chapter |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Example toc structure |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Nested section |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Entry in nested section |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
import nimibook, os | ||
import nimibook, os # os is used inside Toc DSL | ||
|
||
let toc = newToc("Example", "book"): | ||
entry("Introduction", "index") | ||
entry("Pure Markdown", "pure.md") | ||
# .nim extension is optionnal but you can add it if you want to | ||
section("Basics", "basics/index.nim"): | ||
section("Plotting", "plotting.nim"): | ||
entry("Data Manipulation", "data_manipulation") | ||
entry("Models", "models") | ||
draft("Contributors", "misc/but/very/far/contributors") | ||
var book = newBookFromToc("nimibook", "book"): | ||
section("Introduction", "index"): # .nim extension is optional | ||
entry("Content", "content") | ||
entry("Toc DSL", "tocdsl") | ||
entry("Configuration", "configuration") | ||
entry("Tasks", "tasks") | ||
section("Example toc structure", "tocexample/index.md"): | ||
section("Nested section", "nested.md"): | ||
entry("Entry in nested section", "nested_entry.md") | ||
entry("Back to parent section", "back_to_parent.md") | ||
draft("Draft chapter", "draft.md") | ||
|
||
when defined(printToc): | ||
import print | ||
print toc | ||
elif defined(dumpToc): | ||
dump toc | ||
elif defined(cleanToc): | ||
clean toc | ||
else: | ||
publish toc | ||
book.git_repository_url = "https://github.com/pietroppeter/nimibook" | ||
nbBookTasks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I misunderstood something, why does the book rely upon parsing a file in
src
?I think we should keep
book
andsrc
separate as much as possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also relative path based on "../" will break with Nimble; but that may be something to fix later on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also confused about the src/book thing until I realized the file wasn't some kind of configuration file butit rather just one of the pages in the book which did some parsing of the doc-comments in src. 🤦♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
book
being the documentation of what happens insrc
, I think it is fair to makebook
depend onsrc
to avoid repeating stuff and keep documentation up to date. The moment you add a new field toBook
object implementing another feature you will get the corresponding documentation. For sure we should avoid havingsrc
depend onbook
folder.book
folder should be forgotten when installed by nimble so the relative paths there should not matter.Regarding the implementation, it is indeed a bit hacky (main drawback being that how you add and document fields in
Book
object impacts the documentation), but I think it is also rather straightforward. Other options I had in mind for implementation of this autodocumenting configurations were:nim jsondoc ...
and parse json file (will also depend directly on src)macros.getImpl
and parse AST (this depends only on import nimibook, but since we will not have nimibook as a dependency here, it will still ultimately depend on src)The only option to not depend on
src
folder is to duplicate documentation in code andbook
and I would prefer to avoid this unless there are specific issues.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Options 2 was what I had in mind.
Use Nimibook as a library and parse the AST to obtain information but I understand your reasoning.
I think in the long run we need a macros so that we can extract information from the AST for using Nimibook as a API documentation generator (replacement of Nim doc).
Macros based will also allow us to get runnableExamples
But it's a rather complex feature so maybe I think we should merge this first and improve upon it later.