Gavin Howard | ed6860d | 2020-04-07 14:17:29 -0600 | [diff] [blame] | 1 | #! /bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2018-2020 Gavin D. Howard and contributors. |
| 4 | # |
| 5 | # All rights reserved. |
| 6 | # |
| 7 | # Redistribution and use in source and binary forms, with or without |
| 8 | # modification, are permitted provided that the following conditions are met: |
| 9 | # |
| 10 | # * Redistributions of source code must retain the above copyright notice, this |
| 11 | # list of conditions and the following disclaimer. |
| 12 | # |
| 13 | # * Redistributions in binary form must reproduce the above copyright notice, |
| 14 | # this list of conditions and the following disclaimer in the documentation |
| 15 | # and/or other materials provided with the distribution. |
| 16 | # |
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | # POSSIBILITY OF SUCH DAMAGE. |
| 28 | # |
| 29 | |
| 30 | usage() { |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 31 | printf "usage: %s manpage\n" "$0" 1>&2 |
Gavin Howard | ed6860d | 2020-04-07 14:17:29 -0600 | [diff] [blame] | 32 | exit 1 |
| 33 | } |
| 34 | |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 35 | gen_manpage() { |
| 36 | |
| 37 | _gen_manpage_args="$1" |
| 38 | shift |
| 39 | |
| 40 | _gen_manpage_status="$ALL" |
| 41 | _gen_manpage_out="$manualsdir/$manpage/$_gen_manpage_args.1" |
Gavin Howard | a337268 | 2020-06-29 21:36:17 -0600 | [diff] [blame^] | 42 | _gen_manpage_md="$manualsdir/$manpage/$_gen_manpage_args.1.md" |
| 43 | _gen_manpage_temp="$manualsdir/temp.1.md" |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 44 | _gen_manpage_ifs="$IFS" |
| 45 | |
Gavin Howard | a337268 | 2020-06-29 21:36:17 -0600 | [diff] [blame^] | 46 | rm -rf "$_gen_manpage_out" "$_gen_manpage_md" |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 47 | |
| 48 | while IFS= read -r line; do |
| 49 | |
| 50 | if [ "$line" = "{{ end }}" ]; then |
| 51 | |
| 52 | if [ "$_gen_manpage_status" -eq "$ALL" ]; then |
| 53 | err_exit "{{ end }} tag without corresponding start tag" 2 |
| 54 | fi |
| 55 | |
| 56 | _gen_manpage_status="$ALL" |
| 57 | |
| 58 | elif [ "${line#\{\{* $_gen_manpage_args *\}\}}" != "$line" ]; then |
| 59 | |
| 60 | if [ "$_gen_manpage_status" -ne "$ALL" ]; then |
| 61 | err_exit "start tag nested in start tag" 3 |
| 62 | fi |
| 63 | |
| 64 | _gen_manpage_status="$NOSKIP" |
| 65 | |
| 66 | elif [ "${line#\{\{*\}\}}" != "$line" ]; then |
| 67 | |
| 68 | if [ "$_gen_manpage_status" -ne "$ALL" ]; then |
| 69 | err_exit "start tag nested in start tag" 3 |
| 70 | fi |
| 71 | |
| 72 | _gen_manpage_status="$SKIP" |
| 73 | |
| 74 | else |
| 75 | if [ "$_gen_manpage_status" -ne "$SKIP" ]; then |
Gavin Howard | bac7e3b | 2020-06-29 21:20:45 -0600 | [diff] [blame] | 76 | printf '%s\n' "$line" >> "$_gen_manpage_temp" |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 77 | fi |
| 78 | fi |
| 79 | |
Gavin Howard | a337268 | 2020-06-29 21:36:17 -0600 | [diff] [blame^] | 80 | done < "$manualsdir/${manpage}.1.md.in" |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 81 | |
Gavin Howard | a337268 | 2020-06-29 21:36:17 -0600 | [diff] [blame^] | 82 | uniq "$_gen_manpage_temp" "$_gen_manpage_md" |
Gavin Howard | bac7e3b | 2020-06-29 21:20:45 -0600 | [diff] [blame] | 83 | rm -rf "$_gen_manpage_temp" |
| 84 | |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 85 | IFS="$_gen_manpage_ifs" |
| 86 | |
Gavin Howard | a337268 | 2020-06-29 21:36:17 -0600 | [diff] [blame^] | 87 | cat "$manualsdir/header.txt" > "$_gen_manpage_out" |
| 88 | cat "$manualsdir/header_${manpage}.txt" > "$_gen_manpage_out" |
| 89 | pandoc -f markdown -t man >> "$_gen_manpage_out" |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 90 | } |
| 91 | |
Gavin Howard | ed6860d | 2020-04-07 14:17:29 -0600 | [diff] [blame] | 92 | set -e |
| 93 | |
| 94 | script="$0" |
| 95 | scriptdir=$(dirname "$script") |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 96 | manualsdir="$scriptdir/manuals" |
Gavin Howard | ed6860d | 2020-04-07 14:17:29 -0600 | [diff] [blame] | 97 | |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 98 | . "$scriptdir/functions.sh" |
Gavin Howard | ed6860d | 2020-04-07 14:17:29 -0600 | [diff] [blame] | 99 | |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 100 | ARGS="A E H N P EH EN EP HN HP NP EHN EHP ENP HNP EHNP" |
| 101 | ALL=0 |
| 102 | NOSKIP=1 |
| 103 | SKIP=2 |
| 104 | |
| 105 | test "$#" -eq 1 || usage |
| 106 | |
| 107 | manpage="$1" |
Gavin Howard | ed6860d | 2020-04-07 14:17:29 -0600 | [diff] [blame] | 108 | shift |
| 109 | |
Gavin Howard | 69a4995 | 2020-06-28 12:45:32 -0600 | [diff] [blame] | 110 | for a in $ARGS; do |
| 111 | gen_manpage "$a" |
| 112 | done |