소스 검색

Port 'book/build.sh' to python

Bzero 1 년 전
부모
커밋
0e74e58684
4개의 변경된 파일55개의 추가작업 그리고 53개의 파일을 삭제
  1. 3 2
      .github/workflows/ci.yml
  2. 51 0
      book/build.py
  3. 0 50
      book/build.sh
  4. 1 1
      book/deploy.sh

+ 3 - 2
.github/workflows/ci.yml

@@ -50,6 +50,7 @@ jobs:
     steps:
     - uses: dtolnay/rust-toolchain@stable
     - uses: actions/checkout@v3
+    - uses: actions/setup-python@v5
     - name: Install mdbook
       run: |
         cargo install mdbook
@@ -57,10 +58,10 @@ jobs:
     - name: Build documentation
       run: |
         cd book
-        bash build.sh
+        python build.py
         if [ -n "$(git status --porcelain)" ]; then
           echo "Documentation not up to date."
-          echo "Make sure to run 'build.sh' in the book/ folder"
+          echo "Make sure to run 'build.py' in the book/ folder"
           exit 1
         fi
 

+ 51 - 0
book/build.py

@@ -0,0 +1,51 @@
+import subprocess
+from pathlib import Path
+
+
+SCRIPT_DIR = Path(__file__).parent
+
+
+def generate_example(filename, title, strip_assets):
+    path_in = SCRIPT_DIR.parent / "examples" / f"{filename}.nbt"
+    path_out = SCRIPT_DIR / "src" / f"example-{filename}.md"
+
+    code = []
+    with open(path_in, "r") as fin:
+        for line in fin:
+            if not strip_assets or "assert_eq" not in line:
+                code.append(line)
+
+    with open(path_out, "w") as fout:
+        fout.write("<!-- This file is autogenerated! Do not modify it -->\n")
+        fout.write("\n")
+        fout.write(f"# {title}\n")
+        fout.write("\n")
+        fout.write("``` numbat\n")
+        fout.writelines(code)
+        fout.write("```\n")
+
+
+generate_example("acidity", "Acidity", True)
+generate_example("barometric_formula", "Barometric formula", True)
+generate_example("body_mass_index", "Body mass index", True)
+generate_example("factorial", "Factorial", False)
+generate_example("medication_dosage", "Medication dosage", True)
+generate_example("molarity", "Molarity", True)
+generate_example("musical_note_frequency", "Musical note frequency", True)
+generate_example("paper_size", "Paper sizes", False)
+generate_example("pipe_flow_rate", "Flow rate in a pipe", True)
+generate_example("population_growth", "Population growth", True)
+generate_example("recipe", "Recipe", True)
+generate_example("voyager", "Voyager", True)
+generate_example("xkcd_687", "XKCD 687", True)
+generate_example("xkcd_2585", "XKCD 2585", True)
+generate_example("xkcd_2812", "XKCD 2812", True)
+generate_example("workhours", "Workhours", True)
+
+generate_example("numbat_syntax", "Syntax overview", False)
+
+path_units = SCRIPT_DIR / "src" / "list-units.md"
+with open(path_units, "w") as f:
+    subprocess.run(["cargo", "run", "--example=inspect"], stdout=f, text=True)
+
+subprocess.run(["mdbook", "build"], text=True)

+ 0 - 50
book/build.sh

@@ -1,50 +0,0 @@
-#!/bin/bash
-
-set -euo pipefail
-
-SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
-
-generate_example() {
-    filename="$1"
-    title="$2"
-    strip_asserts="$3"
-
-    out_file="$SCRIPT_DIR/src/example-${filename}.md"
-
-    (
-        echo "<!-- This file is autogenerated! Do not modify it -->"
-        echo
-        echo "# ${title}"
-        echo
-        echo "\`\`\` numbat"
-        if [ "$strip_asserts" = true ]; then
-            grep -v assert_eq "../examples/${filename}.nbt"
-        else
-            cat "../examples/${filename}.nbt"
-        fi
-        echo "\`\`\`"
-    ) > "$out_file"
-}
-
-generate_example acidity "Acidity" true
-generate_example barometric_formula "Barometric formula" true
-generate_example body_mass_index "Body mass index" true
-generate_example factorial "Factorial" false
-generate_example medication_dosage "Medication dosage" true
-generate_example molarity "Molarity" true
-generate_example musical_note_frequency "Musical note frequency" true
-generate_example paper_size "Paper sizes" false
-generate_example pipe_flow_rate "Flow rate in a pipe" true
-generate_example population_growth "Population growth" true
-generate_example recipe "Recipe" true
-generate_example voyager "Voyager" true
-generate_example xkcd_687 "XKCD 687" true
-generate_example xkcd_2585 "XKCD 2585" true
-generate_example xkcd_2812 "XKCD 2812" true
-
-generate_example numbat_syntax "Syntax overview" false
-
-
-cargo run --example=inspect > "${SCRIPT_DIR}/src/list-units.md"
-
-mdbook build

+ 1 - 1
book/deploy.sh

@@ -6,6 +6,6 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
 
 cd "$SCRIPT_DIR"
 
-bash build.sh
+python build.py
 
 rsync --delete --archive --stats --progress --human-readable book/html/ shark.fish:numbat.dev/doc/