From 89344005da25fdc21881ba8d67e362069591e74c Mon Sep 17 00:00:00 2001
From: rstular <rok@stular.eu>
Date: Thu, 31 Aug 2023 16:38:05 +0200
Subject: [PATCH] feat(build): Remove some chapters from published book

---
 book/_toc.yml |  2 ++
 build-book.sh | 24 ++++++++++++++----------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/book/_toc.yml b/book/_toc.yml
index e30409f4..c8cb9a37 100644
--- a/book/_toc.yml
+++ b/book/_toc.yml
@@ -142,6 +142,7 @@ parts:
     - file: sandbox/1-5-NumericalMethods/1-general-solvers.ipynb
     - file: sandbox/1-5-NumericalMethods/2-implicit-ode-solver.ipynb
   
+  # START REMOVE-FROM-PUBLISH
   - caption: MUDE Cookbook
     chapters:
     - file: cookbook/blank
@@ -159,6 +160,7 @@ parts:
       - file: cookbook/example_quiz_interactive.ipynb
       - file: cookbook/coding_theory_widgets.ipynb
       - file: cookbook/theory_questions
+  # END REMOVE-FROM-PUBLISH
   - caption: Old Material
     chapters:
     - file: old/blank
diff --git a/build-book.sh b/build-book.sh
index 049dfbe5..5f230af0 100755
--- a/build-book.sh
+++ b/build-book.sh
@@ -4,13 +4,20 @@ set -euo pipefail
 
 START_SERVER=${1:-false}
 PAGE_ROOT=${2:-"/"}
+USE_TOC_BLACKLIST=${3:-false}
+
+# Pre-process the book using the blacklist comment
+if [ "$USE_TOC_BLACKLIST" = true ]; then
+	echo "Using TOC blacklist"
+	sed --in-place=".bak" '/# START REMOVE-FROM-PUBLISH/,/# END REMOVE-FROM-PUBLISH/{//!d}' book/_toc.yml
+fi
 
 # Build the jupyter book, everything else is post-processing
 jupyter-book build book/
 
 # Note: the structure of thebe_lite mimicks where thing are needed in the html folder
 cp thebe_lite/* book/_build/html/ -r
-sed "s,const PAGE_ROOT = \"/\";,const PAGE_ROOT = \"$PAGE_ROOT\";,g" thebe_lite/_static/sphinx-thebe.js > book/_build/html/_static/sphinx-thebe.js
+sed "s,const PAGE_ROOT = \"/\";,const PAGE_ROOT = \"$PAGE_ROOT\";,g" thebe_lite/_static/sphinx-thebe.js >book/_build/html/_static/sphinx-thebe.js
 rm book/_build/html/THEBE_LITE.md
 
 # Copy all non notebook, markdown or build files into the build for local access in pyodide etc.
@@ -21,28 +28,25 @@ rm book/_build/html/THEBE_LITE.md
 # 4. xargs: will execute the script inside the quotes
 # 5. grep: finds a file's parent's path by matching against the section of the string ending in a '/'
 # 6. mkdir: makes all the parent directories, -p will do so recrusively
-# 7. cp: finally copies all files from the /book folder to /book/_build/html  
+# 7. cp: finally copies all files from the /book folder to /book/_build/html
 find book/ -type f | grep -v "^book/_.*\|.*\.\(md\|ipynb\)" | cut -c 6- | xargs -i sh -c 'echo "book/_build/html/{}" | grep -o "^.*/" | xargs -d "\n" mkdir -p; cp book/"{}" book/_build/html/"{}"'
 
 # Check whether python has the alias 'python' or 'python3'
-if command -v python3 > /dev/null 2>&1
-then
+if command -v python3 >/dev/null 2>&1; then
 	python_command="python3"
 else
-	if command -v python > /dev/null 2>&1
-	then
+	if command -v python >/dev/null 2>&1; then
 		python_command="python"
 	fi
 fi
 
-
-if [ "$python_command" = "" ] ; then
+if [ "$python_command" = "" ]; then
 	echo "Book successfully built. If you want to use interactive elements, start a local HTTP server for the _build/html folder."
 	exit 0
 fi
 
 # Serves the files on port 8000, localhost (127.0.0.1:8000)
-if [ "$START_SERVER" = true ] ; then
+if [ "$START_SERVER" = true ]; then
 	echo "Starting server on port 8000"
 	$python_command -m http.server 8000 --directory book/_build/html
 else
@@ -50,7 +54,7 @@ else
 	start a server locally using the command:
 	    $python_command -m http.server 8000 --directory book/_build/html.
 	Or run this script again (book will build again!):
-		${0} true"  
+		${0} true"
 fi
 
 echo "To close a python server run the commeand:
-- 
GitLab