Last active
September 11, 2016 11:24
-
-
Save stisa/27b9d2f4aed9c85d7501ea38e15d9865 to your computer and use it in GitHub Desktop.
Parse nim based jupyter notebook into a sort of literate nim, markdown goes into documentation blocks and code is kept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os,json,strutils | |
## TODO: | |
# - check input file type | |
# - a parser for markdown->html | |
let arguments = commandLineParams() # [0] should always be the input file | |
var nbf = arguments[0].readFile | |
var outfile :string | |
if arguments.len>1 and arguments[1]=="into": | |
outfile = arguments[2] | |
else: | |
outfile = "out.nim" | |
var nbjson = parseJson(nbf) # parse the notebook as json | |
var cnt : string = "" # content of the final file | |
proc jsonArrToStr(j:JsonNode):string # forward declaration, accumulate a JsonArray into a string | |
for cell in nbjson["cells"]: | |
var ct = cell["cell_type"].str | |
if (ct == "markdown"): | |
var src = cell["source"].jsonArrToStr | |
cnt&= ("\n##[" & src & "]##\n") | |
elif (ct=="code"): | |
var src = cell["source"].jsonArrToStr | |
cnt&=("\n"&src&"\n") | |
writefile(outfile, cnt) | |
proc jsonArrToStr(j:JsonNode):string = | |
result = "" | |
for e in j: | |
result&=e.str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment