From 9f7e87e9356b8802e4bb13fc6555088468fd3d7f Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Sat, 8 Oct 2016 20:57:53 +0200 Subject: [PATCH] Ordered snippets with file system hierarchy --- doc/examples/quickstart.json | 43 ------- doc/index.txt | 5 +- doc/rstPreproc.nim | 119 +++++++++--------- .../quickstart/01/code.nim} | 0 .../quickstart/01/out.yaml} | 0 doc/snippets/quickstart/01/title | 1 + .../quickstart/02/code.nim} | 0 .../quickstart/02/in.yaml} | 0 doc/snippets/quickstart/02/title | 1 + .../quickstart/03/code.nim} | 0 .../quickstart/03/out.yaml} | 0 doc/snippets/quickstart/03/title | 1 + .../quickstart/04/code.nim} | 0 .../quickstart/04/out.yaml} | 0 doc/snippets/quickstart/04/title | 1 + .../quickstart/05/code.nim} | 0 .../quickstart/05/in.yaml} | 0 doc/snippets/quickstart/05/title | 1 + .../quickstart/06/code.nim} | 0 .../quickstart/06/out.yaml} | 0 doc/snippets/quickstart/06/title | 1 + .../quickstart/07/code.nim} | 0 .../quickstart/07/out.yaml} | 0 doc/snippets/quickstart/07/title | 1 + .../quickstart/08/code.nim} | 0 .../quickstart/08/in.yaml} | 0 doc/snippets/quickstart/08/title | 1 + .../quickstart/09/01/code.nim} | 0 .../quickstart/09/01/in.yaml} | 0 doc/snippets/quickstart/09/01/title | 1 + .../quickstart/09/02/code.nim} | 0 .../quickstart/09/02/in.yaml} | 0 doc/snippets/quickstart/09/02/title | 1 + doc/snippets/quickstart/09/title | 1 + doc/snippets/quickstart/title | 1 + yaml/serialization.nim | 44 ++++++- yaml/taglib.nim | 26 ---- 37 files changed, 116 insertions(+), 133 deletions(-) delete mode 100644 doc/examples/quickstart.json rename doc/{examples/dump-yaml.nim => snippets/quickstart/01/code.nim} (100%) rename doc/{examples/dump-yaml.out.yaml => snippets/quickstart/01/out.yaml} (100%) create mode 100644 doc/snippets/quickstart/01/title rename doc/{examples/load-yaml.nim => snippets/quickstart/02/code.nim} (100%) rename doc/{examples/load-yaml.in.yaml => snippets/quickstart/02/in.yaml} (100%) create mode 100644 doc/snippets/quickstart/02/title rename doc/{examples/outputstyle.nim => snippets/quickstart/03/code.nim} (100%) rename doc/{examples/outputstyle.out.yaml => snippets/quickstart/03/out.yaml} (100%) create mode 100644 doc/snippets/quickstart/03/title rename doc/{examples/dump-reftypes.nim => snippets/quickstart/04/code.nim} (100%) rename doc/{examples/dump-reftypes.out.yaml => snippets/quickstart/04/out.yaml} (100%) create mode 100644 doc/snippets/quickstart/04/title rename doc/{examples/load-reftypes.nim => snippets/quickstart/05/code.nim} (100%) rename doc/{examples/load-reftypes.in.yaml => snippets/quickstart/05/in.yaml} (100%) create mode 100644 doc/snippets/quickstart/05/title rename doc/{examples/customtag.nim => snippets/quickstart/06/code.nim} (100%) rename doc/{examples/customtag.out.yaml => snippets/quickstart/06/out.yaml} (100%) create mode 100644 doc/snippets/quickstart/06/title rename doc/{examples/dump-json.nim => snippets/quickstart/07/code.nim} (100%) rename doc/{examples/dump-json.out.yaml => snippets/quickstart/07/out.yaml} (100%) create mode 100644 doc/snippets/quickstart/07/title rename doc/{examples/load-json.nim => snippets/quickstart/08/code.nim} (100%) rename doc/{examples/load-json.in.yaml => snippets/quickstart/08/in.yaml} (100%) create mode 100644 doc/snippets/quickstart/08/title rename doc/{examples/implicit-variant.nim => snippets/quickstart/09/01/code.nim} (100%) rename doc/{examples/implicit-variant.in.yaml => snippets/quickstart/09/01/in.yaml} (100%) create mode 100644 doc/snippets/quickstart/09/01/title rename doc/{examples/sequential-api.nim => snippets/quickstart/09/02/code.nim} (100%) rename doc/{examples/sequential-api.in.yaml => snippets/quickstart/09/02/in.yaml} (100%) create mode 100644 doc/snippets/quickstart/09/02/title create mode 100644 doc/snippets/quickstart/09/title create mode 100644 doc/snippets/quickstart/title diff --git a/doc/examples/quickstart.json b/doc/examples/quickstart.json deleted file mode 100644 index b8efe1c..0000000 --- a/doc/examples/quickstart.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "Dumping Nim objects as YAML": [ - "dump-yaml", "out" - ], - - "Loading Nim objects from YAML": [ - "load-yaml", "in" - ], - - "Customizing output style" : [ - "outputstyle", "out" - ], - - "Dumping reference types and cyclic structures": [ - "dump-reftypes", "out" - ], - - "Loading reference types and cyclic structures": [ - "load-reftypes", "in" - ], - - "Defining a custom tag uri for a type": [ - "customtag", "out" - ], - - "Dumping Nim objects as JSON": [ - "dump-json", "out" - ], - - "Loading Nim objects from JSON": [ - "load-json", "in" - ], - - "Processing a Sequence of Heterogeneous Items": { - "… With variant objects": [ - "implicit-variant", "in" - ], - - "… With the Sequential API": [ - "sequential-api", "in" - ] - } -} \ No newline at end of file diff --git a/doc/index.txt b/doc/index.txt index 3bd71cd..d1a7498 100644 --- a/doc/index.txt +++ b/doc/index.txt @@ -16,7 +16,4 @@ install it with `Nimble `_: .. code-block:: bash nimble install yaml -Quickstart -========== - -%examples/quickstart.json%/ \ No newline at end of file +%quickstart%0 \ No newline at end of file diff --git a/doc/rstPreproc.nim b/doc/rstPreproc.nim index 6b92460..22522eb 100644 --- a/doc/rstPreproc.nim +++ b/doc/rstPreproc.nim @@ -1,9 +1,10 @@ ## This is a tool for preprocessing rst files. Lines starting with ``%`` will -## get substituted by nicely layouted included nim and yaml code. +## get substituted by nicely layouted nim and yaml code included from file in +## the snippets tree. ## -## The syntax of substituted lines is ``'%' jsonfile '%' jsonpath``. *jsonfile* -## shall be the path to a JSON file. *jsonpath* shall be a path to some node in -## that JSON file. +## The syntax of substituted lines is ``'%' path '%' level``. *path* shall be +## a path relative to the *snippets* directory. *level* shall be the level depth +## of the first title that should be produced. ## ## Usage: ## @@ -11,8 +12,12 @@ ## ## *path* is the output path. If omitted, it will be equal to infile with its ## suffix substituted by ``.rst``. *infile* is the source rst file. +## +## The reason for this complex approach is to have all snippets used in the docs +## available as source files for automatic testing. This way, we can make sure +## that the code in the docs actually works. -import parseopt2, json, streams, tables, strutils, os +import parseopt2, streams, tables, strutils, os var infile = "" @@ -57,71 +62,73 @@ var tmpOut = newFileStream(path, fmWrite) proc append(s: string) = tmpOut.writeLine(s) -proc gotoPath(root: JsonNode, path: string): JsonNode = - doAssert path[0] == '/' - if path.len == 1: return root - doAssert root.kind == JObject - for i in 1..= headingChars.len: headingChars[^1] else: headingChars[level] - append(repeat(headingChar, key.len) & '\l') - outputExamples(value, prefix, level + 1) - of JArray: - let elems = node.getElems() - case elems.len - of 2: - append(".. raw:: html") - append(" ") - append(" \n
code.nim" & elems[1].getStr() & - ".yaml
\n") - append(".. code:: nim") - append(" :file: " & prefix & elems[0].getStr() & ".nim\n") - append(".. raw:: html") - append(" \n") - append(".. code:: yaml") - append(" :file: " & prefix & elems[0].getStr() & '.' & - elems[1].getStr() & ".yaml\n") - append(".. raw:: html") - append("
\n") - else: - echo "Unexpected number of elements in array: ", elems.len - quit 1 + append(title) + append(repeat(headingChar, title.len) & '\l') + + # process content files under this directory + + var codeFiles = newSeq[string]() + for kind, filePath in walkDir(curPath, true): + if kind == pcFile: + if filePath != "title": codeFiles.add(filePath) + case codeFiles.len + of 0: discard + of 1: + let (nullPath, name, extension) = codeFiles[0].splitFile() + append(".. code:: " & extension[1..^1]) + append(" :file: " & (curPath / codeFiles[0]) & '\l') + of 2: + append(".. raw:: html") + append(" ") + for codeFile in codeFiles: + append(" ") + append(" \n
" & codeFile & "
\n") + + var first = true + for codeFile in codeFiles: + if first: first = false + else: append(".. raw:: html\n \n") + let (nullPath, name, extension) = codeFile.splitFile() + append(".. code:: " & extension[1..^1]) + append(" :file: " & (curPath / codeFile) & '\l') + + append(".. raw:: html") + append("
\n") else: - echo "Unexpected node kind: ", node.kind - quit 1 + echo "Unexpected number of files in ", curPath, ": ", codeFiles.len + + # process child directories + + for kind, dirPath in walkDir(curPath): + if kind == pcDir: + outputExamples(dirPath, level + 1) var lineNum = 0 for line in infile.lines(): if line.len > 0 and line[0] == '%': var - jsonFile: string = nil - jsonPath: string = nil + srcPath: string = nil + level = 0 for i in 1..