2 # Desc: Generates Mediawiki subpage navigation links
3 # Input: file text file with list of chapters
4 # stdin text with list of chapters
5 # Output: [[../Chapter 4|Next]], [[../Chapter 2|Previous]], [[../|Up]]
7 # Attrib: Steven Baltakatei Sandoval. (2024-01-29). reboil.com
10 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
11 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
12 must
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
14 # Note: May accept specified file or stdin, but not both
15 # Input: arg input text (newline delimited)
16 # stdin input text (newline delimited)
17 # Output: stdout output text (newline delimited)
18 # Depends: BK-2020-03 read_stdin (v0.1.1), yell(), die()
22 if [[ "$#" -gt 1 ]]; then die
"FATAL:Too many arguments ($#):$*"; fi;
24 if [[ "$#" -eq 1 ]] && [[ -f "$1" ]] && [[ ! -p /dev
/stdin
]]; then
25 while read -r line
; do
26 printf "%s\n" "$line";
27 done < "$1" && return 0; fi;
28 if [[ "$#" -eq 0 ]] && [[ -p /dev
/stdin
]]; then
29 read_stdin
&& return 0; fi;
30 die
"FATAL:Unknown error.";
33 # Desc: Consumes stdin; outputs as stdout lines
34 # Input: stdin (consumes)
35 # Output: stdout (newline delimited)
37 # return 1 stdin not present
38 # Example: printf "foo\nbar\n" | read_stdin
39 # Depends: GNU bash (version 5.1.16), GNU Coreutils 8.32 (cat)
41 # Attrib: Steven Baltakatei Sandoval (2024-01-29). reboil.com
42 local input_stdin output
;
45 if [[ -p /dev
/stdin
]]; then
46 input_stdin
="$(cat -)" ||
{
47 echo "FATAL:Error reading stdin." 1>&2; return 1; };
52 # Store as output array elements
54 if [[ -n $input_stdin ]]; then
55 while read -r line
; do
57 done < <(printf "%s\n" "$input_stdin") ||
{
58 echo "FATAL:Error parsing stdin."; return 1; };
62 printf "%s\n" "${output[@]}";
65 }; # read stdin to stdout lines
66 validate_subpage_list
() {
67 # Desc: Check for illegal characters in subpage titles
68 # Input: stdin unvalidated subpage list
69 # Output: stdout validated subpage list
70 # Depends: BK-2020-03 read_stdin(), yell(), die()
72 while read -r line
; do
74 # Reject chars illegal in Mediawiki page titles.
75 re_illegal
='[][><|}{#_]'; # match illegal page names chars #, <, >, [, ], _, {, |, }
76 if [[ "$line" =~
$re_illegal ]]; then
77 die
"FATAL:Illegal char. Not allowed: #, <, >, [, ], _, {, |, }:$line";
80 # Reject trailing spaces.
81 re_ts
=' $'; # match trailing space
82 if [[ "$line" =~
$re_ts ]]; then
83 die
"FATAL:Trailing spaces not allowed:$line";
86 # Replace some chars with HTML-style codes
87 ## replace ampersand & with & # must be first
88 ## replace double quote " with "
89 ## replace single quote ' with '
94 <<< "$line" )" || { echo "FATAL
:Error running
sed.
"; };
95 printf "%s
\n" "$line";
97 echo "FATAL
:Error reading stdin.
" 1>&2; return 1; };
100 # Input: stdin validated subpage list
101 # Output: stdout wikicode
105 while read -r line; do
106 #yell "$n:Processing line
:$line"; # debug
110 #declare -p lprev lcurr lnext; # debug
112 # Skip first iteration
113 if [[ "$n" -eq 0 ]]; then
114 # yell "$n:DEBUG
:Skipping first iteration.
"; # debug
116 #printf -- "----\n" 1>&2; # debug
119 # Handle first valid input set
120 # yell "$n:DEBUG
:Handling first valid input
set.
"; # debug
121 if [[ "$n" -eq 1 ]]; then
122 printf "[[..
/%s|Next
]], [[..
/|Up
]]\n" \
124 #printf -- "----\n" 1>&2; # debug
125 ((n++)); continue; fi;
127 # Handle middle lines
128 # yell "$n:DEBUG
:Handling middle lines.
"; # debug
129 printf "[[..
/%s|Next
]], [[..
/%s|Previous
]], [[..
/|Up
]]\n" \
132 #printf -- "----\n" 1>&2; # debug
139 printf "[[..
/%s|Previous
]], [[..
/|Up
]]\n" \
144 read_input "$@
" | validate_subpage_list | generate_wikicode;