Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/docs/literate/make.jl
2055 views
1
using Literate: Literate
2
using Test: @testset
3
import Pkg
4
5
# Create markdown and notebook files for `file`
6
function create_files(title, file, repo_src, pages_dir, notebooks_dir; folder = "")
7
notebook_filename = first(splitext(file)) * ".ipynb"
8
if !isempty(folder)
9
notebook_filename = joinpath(folder, notebook_filename)
10
end
11
12
binder_logo = "https://mybinder.org/badge_logo.svg"
13
nbviewer_logo = "https://img.shields.io/badge/render-nbviewer-f37726"
14
raw_notebook_logo = "https://img.shields.io/badge/raw-notebook-4cc61e"
15
colab_logo = "https://colab.research.google.com/assets/colab-badge.svg"
16
17
notebook_path = "tutorials/notebooks/$notebook_filename"
18
binder_url = "https://mybinder.org/v2/gh/trixi-framework/TrixiDocumentation/tutorial_notebooks?filepath=$notebook_path"
19
nbviewer_url = "https://nbviewer.jupyter.org/github/trixi-framework/TrixiDocumentation/blob/tutorial_notebooks/$notebook_path"
20
raw_notebook_url = "https://raw.githubusercontent.com/trixi-framework/TrixiDocumentation/tutorial_notebooks/$notebook_path"
21
colab_url = "https://colab.research.google.com/github/trixi-framework/TrixiDocumentation/blob/tutorial_notebooks/$notebook_path"
22
23
binder_badge = "# [![]($binder_logo)]($binder_url)"
24
nbviewer_badge = "# [![]($nbviewer_logo)]($nbviewer_url)"
25
raw_notebook_badge = "# [![]($raw_notebook_logo)]($raw_notebook_url)"
26
colab_badge = "# [![]($colab_logo)]($colab_url)"
27
28
# Generate notebook file
29
function preprocess_notebook(content)
30
warning = "# **Note:** To improve responsiveness via caching, the notebooks are updated only once a week. They are only
31
# available for the latest stable release of Trixi.jl at the time of caching.\n\n"
32
return string("# # $title\n\n", warning, content)
33
end
34
Literate.notebook(joinpath(repo_src, folder, file), joinpath(notebooks_dir, folder);
35
execute = false, preprocess = preprocess_notebook, credit = false)
36
37
# Generate markdown file
38
function preprocess_docs(content)
39
return string("# # [$title](@id $(splitext(file)[1]))\n $binder_badge\n $nbviewer_badge\n $colab_badge\n $raw_notebook_badge\n\n",
40
content)
41
end
42
Literate.markdown(joinpath(repo_src, folder, file), joinpath(pages_dir, folder);
43
preprocess = preprocess_docs,)
44
end
45
46
# Create tutorials with Literate.jl
47
function create_tutorials(files)
48
repo_src = joinpath(@__DIR__, "src", "files")
49
50
pages_dir = joinpath(@__DIR__, "..", "src", "tutorials")
51
notebooks_dir = joinpath(pages_dir, "notebooks")
52
53
Sys.rm(pages_dir; recursive = true, force = true)
54
55
Sys.rm("out"; recursive = true, force = true)
56
57
# Run tests on all tutorial files
58
@testset "TrixiTutorials" begin
59
for (i, (title, filename)) in enumerate(files)
60
# Evaluate each tutorial in its own module to avoid leaking of
61
# function/variable names, polluting the namespace of later tutorials
62
# by stuff defined in earlier tutorials.
63
if filename isa Vector # Several files of one topic
64
for j in eachindex(filename)
65
mod = gensym(filename[j][2][2])
66
@testset "$(filename[j][2][2])" begin
67
@eval module $mod
68
include(joinpath($repo_src, $(filename[j][2][1]),
69
$(filename[j][2][2])))
70
end
71
end
72
end
73
else # Single files
74
mod = gensym(title)
75
@testset "$title" begin
76
@eval module $mod
77
include(joinpath($repo_src, $filename))
78
end
79
end
80
end
81
end
82
end
83
84
# Generate markdown file for introduction page
85
# Preprocessing introduction file: Generate consecutive tutorial numbers by replacing
86
# each occurrence of `{index}` with an integer incremented by 1, starting at 1.
87
function preprocess_introduction(content)
88
counter = 1
89
while occursin("{index}", content)
90
content = replace(content, "{index}" => "$counter", count = 1)
91
counter += 1
92
end
93
return content
94
end
95
Literate.markdown(joinpath(repo_src, "index.jl"), pages_dir; name = "introduction",
96
preprocess = preprocess_introduction)
97
# Navigation system for makedocs
98
pages = Any["Introduction" => "tutorials/introduction.md"]
99
100
# Create markdown and notebook files for tutorials
101
for (i, (title, filename)) in enumerate(files)
102
# Several files of one topic are created separately and pushed to `pages` together.
103
if filename isa Vector
104
vector = []
105
for j in eachindex(filename)
106
create_files("$i.$j: $title: $(filename[j][1])", filename[j][2][2],
107
repo_src,
108
pages_dir, notebooks_dir; folder = filename[j][2][1])
109
110
path = "$(filename[j][2][1])/$(splitext(filename[j][2][2])[1]).md"
111
push!(vector, "$i.$j $(filename[j][1])" => "tutorials/$path")
112
end
113
# Add to navigation menu
114
push!(pages, ("$i $title" => vector))
115
else # Single files
116
create_files("$i: $title", filename, repo_src, pages_dir, notebooks_dir)
117
# Add to navigation menu
118
path = first(splitext(filename)) * ".md"
119
push!(pages, ("$i $title" => "tutorials/$path"))
120
end
121
end
122
123
return pages
124
end
125
126