Skip to content

Commit 1eecd82

Browse files
committed
move add_notebooks to Julia too
1 parent e740585 commit 1eecd82

File tree

4 files changed

+58
-73
lines changed

4 files changed

+58
-73
lines changed

.github/workflows/preview.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,10 @@ jobs:
5353
- name: Render Quarto site
5454
run: quarto render
5555

56-
- name: Generate Jupyter notebooks
57-
run: julia --project=assets/scripts/notebooks assets/scripts/notebooks/make_notebooks.jl
58-
59-
- name: Add notebook download links to HTML
60-
run: sh assets/scripts/add_notebook_links.sh
56+
- name: Generate Jupyter notebooks and add to HTML
57+
run: julia --project=assets/scripts/notebooks assets/scripts/notebooks/notebooks.jl
6158
env:
62-
COLAB_PATH_PREFIX: pr-previews/${{ github.event.pull_request.number }}
59+
PATH_PREFIX: /pr-previews/${{ github.event.pull_request.number }}
6360

6461
- name: Save _freeze folder
6562
id: cache-save

.github/workflows/publish.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,10 @@ jobs:
8585
- name: Render Quarto site
8686
run: quarto render
8787

88-
- name: Generate Jupyter notebooks
89-
run: julia --project=assets/scripts/notebooks assets/scripts/notebooks/make_notebooks.jl
90-
91-
- name: Add notebook download links to HTML
92-
run: sh assets/scripts/add_notebook_links.sh
88+
- name: Generate Jupyter notebooks and add to HTML
89+
run: julia --project=assets/scripts/notebooks assets/scripts/notebooks/notebooks.jl
9390
env:
94-
COLAB_PATH_PREFIX: versions/${{ env.version }}
91+
PATH_PREFIX: /versions/${{ env.version }}
9592

9693
- name: Rename original search index
9794
run: mv _site/search.json _site/search_original.json

assets/scripts/add_notebook_links.sh

Lines changed: 0 additions & 49 deletions
This file was deleted.

assets/scripts/notebooks/make_notebooks.jl renamed to assets/scripts/notebooks/notebooks.jl

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,48 @@ function convert_qmd_to_ipynb(in_qmd_path::String, out_ipynb_path::String)
143143
@info "converting $in_qmd_path to $out_ipynb_path..."
144144
notebook = parse_cells(in_qmd_path)
145145
JSON.json(out_ipynb_path, notebook; pretty=true)
146-
@info "done."
146+
@info " - done."
147+
end
148+
149+
function add_ipynb_link_to_html(html_path::String, ipynb_path::String)
150+
PATH_PREFIX = get(ENV, "PATH_PREFIX", "")
151+
COLAB_URL = "https://colab.research.google.com/github/TuringLang/docs/blob/gh-pages$PATH_PREFIX/$ipynb_path"
152+
SUGGESTED_FILENAME = basename(dirname(ipynb_path)) * ".ipynb"
153+
@info "adding link to ipynb notebook in $html_path... with PATH_PREFIX='$PATH_PREFIX'"
154+
if !isfile(html_path)
155+
@info " - HTML file $html_path does not exist; skipping"
156+
return
157+
end
158+
html_content = read(html_path, String)
159+
if occursin("colab.research.google.com", html_content)
160+
@info " - colab link already present; skipping"
161+
return
162+
end
163+
# The line to edit looks like this:
164+
# <div class="toc-actions"><ul><li><a href="https://github.com/TuringLang/docs/edit/main/getting-started/index.qmd" target="_blank" class="toc-action"><i class="bi bi-github"></i>Edit this page</a></li><li><a href="https://github.com/TuringLang/docs/issues/new" target="_blank" class="toc-action"><i class="bi empty"></i>Report an issue</a></li></ul></div></nav>
165+
# We want to insert two new list items at the end of the ul.
166+
lines = split(html_content, '\n')
167+
new_lines = map(lines) do line
168+
if occursin(r"^<div class=\"toc-actions\">", line)
169+
insertion = (
170+
"<li><a href=\"index.ipynb\" target=\"_blank\" class=\"toc-action\" download=\"$SUGGESTED_FILENAME\"><i class=\"bi bi-journal-code\"></i>Download notebook</a></li>" *
171+
"<li><a href=\"$COLAB_URL\" target=\"_blank\" class=\"toc-action\"><i class=\"bi bi-google\"></i>Open in Colab</a></li>"
172+
)
173+
return replace(line, r"</ul>" => "$insertion</ul>")
174+
else
175+
return line
176+
end
177+
end
178+
new_html_content = join(new_lines, '\n')
179+
write(html_path, new_html_content)
180+
@info " - done."
147181
end
148182

149183
function main(args)
150184
if length(args) == 0
151-
# Get the list of .qmd files from the _quarto.yml file. This conveniently
152-
# also checks that we are at the repo root.
153-
try
185+
# Get the list of .qmd files from the _quarto.yml file. This conveniently also
186+
# checks that we are at the repo root.
187+
qmd_files = try
154188
quarto_config = split(read("_quarto.yml", String), '\n')
155189
qmd_files = String[]
156190
for line in quarto_config
@@ -159,24 +193,30 @@ function main(args)
159193
push!(qmd_files, m.captures[1])
160194
end
161195
end
162-
for file in qmd_files
163-
dir = "_site/" * dirname(file)
164-
base = replace(basename(file), r"\.qmd$" => ".ipynb")
165-
isdir(dir) || mkpath(dir) # mkpath is essentially mkdir -p
166-
out_ipynb_path = joinpath(dir, base)
167-
convert_qmd_to_ipynb(file, out_ipynb_path)
168-
end
196+
qmd_files
169197
catch e
170198
if e isa SystemError
171199
error("Could not find _quarto.yml; please run this script from the repo root.")
172200
else
173201
rethrow(e)
174202
end
175203
end
176-
204+
for file in qmd_files
205+
# Convert qmd to ipynb
206+
dir = "_site/" * dirname(file)
207+
ipynb_base = replace(basename(file), r"\.qmd$" => ".ipynb")
208+
isdir(dir) || mkpath(dir) # mkpath is essentially mkdir -p
209+
out_ipynb_path = joinpath(dir, ipynb_base)
210+
convert_qmd_to_ipynb(file, out_ipynb_path)
211+
# Add a link in the corresponding html file
212+
html_base = replace(basename(file), r"\.qmd$" => ".html")
213+
out_html_path = joinpath(dir, html_base)
214+
add_ipynb_link_to_html(out_html_path, out_ipynb_path)
215+
end
177216
elseif length(args) == 2
178217
in_qmd_path, out_ipynb_path = args
179218
convert_qmd_to_ipynb(in_qmd_path, out_ipynb_path)
219+
add_ipynb_link_to_html(replace(out_ipynb_path, r"\.ipynb$" => ".html"), out_ipynb_path)
180220
end
181221
end
182222
@main

0 commit comments

Comments
 (0)