Skip to content

Instantly share code, notes, and snippets.

Last active Sep 11, 2016
What would you like to do?
Parse nim based jupyter notebook into a sort of literate nim, markdown goes into documentation blocks and code is kept
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]
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
writefile(outfile, cnt)
proc jsonArrToStr(j:JsonNode):string =
result = ""
for e in j:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment