Skip to content

Instantly share code, notes, and snippets.

@stisa
Last active September 11, 2016 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stisa/27b9d2f4aed9c85d7501ea38e15d9865 to your computer and use it in GitHub Desktop.
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
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