|
| 1 | +# This file contains a script to convert `*.ipynb` files |
| 2 | +# to Markdown files to further transfer to `Documenter` |
| 3 | + |
| 4 | +@info "ConvertScript: converting notebooks to markdown files." |
| 5 | + |
| 6 | +# Get path to the notebooks folder |
| 7 | +notebooks_folder_name = "notebooks" |
| 8 | +notebooks_folder = normpath(joinpath(@__DIR__, "..", notebooks_folder_name)) |
| 9 | + |
| 10 | +# Get paths to notebooks and their (base)names |
| 11 | +names = String[] |
| 12 | +notebooks = String[] |
| 13 | +for (root, dirs, files) in walkdir(notebooks_folder) |
| 14 | + for file in files |
| 15 | + endswith(file, ".ipynb") && push!(notebooks, joinpath(root, file)) |
| 16 | + end |
| 17 | +end |
| 18 | +names = replace.(basename.(notebooks), ".ipynb" => "") |
| 19 | + |
| 20 | +# Get path to the `make.jl` script |
| 21 | +make = joinpath(@__DIR__, "make.jl") |
| 22 | + |
| 23 | +# Go to the output directory |
| 24 | +generated_folder = joinpath(@__DIR__, "src", "generated") |
| 25 | +output_folder = joinpath(generated_folder, notebooks_folder_name) |
| 26 | +!isdir(generated_folder) && mkdir(generated_folder) |
| 27 | +!isdir(output_folder) && mkdir(output_folder) |
| 28 | +cd(output_folder) |
| 29 | + |
| 30 | +for (index, notebook) in enumerate(notebooks) |
| 31 | + |
| 32 | + # Get the name of the notebook |
| 33 | + name = names[index] |
| 34 | + |
| 35 | + # Go to the folder of the notebook |
| 36 | + !isdir(name) && mkdir(name) |
| 37 | + cd(name) |
| 38 | + |
| 39 | + # Construct the name of a Markdown file |
| 40 | + md = "$name.md" |
| 41 | + |
| 42 | + # Convert the notebook to a Markdown file |
| 43 | + output_cmd_part = ["--output-dir", ".", "--output", "$md", "--log-level", "WARN"] |
| 44 | + run(`jupyter nbconvert --to markdown $notebook $output_cmd_part`) |
| 45 | + |
| 46 | + # Get the content of the generated Markdown file |
| 47 | + lines = readlines("$md") |
| 48 | + |
| 49 | + # Initialize auxiliary variables |
| 50 | + inside_julia_block = false |
| 51 | + inside_output_block = false |
| 52 | + last_index = size(lines, 1) |
| 53 | + |
| 54 | + # Put the text output in the text block |
| 55 | + for (index, line) in enumerate(lines) |
| 56 | + |
| 57 | + if startswith(line, "```julia") && !inside_julia_block |
| 58 | + inside_julia_block = true |
| 59 | + if inside_output_block |
| 60 | + lines[index - 1] = "```\n" |
| 61 | + inside_output_block = false |
| 62 | + end |
| 63 | + continue |
| 64 | + elseif startswith(line, "```") && inside_julia_block |
| 65 | + inside_julia_block = false |
| 66 | + continue |
| 67 | + end |
| 68 | + |
| 69 | + if !inside_output_block && !inside_julia_block && !isempty(line) #= |
| 70 | + =# && !startswith(line, "![png]") && !startswith(line, "![svg]") |
| 71 | + inside_output_block = true |
| 72 | + lines[index - 1] = "\n```text\n" |
| 73 | + continue |
| 74 | + end |
| 75 | + |
| 76 | + if inside_output_block && index == last_index |
| 77 | + lines[index] = "\n```" |
| 78 | + end |
| 79 | + |
| 80 | + end |
| 81 | + |
| 82 | + # Construct a meta block to hide the `Edit on GitHub` URL |
| 83 | + meta_block = join( |
| 84 | + [ |
| 85 | + "```@meta", |
| 86 | + "EditURL = \"nothing\"", |
| 87 | + "```\n\n", |
| 88 | + ], |
| 89 | + '\n', |
| 90 | + ) |
| 91 | + |
| 92 | + open("$md", "w") do io |
| 93 | + lines[1] = meta_block * lines[1] |
| 94 | + print(io, join(lines, '\n')) |
| 95 | + end |
| 96 | + |
| 97 | +end |
0 commit comments