-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_notebooks.jl
70 lines (61 loc) · 1.94 KB
/
convert_notebooks.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Adapted with minor changes from DataScienceTutorials.jl :
# https://github.com/JuliaAI/DataScienceTutorials.jl/blob/5e24796b3621722fefd49465dd0c902c835d68a7/deploy.jl
using Literate
function pre_process_script(io, s)
chunks = Literate.parse(Literate.FranklinFlavor(), s)
remove = Int[]
rx = r".*?#\s*?(?i)hideall.*?"
rx2 = r".*?#\s*?(?i)hide.*?" # note: superset, doesn't matter
for (i, c) in enumerate(chunks)
c isa Literate.CodeChunk || continue
remove_lines = Int[]
hideall = false
for (j, l) in enumerate(c.lines)
if match(rx, l) !== nothing
push!(remove, i)
hideall = true
break
end
if match(rx2, l) !== nothing
push!(remove_lines, j)
end
end
!hideall && deleteat!(c.lines, remove_lines)
end
deleteat!(chunks, remove)
for c in chunks
if c isa Literate.CodeChunk
for l in c.lines
println(io, l)
end
else
for l in c.lines
println(io, "# ", l.second)
end
end
println(io, "")
end
return
end
path = "./notebooks/"
for script in readdir("_literate")
temp_script = tempname()
open(temp_script, "w") do ts
s = read(joinpath("./_literate", script), String)
pre_process_script(ts, s)
end
script_name = String(first(split(script, ".")))
# Notebook
Literate.notebook(temp_script, path, name=script_name,
execute=false, documenter=false)
#=
# Unnecessary to generate these for now.
# Need to change `path` above if we want to store these too.
# Annotated script
Literate.script(temp_script, path, name=script_name,
keep_comments=true, documenter=false)
# Stripped script
Literate.script(temp_script, path, name=script_name,
keep_comments=false, documenter=false)
=#
end