|
| 1 | +using Pkg |
| 2 | +#Pkg.activate(dirname(@__FILE__)) |
| 3 | +Pkg.activate(@__DIR__) |
| 4 | +using Fire |
| 5 | +using PhysicsTutorials |
| 6 | + |
| 7 | +@main function generate(filename::String) |
| 8 | + pns = _splitpath(abspath(filename)) |
| 9 | + length(pns) < 3 && error("Invalid path to source file: $filename") |
| 10 | + |
| 11 | + category, tutorial_name = pns[end-2:end-1] |
| 12 | + fname, ext = splitext(pns[end]) |
| 13 | + |
| 14 | + fname==tutorial_name || error("tutorial name $tutorial_name is not consistent with filename $fname, expected format: `tutorials/quantum_computing/<name>/<name>.ipynb`") |
| 15 | + |
| 16 | + if ext == ".ipynb" |
| 17 | + source = PhysicsTutorials.NotebookSource() |
| 18 | + elseif ext ==".jmd" |
| 19 | + source = PhysicsTutorials.WeaveSource() |
| 20 | + elseif ext ==".jl" |
| 21 | + source = PhysicsTutorials.LiterateSource() |
| 22 | + else |
| 23 | + error("Expected file extension: `*.jl`, `*.jmd` or `*.ipynb`, but got $ext.") |
| 24 | + end |
| 25 | + PhysicsTutorials.convert_tutorial(category,tutorial_name,source) |
| 26 | +end |
| 27 | + |
| 28 | + |
| 29 | +# compatibility patch |
| 30 | +const path_dir_splitter = r"^(.*?)([/\\]+)([^/\\]*)$" |
| 31 | + |
| 32 | +_splitdir_nodrive(path::String) = _splitdir_nodrive("", path) |
| 33 | +function _splitdir_nodrive(a::String, b::String) |
| 34 | + m = match(path_dir_splitter,b) |
| 35 | + m === nothing && return (a,b) |
| 36 | + a = string(a, isempty(m.captures[1]) ? m.captures[2][1] : m.captures[1]) |
| 37 | + a, String(m.captures[3]) |
| 38 | +end |
| 39 | + |
| 40 | +function _splitpath(p::String) |
| 41 | + drive, p = splitdrive(p) |
| 42 | + out = String[] |
| 43 | + isempty(p) && (pushfirst!(out,p)) # "" means the current directory. |
| 44 | + while !isempty(p) |
| 45 | + dir, base = _splitdir_nodrive(p) |
| 46 | + dir == p && (pushfirst!(out, dir); break) # Reached root node. |
| 47 | + if !isempty(base) # Skip trailing '/' in basename |
| 48 | + pushfirst!(out, base) |
| 49 | + end |
| 50 | + p = dir |
| 51 | + end |
| 52 | + if !isempty(drive) # Tack the drive back on to the first element. |
| 53 | + out[1] = drive*out[1] # Note that length(out) is always >= 1. |
| 54 | + end |
| 55 | + return out |
| 56 | +end |
| 57 | + |
| 58 | + |
0 commit comments