github.com/gemaraproj/gemara@v0.23.0

cmd/scripts/parse-nav.sh raw

 1#!/bin/sh
 2# Parses docs/schema-nav.yml and outputs page information
 3# Usage: parse-nav.sh <nav-file> <command>
 4# Commands: list-pages, get-title <filename>, get-filename <title>
 5
 6NAV_FILE="$1"
 7COMMAND="$2"
 8ARG="$3"
 9
10case "$COMMAND" in
11  list-pages)
12    # Output: filename|title (one per line)
13    awk '/^  - title:/ { 
14      title = $0; 
15      gsub(/.*title: "/, "", title); 
16      gsub(/".*/, "", title); 
17      getline; 
18      if ($0 ~ /filename:/) { 
19        filename = $0; 
20        gsub(/.*filename: /, "", filename); 
21        gsub(/[ "]/, "", filename); 
22      } else { 
23        filename = ""; 
24      } 
25      if (filename == "") { 
26        filename = tolower(title); 
27        gsub(/ /, "-", filename); 
28      } 
29      print filename "|" title 
30    }' "$NAV_FILE"
31    ;;
32  get-title)
33    # Get title for a given filename
34    awk -v filename="$ARG" '
35    BEGIN { found = 0 }
36    /^  - title:/ { 
37      title = $0; 
38      gsub(/.*title: "/, "", title); 
39      gsub(/".*/, "", title); 
40      getline; 
41      if ($0 ~ /filename:/) { 
42        file = $0; 
43        gsub(/.*filename: /, "", file); 
44        gsub(/[ "]/, "", file); 
45      } else { 
46        file = ""; 
47      } 
48      if (file == "") { 
49        file = tolower(title); 
50        gsub(/ /, "-", file); 
51      } 
52      if (file == filename) { 
53        print title; 
54        found = 1; 
55        exit 
56      } 
57    }
58    END { if (!found) exit 1 }
59    ' "$NAV_FILE"
60    ;;
61  *)
62    echo "Unknown command: $COMMAND" >&2
63    exit 1
64    ;;
65esac