forked from jdadavid/Jl2pluto.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plutojl2jl.jl
153 lines (119 loc) · 4.74 KB
/
Plutojl2jl.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Plutojl2jl.jl : Convert a Pluto notebook file to ordinary Julia file.
#
# Call by :
#
# julia Plutojl2jl.jl [-h] [-i] [-f] inputplutonb.jl [output.jl]
#
# If "output.jl" unspecified and no "-i", aborts.
# If "-i", do the conversion inplace (so, also assume "-f")
# If [no "-i and ] no "-f", do not overwrite existing "output.jl"
# If "-f", force write "output.jl", erasing it if already existing
#
# If "-k" keep one blank line between cells (else delete both two blank lines between cells)
# J.D.A.DAVID 04/01/2021
# Conception following asking of Rafael Guerra
# Note (04/01/2021) infrastructing for reading from stdin/ writing to stdout partielly in place / not yet working ...
# From https://github.com/fonsp/Pluto.jl/blob/master/src/notebook/Notebook.jl
const _notebook_header = "### A Pluto.jl notebook ###"
# We use a creative delimiter to avoid accidental use in code
# so don't get inspired to suddenly use these in your code!
const _cell_id_delimiter = "# ╔═╡ "
const _order_delimiter = "# ╠═"
const _order_delimiter_folded = "# ╟─"
const _cell_suffix = "\n\n"
# defined by JDAD
const _cell_order_delimiter = "# ╔═╡ Cell order:"
msghelp="""
Plutojl2jl.jl : Convert a Pluto notebook file to ordinary Julia file.
Call by :
julia Plutojl2jl.jl [-h] [-i] [-f] [-k] inputplutonb.jl [output.jl]
If "output.jl" unspecified and no "-i", aborts.
If "-i", do the conversion inplace (so, also assume "-f")
If [no "-i and ] no "-f", do not overwrite existing "output.jl"
If "-f", force write "output.jl", erasing it if already existing
If "-k" keep one blank line between cells (else delete both two blank lines between cells)
"""
mhelp() = (@info msghelp)
if length(ARGS) < 1; @info("plutojl2jl : No option nor inputfile, aborting\n"); mhelp(); exit(3); end
if ARGS[1] == "-h"; mhelp(); exit(0); end
inplace=false
enableoverwrite=false
if ARGS[1] == "-i"; inplace=true; popfirst!(ARGS) end
if length(ARGS) < 1; @info "plutojl2jl : No option nor inputfile, aborting"; mhelp(); exit(4); end
if ARGS[1] == "-f"; enableoverwrite=true; popfirst!(ARGS) end
if length(ARGS) < 1; @info "plutojl2jl : No inputfile, aborting"; mhelp(); exit(5); end
inputfile=ARGS[1]
if inputfile == "-" ; inputfile=stdin; end
if inputfile == "stdin"; inputfile=stdin; end
if inplace
if length(ARGS) > 1; @info "plutojl2jl : Inplace, so only input should be specified, aborting\n"; mhelp(); exit(6); end
if enableoverwrite; @info "plutojl2jl : -f (forced overwrite) redundant with Inplace\n"; end
outputfile=inputfile
else
if length(ARGS) > 2; @info "plutojl2jl : Only input and output should be specified, aborting\n"; mhelp(); exit(7); end
if length(ARGS) <2 ; @info "plutojl2jl : No output specified, aborting\n"; mhelp(); exit(8); end
outputfile=ARGS[2]
if outputfile == "-" ; inputfile=stdout; end
if outputfile == "stdin"; inputfile=stdout; end
if outputfile == inputfile && !enableoverwrite; @info "plutojl2jl : input and output are same -- no overwrite -- aborting\n"; exit(9); end
if outputfile == inputfile && enableoverwrite; @info "plutojl2jl : input and output are same -- overwrite ok -- doing inplace\n"; inplace=true; end
end
if !isfile(inputfile)
@info "plutojl2jl : inputfile $inputfile does not exists, aborting"
exit(10)
end
if filesize(inputfile) < 2
@info "plutojl2jl : inputfile $inputfile is empty, aborting"
exit(11)
end
lines=readlines(inputfile)
if lines[1] == _notebook_header
if lines[2][1:3] == "# v" && lines[3] ==""
# notebook OK
deleteat!(lines,[1,2,3])
else
@info "plutojl2jl : Malformed Pluto notebook (2nd/3rd lines notok), aborting."
exit(12)
end
else
@info "plutojl2jl : Not a Pluto noteboook, doing nothing.";
exit(0)
end
# Deletion of Cell Order lines (should be last lines in file)
numcods=findall(x -> x == _cell_order_delimiter, lines)
i=numcods[1]
deleteat!(lines,i:size(lines,1))
if lines[i-1] == "" && lines[i-2] == ""
deleteat!(lines,[i-2, i-1])
end
# Deletion of each cell marker begin, and possibly cell ends (empty) lines
numcids=reverse(findall(x -> startswith(x,_cell_id_delimiter), lines))
@show numcids
for i in numcids
deleteat!(lines,[i])
if lines[i-1] == "" && lines[i-2] == ""
if keepblank
# we keep one empty line
deleteat!(lines,i-1)
else
deleteat!(lines,[i-2, i-1])
end
end
end
if inplace
nothing
else
if isfile(outputfile)
if !enableoverwrite
@info "plutojl2jl : outputfile $outputfile already exists, no forced override, aborting"
exit(1)
else
@info "plutojl2jl : overwriting $(outputfile) as authorised"
end
end
end
iomode= outputfile == stdout ? "w+" : "w"
open(outputfile, iomode) do io
foreach(line -> println(io, line),lines)
end
@info "plutojl2jl : Julia ordinary file $outputfile written (from notebook $inputfile)."