Skip to content
This repository was archived by the owner on Mar 31, 2022. It is now read-only.

Commit e9771ef

Browse files
authored
(#7) Generate docs from the notebooks.
1 parent a509f70 commit e9771ef

File tree

8 files changed

+196
-1
lines changed

8 files changed

+196
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# A script to install `nbconvert`
4+
5+
# Set auxiliary variables
6+
# for ANSI escape codes
7+
cyan="\e[1;36m" # Bold cyan
8+
reset="\e[0m" # Reset colors
9+
10+
echo -e "\n${cyan}Upgrading \`pip3\`...${reset}\n"
11+
pip3 install --upgrade pip
12+
13+
echo -e "\n${cyan}Installing \`nbconvert\`...${reset}\n"
14+
pip3 install --no-cache-dir nbconvert

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# User's Makefile
22
Makefile
33

4+
# Project manifests
5+
*Manifest.toml
6+
47
# Pictures and plots
5-
*figures
8+
*figures
9+
10+
# Directories created when generating documentation
11+
docs/build
12+
docs/src/generated

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: julia
2+
dist: bionic
3+
4+
notifications:
5+
email: false
6+
7+
env:
8+
global:
9+
- PATH=/opt/python/3.8.1/bin:$PATH
10+
11+
branches:
12+
only:
13+
- notebooks
14+
15+
jobs:
16+
include:
17+
- name: "Documentation Build, Julia 1.5 (Linux, x64)"
18+
julia: 1.5
19+
os: linux
20+
arch: amd64
21+
script:
22+
- bash .github/scripts/install-nbconvert.bash
23+
- julia --project=docs/ -e 'using Pkg; Pkg.instantiate()'
24+
- julia --project=docs docs/make.jl

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[deps]
2+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"

docs/convert.jl

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

docs/make.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Documenter # A package to manage documentation
2+
3+
# Convert the Jupyter notebooks to Markdown files
4+
include("convert.jl")
5+
6+
# Create documentation
7+
makedocs(
8+
# Specify a name for the site
9+
sitename = "GPKernels",
10+
11+
# Specify the author
12+
authors = "Pavel Sobolev.",
13+
14+
# Specify the pages on the left side
15+
pages = [
16+
"Home" => "index.md",
17+
"Notebooks" => [
18+
"Kernels" => map(
19+
s -> "$s" => "generated/$notebooks_folder_name/$s/$s.md", names,
20+
),
21+
],
22+
],
23+
24+
# Specify a format
25+
format = Documenter.HTML(
26+
# A fallback for creating docs locally
27+
prettyurls = get(ENV, "CI", nothing) == "true"
28+
),
29+
30+
# Disable doctests
31+
doctest = false,
32+
33+
# Fail if any error occurred
34+
strict = true,
35+
)
36+
37+
# Deploy documentation
38+
deploydocs(
39+
# Specify a repository
40+
repo = "github.com/paveloom-c/GPKernels.git",
41+
42+
# Specify a development branch
43+
devbranch = "notebooks",
44+
)

docs/src/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```@meta
2+
EditURL = "nothing"
3+
```
4+
5+
# GPKernels
6+
7+
This work is in progress.
File renamed without changes.

0 commit comments

Comments
 (0)