version/0.2
[shelmfish.git] / helpm2text
blobfdb3b7614e483ef7be8a35df00d050ffdf12f27f
1 #!/bin/sh
2 # Copyright © 2014 Géraud Meyer <graud@gmx.com>
4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License version 2 as published by the
6 # Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 # details.
13 # You should have received a copy of the GNU General Public License along with
14 # this program. If not, see <http://www.gnu.org/licenses/>.
16 PROGRAM_NAME="helpm2text"
17 PROGRAM_VERSION="0.2"
18 # <<"HelpMessage"
19 #NAME
20 # helpm2text - format a HelpMessage to be displayed
21 #SYNOPSIS
22 # helpm2text [--] [<file>]
23 #HelpMessage
25 while [ $# -gt 0 ]
27 case $1 in
28 --version|-V) echo "$PROGRAM_NAME version $PROGRAM_VERSION"; exit ;;
29 --?*) echo >&2 "$PROGRAM_NAME: error: invalid option"; exit 255 ;;
30 --) shift; break ;;
31 *) break ;;
32 esac
33 shift
34 done
35 if [ $# -gt 1 ]
36 then echo >&2 "$PROGRAM_NAME: error: too many arguments"; exit 255
38 if [ $# -gt 0 ] && expr "$1" : '[a-zA-Z_][a-zA-Z_0-9]*=' >/dev/null
39 then set -- ./"$1"
42 awk '
43 # delete header
44 FNR == 1 && /^[ \t#]/ { next }
45 # delete comment
46 /^\/\// { next }
47 # detect blank lines
48 /^[ \t]*$/ { blank=1; print; next }
49 # put a blank line before a section heading if needed
50 !/^[ \t\/#]/ && noheader { if (!blank) print "" }
51 # end of header
52 { noheader = 1 }
53 # suppress special list item syntax
54 /^ *\t/ { sub(/^ */, "") }
55 # print
56 { blank=0; print }' "${1--}"
58 # <<"HelpMessage"
59 #DESCRIPTION
60 # helpm2text formats a HelpMessage to be displayed as text. HelpMessage is
61 # already a format prone to be viewed as is; helpm2text simply strips
62 # comments, deletes special list indentation and makes sure that each section
63 # starts by (at least) one blank line.
65 # If <file> is not given or is a dash, the HelpMessage is read from the
66 # standard input.
68 # If the HelpMessage does contain a lot of POD syntax, it may be more
69 # appropriate to convert to POD by helpm2pod(1) then to text by pod2text(1).
70 # pod2text(1) offers additonal options to format the document.
71 #EXIT STATUS
72 # A non zero exit status would indicate an error for awk itself.
73 #EXAMPLES
74 # The following two commands give similar results:
75 # $ <doc.helpm helpm2text | expand -t4 | fmt -w76
76 # $ <doc.helpm helpm2pod | pod2text
77 #AUTHOR
78 # helpm2text was written by G.raud Meyer.
79 #SEE ALSO
80 # helpmessage(5), helpm2pod(1), pod2text(1)
81 #HelpMessage