6
6
7
7
echo " Adding notebook download links to HTML pages..."
8
8
9
- # Link text variable
10
- LINK_TEXT=" Download notebook"
9
+ # Link text variables
10
+ DOWNLOAD_TEXT=" Download notebook"
11
+ COLAB_TEXT=" Open in Colab"
12
+
13
+ # Colab URL configuration (can be overridden via environment variables)
14
+ COLAB_REPO=" ${COLAB_REPO:- TuringLang/ docs} "
15
+ COLAB_BRANCH=" ${COLAB_BRANCH:- gh-pages} "
16
+ COLAB_PATH_PREFIX=" ${COLAB_PATH_PREFIX:- } "
11
17
12
18
# Find all HTML files that have corresponding .ipynb files
13
19
find _site/tutorials _site/usage _site/developers -name " index.html" 2> /dev/null | while read html_file; do
@@ -17,12 +23,22 @@ find _site/tutorials _site/usage _site/developers -name "index.html" 2>/dev/null
17
23
# Check if the corresponding .ipynb file exists
18
24
if [ -f " $ipynb_file " ]; then
19
25
# Check if link is already present
20
- if ! grep -q " $LINK_TEXT " " $html_file " ; then
21
- # Insert the notebook link AFTER the "Report an issue" link
22
- # This ensures it goes in the right place in the sidebar toc-actions
26
+ if ! grep -q " $DOWNLOAD_TEXT " " $html_file " ; then
27
+ # Get relative path from _site/ directory
28
+ relative_path=" ${html_file# _site/ } "
29
+ relative_path=" ${relative_path%/ index.html} "
30
+
31
+ # Construct Colab URL
32
+ if [ -n " $COLAB_PATH_PREFIX " ]; then
33
+ colab_url=" https://colab.research.google.com/github/${COLAB_REPO} /blob/${COLAB_BRANCH} /${COLAB_PATH_PREFIX} /${relative_path} /index.ipynb"
34
+ else
35
+ colab_url=" https://colab.research.google.com/github/${COLAB_REPO} /blob/${COLAB_BRANCH} /${relative_path} /index.ipynb"
36
+ fi
37
+
38
+ # Insert both download and Colab links AFTER the "Report an issue" link
23
39
# The download="index.ipynb" attribute forces browser to download instead of navigate
24
- perl -i -pe " s/(<a href=\" [^\" ]*issues\/new\" [^>]*><i class=\" bi[^\" ]*\" ><\/i>Report an issue<\/a><\/li>)/\$ 1<li><a href=\" index.ipynb\" class=\" toc-action\" download=\" index.ipynb\" ><i class=\" bi bi-journal-code\" ><\/i>$LINK_TEXT <\/a><\/li>/g" " $html_file "
25
- echo " ✓ Added notebook link to $html_file "
40
+ perl -i -pe " s/(<a href=\" [^\" ]*issues\/new\" [^>]*><i class=\" bi[^\" ]*\" ><\/i>Report an issue<\/a><\/li>)/\$ 1<li><a href=\" index.ipynb\" class=\" toc-action\" download=\" index.ipynb\" ><i class=\" bi bi-journal-code\" ><\/i>$DOWNLOAD_TEXT <\/a><\/li><li><a href= \" $colab_url \" class= \" toc-action \" target= \" _blank \" rel= \" noopener \" ><i class= \" bi bi-google \" ><\/i> $COLAB_TEXT <\/a><\/li>/g" " $html_file "
41
+ echo " ✓ Added notebook links to $html_file "
26
42
fi
27
43
fi
28
44
done
0 commit comments