Conversion .ngscript in .jl
This example implements a simple conversion utility. .ngscript to files with the extension .jl designed for direct execution in the Julia terminal.
The main task of the script is to extract the contents of laptop cells (including both markdown text and executable code) and save it in a readable format.
The script itself reads the contents of the notebook, presented in JSON format, processing two main categories of cells: markdown type cells and code type cells. Markdown cells are saved as comments, while code cells are written literally, enabling a subsequent run. It is also possible to skip markdown cells if necessary.
using JSON
Consider the function itself. It takes the path to the input file and an optional argument. skip_markdown, which determines whether to skip markdown type cells during conversion.
Now let's move on to the algorithm and consider it line by line.
- We declare the function.
- Transform the relative path to the absolute one.
- We form the name of the output file by replacing the extension of the source file with
.jl. - We read the contents of the JSON file and parse it into the Julia structure.
- Open the output file for recording.
- Loop through all cells.
- If it is found that the cell belongs to the markdown type.
- Skip processing if the markdown cells skip flag is set.
- Skip markdown cells containing inserted images (
![]). - We record the contents of the markdown cell in the form of a Julia comment (
#=...=# - If the cell is a code and contains a non-zero number of rows.
- Write the lines of code directly to the file, preserving their order.
- The end.
function script2jl(input_path::String; skip_markdown=false)
input_file = abspath(input_path)
output_file = replace(input_file, r"\.\w+$" => ".jl")
notebook = JSON.parsefile(input_file)
open(output_file, "w") do f
for cell in notebook["cells"]
if cell["cell_type"] == "markdown"
skip_markdown && continue
any(startswith(l, "![") for l in cell["source"]) && continue
println(f, "#= ", join(cell["source"]), " =#\n")
elseif cell["cell_type"] == "code" && !isempty(cell["source"])
println(f, join(cell["source"]), "\n")
end
end
end
println("Конвертация ngscript в jl завершена.")
end
Usage examples
Converting a script2jl.ngscript file with an absolute path without skipping the markdown cell.
@time script2jl("$(@__DIR__)/script2jl.ngscript")
Converting a script2jl.ngscript file with a markdown cell omitted.
@time script2jl("script2jl.ngscript", skip_markdown=true)
Conclusion
Based on the results of this example, we received the code in the format .jl which can be connected to any project via the command include("$(@__DIR__)/script2jl.jl").

This tool is useful for developers who use .ngscript for experimentation and prototyping. It allows you to easily transfer working code into full-fledged programs in the Julia language.
Due to its simplicity and flexibility, the script increases the efficiency of software development and simplifies migration between programming environments.