Skip to content

Commit

Permalink
Add fix for utterances bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcraig committed Mar 25, 2024
1 parent e1eb372 commit 8da3bb8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ jobs:
run: |
export GUIDE_RANDOM_SEED=4920385 # Use a seed to minimize changes in images
jb build .
# Workaround to enable utterances comments. See script for details.
python fix_jupyter_book_comments.py _build/html
- name: Upload HTML
uses: actions/upload-artifact@v4
Expand All @@ -87,12 +89,13 @@ jobs:
name: Deploy
runs-on: ubuntu-latest
needs: build_book
if: ${{ github.ref == 'refs/heads/main' || contains(github.ref, 'tags') }}
if: ${{ github.ref == 'refs/heads/main' || contains(github.ref, 'tags') || github.ref == 'refs/heads/photometry-dev' }}
steps:
- name: Set version number
run: |
VERSION_NUMBER=${GITHUB_REF#refs/tags/}
if [[ "${{ github.ref }}" == 'refs/heads/main' ]]; then VERSION_NUMBER=dev; fi
if [[ "${{ github.ref }}" == 'refs/heads/photometry-dev' ]]; then VERSION_NUMBER=pdev; fi
echo "VERSION_NUMBER=$VERSION_NUMBER" >> $GITHUB_ENV
- name: Download artifact
uses: actions/download-artifact@v4
Expand Down
37 changes: 37 additions & 0 deletions fix_jupyter_book_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This is intended to implement the fix to sphinx-comments in
# https://github.com/executablebooks/sphinx-comments/pull/18/
# This is a temporary fix until the PR is merged and released.
#
# The fix enables us to use utterances comments in Jupyter Book.

import re
from argparse import ArgumentParser
from pathlib import Path


def fix_utterances_script(html_path):
with open(html_path, 'r') as f:
notebook = f.read()

new_notebook = notebook.replace("div.section", "section")
new_notebook = new_notebook.replace(
"sections !== null",
"sections !== null && sections.length > 0"
)
if new_notebook != notebook:
print(f"Fixing {html_path}")
with open(html_path, 'w') as f:
f.write(new_notebook)


def main(path):
for html_path in Path(path).glob('**/*.html'):
fix_utterances_script(html_path)


if __name__ == "__main__":
parser = ArgumentParser(description='Fix Jupyter Book comments')
parser.add_argument('path', help='Path to the Jupyter Book build directory',
type=str, default='_build/html')
args = parser.parse_args()
main(args.path)

0 comments on commit 8da3bb8

Please sign in to comment.