blob: edde671da238b95c6920fefafdaa70aec2774a6e [file] [log] [blame]
Gavin Howarded6860d2020-04-07 14:17:29 -06001#! /bin/sh
2#
Gavin Howard29e00ba2020-06-30 09:25:21 -06003# SPDX-License-Identifier: BSD-2-Clause
4#
Gavin Howarded6860d2020-04-07 14:17:29 -06005# Copyright (c) 2018-2020 Gavin D. Howard and contributors.
6#
Gavin Howarded6860d2020-04-07 14:17:29 -06007# 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
30usage() {
Gavin Howard69a49952020-06-28 12:45:32 -060031 printf "usage: %s manpage\n" "$0" 1>&2
Gavin Howarded6860d2020-04-07 14:17:29 -060032 exit 1
33}
34
Gavin Howard966a5612020-10-28 14:07:00 -060035print_manpage() {
36
37 _print_manpage_md="$1"
38 shift
39
40 _print_manpage_out="$1"
41 shift
42
43 cat "$manualsdir/header.txt" > "$_print_manpage_out"
44 cat "$manualsdir/header_${manpage}.txt" >> "$_print_manpage_out"
45
46 pandoc -f markdown -t man "$_print_manpage_md" >> "$_print_manpage_out"
47
48}
49
Gavin Howard69a49952020-06-28 12:45:32 -060050gen_manpage() {
51
52 _gen_manpage_args="$1"
53 shift
54
55 _gen_manpage_status="$ALL"
56 _gen_manpage_out="$manualsdir/$manpage/$_gen_manpage_args.1"
Gavin Howarda3372682020-06-29 21:36:17 -060057 _gen_manpage_md="$manualsdir/$manpage/$_gen_manpage_args.1.md"
58 _gen_manpage_temp="$manualsdir/temp.1.md"
Gavin Howard69a49952020-06-28 12:45:32 -060059 _gen_manpage_ifs="$IFS"
60
Gavin Howarda3372682020-06-29 21:36:17 -060061 rm -rf "$_gen_manpage_out" "$_gen_manpage_md"
Gavin Howard69a49952020-06-28 12:45:32 -060062
63 while IFS= read -r line; do
64
65 if [ "$line" = "{{ end }}" ]; then
66
67 if [ "$_gen_manpage_status" -eq "$ALL" ]; then
68 err_exit "{{ end }} tag without corresponding start tag" 2
69 fi
70
71 _gen_manpage_status="$ALL"
72
73 elif [ "${line#\{\{* $_gen_manpage_args *\}\}}" != "$line" ]; then
74
75 if [ "$_gen_manpage_status" -ne "$ALL" ]; then
76 err_exit "start tag nested in start tag" 3
77 fi
78
79 _gen_manpage_status="$NOSKIP"
80
81 elif [ "${line#\{\{*\}\}}" != "$line" ]; then
82
83 if [ "$_gen_manpage_status" -ne "$ALL" ]; then
84 err_exit "start tag nested in start tag" 3
85 fi
86
87 _gen_manpage_status="$SKIP"
88
89 else
90 if [ "$_gen_manpage_status" -ne "$SKIP" ]; then
Gavin Howardbac7e3b2020-06-29 21:20:45 -060091 printf '%s\n' "$line" >> "$_gen_manpage_temp"
Gavin Howard69a49952020-06-28 12:45:32 -060092 fi
93 fi
94
Gavin Howarda3372682020-06-29 21:36:17 -060095 done < "$manualsdir/${manpage}.1.md.in"
Gavin Howard69a49952020-06-28 12:45:32 -060096
Gavin Howarda3372682020-06-29 21:36:17 -060097 uniq "$_gen_manpage_temp" "$_gen_manpage_md"
Gavin Howardbac7e3b2020-06-29 21:20:45 -060098 rm -rf "$_gen_manpage_temp"
99
Gavin Howard69a49952020-06-28 12:45:32 -0600100 IFS="$_gen_manpage_ifs"
101
Gavin Howard966a5612020-10-28 14:07:00 -0600102 print_manpage "$_gen_manpage_md" "$_gen_manpage_out"
Gavin Howard69a49952020-06-28 12:45:32 -0600103}
104
Gavin Howarded6860d2020-04-07 14:17:29 -0600105set -e
106
107script="$0"
108scriptdir=$(dirname "$script")
Gavin Howard69a49952020-06-28 12:45:32 -0600109manualsdir="$scriptdir/manuals"
Gavin Howarded6860d2020-04-07 14:17:29 -0600110
Gavin Howard69a49952020-06-28 12:45:32 -0600111. "$scriptdir/functions.sh"
Gavin Howarded6860d2020-04-07 14:17:29 -0600112
Gavin Howard69a49952020-06-28 12:45:32 -0600113ARGS="A E H N P EH EN EP HN HP NP EHN EHP ENP HNP EHNP"
114ALL=0
115NOSKIP=1
116SKIP=2
117
118test "$#" -eq 1 || usage
119
120manpage="$1"
Gavin Howarded6860d2020-04-07 14:17:29 -0600121shift
122
Gavin Howard966a5612020-10-28 14:07:00 -0600123if [ "$manpage" != "bcl" ]; then
124
125 for a in $ARGS; do
126 gen_manpage "$a"
127 done
128
129else
130 print_manpage "$manualsdir/${manpage}.3.md" "$manualsdir/${manpage}.3"
131fi