Skip to content

Commit c00b84d

Browse files
author
AoifeHughes
committed
Add environment variable for Colab path prefix in notebook link scripts
1 parent d07ab29 commit c00b84d

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

.github/workflows/preview.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ jobs:
5858

5959
- name: Add notebook download links to HTML
6060
run: sh assets/scripts/add_notebook_links.sh
61+
env:
62+
COLAB_PATH_PREFIX: pr-previews/${{ github.event.pull_request.number }}
6163

6264
- name: Save _freeze folder
6365
id: cache-save

.github/workflows/publish.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ jobs:
9090

9191
- name: Add notebook download links to HTML
9292
run: sh assets/scripts/add_notebook_links.sh
93+
env:
94+
COLAB_PATH_PREFIX: versions/${{ env.version }}
9395

9496
- name: Rename original search index
9597
run: mv _site/search.json _site/search_original.json

assets/scripts/add_notebook_links.sh

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ set -e
66

77
echo "Adding notebook download links to HTML pages..."
88

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:-}"
1117

1218
# Find all HTML files that have corresponding .ipynb files
1319
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
1723
# Check if the corresponding .ipynb file exists
1824
if [ -f "$ipynb_file" ]; then
1925
# 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
2339
# 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"
2642
fi
2743
fi
2844
done

0 commit comments

Comments
 (0)