Skip to content

Commit 87698b0

Browse files
GiggleLiucarstenbauer
authored andcommitted
A tutorial generation script based on Fire. (#3)
1 parent 865da3a commit 87698b0

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ PhysicsTutorials.open_notebooks()
3030
Supported source files for tutorials are Jupyter notebooks, Weave.jl files, or Literate.jl files.
3131
To contribute a tutorial, clone the repository and put the source file into `tutorials/<category>/<tutorial_name>/` and name it `<tutorial_name>.ipynb` (extension `.jmd`/`.jl` for Weave/Literate sources). To trigger the generation process of all output formats, run the following code from within the repository root folder:
3232

33+
0. Make sure you have `Fire` installed, if not, install it with `julia> ] add Fire`,
34+
1. Create source file `tutorials/<category>/<tutorial_name>/<tutorial_name>.<extension>`, where the extension should be one of `.ipynb`, `.jl` and `.jmd` .
35+
2. Type `julia convert.jl tutorials/<category>/<tutorial_name>/<tutorial_name>.<extension>` in a terminal.
36+
37+
38+
#### Alternative approach: trigger conversion manually
39+
3340
```julia
3441
using Pkg; Pkg.activate(".")
3542
using PhysicsTutorials

Diff for: convert.jl

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)