@@ -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."
147181end
148182
149183function 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
181221end
182222@main
0 commit comments