|
| 1 | +module Jupyter2Pluto |
| 2 | +using JSON |
| 3 | +using UUIDs |
| 4 | + |
| 5 | +const _notebook_header = "### A Pluto.jl notebook ###" |
| 6 | +const _cell_id_delimiter = "# ╔═╡ " |
| 7 | +const _order_delimiter = "# ╠═" |
| 8 | +const _order_delimiter_folded = "# ╟─" |
| 9 | +const _cell_suffix = "\n\n" |
| 10 | + |
| 11 | +export convert_notebook |
| 12 | + |
| 13 | +abstract type JupyterCell end |
| 14 | + |
| 15 | +struct JupyterCodeCell <: JupyterCell |
| 16 | + content::Vector{String} |
| 17 | +end |
| 18 | +struct JupyterMarkdownCell <: JupyterCell |
| 19 | + content::String |
| 20 | +end |
| 21 | + |
| 22 | +abstract type PlutoCell end |
| 23 | + |
| 24 | +struct PlutoCodeCell <: PlutoCell |
| 25 | + cell_id::UUID |
| 26 | + content::String |
| 27 | +end |
| 28 | + |
| 29 | +struct PlutoBlockCodeCell <: PlutoCell |
| 30 | + cell_id::UUID |
| 31 | + content::String |
| 32 | +end |
| 33 | + |
| 34 | +struct PlutoMarkdownCell <: PlutoCell |
| 35 | + cell_id::UUID |
| 36 | + content::String |
| 37 | +end |
| 38 | + |
| 39 | +function Base.show(io::IO, cell::PlutoCodeCell) |
| 40 | + gl = """ |
| 41 | +$(_cell_id_delimiter)$(cell.cell_id) |
| 42 | +$(cell.content)$(_cell_suffix)""" |
| 43 | + print(io, gl) |
| 44 | +end |
| 45 | + |
| 46 | +function Base.show(io::IO, cell::PlutoBlockCodeCell) |
| 47 | + gl = """ |
| 48 | +$(_cell_id_delimiter)$(cell.cell_id) |
| 49 | +begin\n$(cell.content)\nend$(_cell_suffix)""" |
| 50 | + print(io, gl) |
| 51 | +end |
| 52 | + |
| 53 | + |
| 54 | +function Base.show(io::IO, cell::PlutoMarkdownCell) |
| 55 | + content = replace(cell.content, "\"" => "\\\"") |
| 56 | + r = """ |
| 57 | +$(_cell_id_delimiter)$(cell.cell_id) |
| 58 | +md\" |
| 59 | +$(content) |
| 60 | +\"$(_cell_suffix)""" |
| 61 | + print(io, r) |
| 62 | +end |
| 63 | + |
| 64 | +function cell_id() |
| 65 | + uuid4() |
| 66 | +end |
| 67 | + |
| 68 | +function generate_code(pcells::Vector{PlutoCell}) |
| 69 | + pheader = _notebook_header*"\n\n" |
| 70 | + codestring = "$pheader$(join(pcells))$(order_code(pcells))" |
| 71 | + return codestring |
| 72 | +end |
| 73 | + |
| 74 | +function order_code(pcells::Vector{PlutoCell}) |
| 75 | + orderstrprefix = "$(_cell_id_delimiter)Cell order:\n" |
| 76 | + orderstrprefix *join(order_code.(pcells)) |
| 77 | +end |
| 78 | + |
| 79 | +function order_code(pcell::PlutoMarkdownCell) |
| 80 | + return _order_delimiter_folded*""*string(pcell.cell_id)*"\n" |
| 81 | +end |
| 82 | + |
| 83 | +function order_code(pcell::Union{PlutoCodeCell, PlutoBlockCodeCell}) |
| 84 | + return _order_delimiter*""*string(pcell.cell_id)*"\n" |
| 85 | +end |
| 86 | + |
| 87 | +function generate_plutocells(codecell::JupyterCodeCell) |
| 88 | + pcells = PlutoCell[] |
| 89 | + for content in codecell.content |
| 90 | + id = cell_id() |
| 91 | + push!(pcells, PlutoCodeCell(id, content)) |
| 92 | + end |
| 93 | + return pcells |
| 94 | +end |
| 95 | + |
| 96 | +function PlutoCell(codecell::JupyterCodeCell) |
| 97 | + id = cell_id() |
| 98 | + if has_multiple_expressions(codecell) |
| 99 | + return PlutoBlockCodeCell(id, join(codecell.content)) |
| 100 | + end |
| 101 | + return PlutoCodeCell(id, join(codecell.content)) |
| 102 | +end |
| 103 | + |
| 104 | +function PlutoCell(codecell::JupyterMarkdownCell) |
| 105 | + id = cell_id() |
| 106 | + return PlutoMarkdownCell(id, codecell.content) |
| 107 | +end |
| 108 | + |
| 109 | +function has_multiple_expressions(codecell::JupyterCodeCell) |
| 110 | + expressions = Meta.parse("begin "*join(codecell.content, "\n")*" end").args |
| 111 | + filter!(x -> !(x isa LineNumberNode), expressions) |
| 112 | + length(expressions) > 1 |
| 113 | +end |
| 114 | + |
| 115 | +function convert_file(jupyter_file) |
| 116 | + jupyter_cells = try |
| 117 | + content = JSON.parsefile(jupyter_file) |
| 118 | + content["cells"] |
| 119 | + catch ex |
| 120 | + error("Jupyter notebook parse error") |
| 121 | + end |
| 122 | + pluto_cells = PlutoCell[] |
| 123 | + for cell in jupyter_cells |
| 124 | + !haskey(cell, "cell_type") || !haskey(cell,"source") && continue |
| 125 | + if cell["cell_type"] == "markdown" && !isempty(cell["source"]) |
| 126 | + pc = PlutoCell(JupyterMarkdownCell(join(cell["source"]))) |
| 127 | + push!(pluto_cells, pc) |
| 128 | + end |
| 129 | + if cell["cell_type"] == "code" && !isempty(cell["source"]) |
| 130 | + pc = PlutoCell(JupyterCodeCell(cell["source"])) |
| 131 | + push!(pluto_cells, pc) |
| 132 | + end |
| 133 | + end |
| 134 | + output = generate_code(pluto_cells) |
| 135 | + dest = "$(jupyter_file).jl" |
| 136 | + open(dest, "w") do f |
| 137 | + write(f, output) |
| 138 | + end |
| 139 | + dest |
| 140 | +end |
| 141 | + |
| 142 | +function convert_notebook(file) |
| 143 | + convert_file(file) |
| 144 | +end |
| 145 | +end # module |
0 commit comments