github.com/gemaraproj/gemara@v1.3.0

cmd/scripts/parse-nav.sh raw

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