|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-FileCopyrightText: 2024 Robin Vobruba <hoijui.quaero@gmail.com> |
| 3 | +# SPDX-License-Identifier: AGPL-3.0-only |
| 4 | +# |
| 5 | +# See the output of "$0 -h" for details. |
| 6 | + |
| 7 | +# Exit immediately on each error and unset variable; |
| 8 | +# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ |
| 9 | +set -Eeuo pipefail |
| 10 | +#set -Eeu |
| 11 | + |
| 12 | +script_path="$(readlink -f "${BASH_SOURCE[0]}")" |
| 13 | +#script_dir="$(dirname "$script_path")" |
| 14 | +script_name="$(basename "$script_path")" |
| 15 | + |
| 16 | +function print_help() { |
| 17 | + |
| 18 | + echo -e "$script_name -" |
| 19 | + echo "Generates the 'SUMMARY.md' file" |
| 20 | + echo "(required by [mdbook](https://github.com/rust-lang/mdBook/))." |
| 21 | + echo |
| 22 | + echo "Usage:" |
| 23 | + echo " $script_name [OPTION...]" |
| 24 | + echo "Options:" |
| 25 | + echo " -h, --help Print this usage help and exits." |
| 26 | + echo "Examples:" |
| 27 | + echo " $script_name --help" |
| 28 | + echo " $script_name" |
| 29 | +} |
| 30 | + |
| 31 | +# read command-line args |
| 32 | +POSITIONAL=() |
| 33 | +while [[ $# -gt 0 ]] |
| 34 | +do |
| 35 | + arg="$1" |
| 36 | + shift # $2 -> $1, $3 -> $2, ... |
| 37 | + |
| 38 | + case "$arg" in |
| 39 | + -h|--help) |
| 40 | + print_help |
| 41 | + exit 0 |
| 42 | + ;; |
| 43 | + *) # non-/unknown option |
| 44 | + POSITIONAL+=("$arg") # save it in an array for later |
| 45 | + ;; |
| 46 | + esac |
| 47 | +done |
| 48 | +set -- "${POSITIONAL[@]}" # restore positional parameters |
| 49 | + |
| 50 | +# REUSE-IgnoreStart |
| 51 | +find text/ -name "*.md" | sed -e 's|^\./||' | sort | awk -F'-' -e ' |
| 52 | +BEGIN { |
| 53 | + section_title = "" |
| 54 | + chapter_title = "" |
| 55 | + last_section_id = -1 |
| 56 | + last_chapter_id = -1 |
| 57 | + introduction_ended = 0 |
| 58 | + annex_started = 0 |
| 59 | +
|
| 60 | + print("# Summary") |
| 61 | + print("\n<!--\nSPDX-FileCopyrightText: Robin Vobruba <hoijui.quaero@gmail.com>\n\nSPDX-License-Identifier: CC-BY-SA-3.0\n-->") |
| 62 | + print("\n<!--\nNOTE:\nThis file was auto-generated with script\n\"'"$0"'\";\ndo not edit manually!\n-->") |
| 63 | +} |
| 64 | +{ |
| 65 | + section_id = $1 |
| 66 | + sub(/^text\/s/, "", section_id) |
| 67 | + chapter_id = $2 |
| 68 | + sub(/^c/, "", chapter_id) |
| 69 | + file_name = $0 |
| 70 | + new_section = section_id != last_section_id && section_id > 0 |
| 71 | +
|
| 72 | + if (new_section) { |
| 73 | + cmd_get_section_title = sprintf("grep \"^# \" < \"%s\" | head -1 | sed -e \"s/^# *//\" -e \"s/ *$//\"", file_name) |
| 74 | + section_title = "<SECTION_TITLE_NOT_FOUND>" |
| 75 | + while ( ( cmd_get_section_title | getline result ) > 0 ) { |
| 76 | + section_title = result |
| 77 | + break |
| 78 | + } |
| 79 | + } else { |
| 80 | + cmd_get_chapter_title = sprintf("grep \"^## \" < \"%s\" | head -1 | sed -e \"s/^## *//\" -e \"s/ *$//\"", file_name) |
| 81 | + chapter_title = "<CHAPTER_TITLE_NOT_FOUND>" |
| 82 | + while ( ( cmd_get_chapter_title | getline result ) > 0 ) { |
| 83 | + chapter_title = result |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | +
|
| 88 | + if (section_id > 0) { |
| 89 | + introduction_ended = 1 |
| 90 | + } |
| 91 | +
|
| 92 | + if (new_section && section_id == 3) { |
| 93 | + annex_started = 1 |
| 94 | + } |
| 95 | + if (new_section && section_id > 3) { |
| 96 | + new_section = 0 |
| 97 | + } |
| 98 | + if (annex_started) { |
| 99 | + chapter_title = section_title |
| 100 | + } |
| 101 | +
|
| 102 | + main_section = introduction_ended && !annex_started |
| 103 | +
|
| 104 | + if (new_section) { |
| 105 | + if (main_section) { |
| 106 | + sec_title = section_title "\n" |
| 107 | + chapter_title = section_title |
| 108 | + } else { |
| 109 | + sec_title = "Annex\n\n." |
| 110 | + } |
| 111 | + printf("\n# %s\n", sec_title) |
| 112 | + } |
| 113 | +
|
| 114 | + if (main_section) { |
| 115 | + sep = "- " |
| 116 | + } else { |
| 117 | + sep = "\n" |
| 118 | + } |
| 119 | +
|
| 120 | + printf("%s[%s](%s)\n", sep, chapter_title, file_name) |
| 121 | +
|
| 122 | + annex_started_now = 0 |
| 123 | + section_title = "" |
| 124 | + chapter_title = "" |
| 125 | + last_section_id = section_id |
| 126 | + last_chapter_id = chapter_id |
| 127 | +} |
| 128 | +' > "SUMMARY.md" |
| 129 | +# REUSE-IgnoreEnd |
0 commit comments