3 # Generate asciidoc manpage markup from Cogito script header.
4 # Copyright (c) Jonas Fonseca, 2005
6 # Takes a path to a Cogito script. Prints the manpage to stdout.
8 PACKAGE
=${CGPACKAGE:-cogito}
12 if [ ! -e "$command" ]; then
13 echo "$command does not exist" >&2
17 COMMAND
=$
(basename $command)
19 # FIXME: Show the SHA1 of the command below the TITLELINE.
21 # Generate the 'CG-COMMAND(1)' title from cg-command
23 # Asciidocs wants the title line ('====') to be as wide as the title.
24 TITLELINE
=$
(echo "$TITLE" |
sed 's/[0-9a-zA-Z()-]/=/g')
26 # Get `USAGE="cg-command args..."` line and make it `cg-command args...`
27 SYNOPSIS
=$
(sed -n '/^USAGE=/,0s/^USAGE="\(.*\)"/\1/p' < $command)
29 # Extract the script header.
30 HEADER
=$
(sed -n '3,/^$/s/^# *//p' < $command)
32 # Some scripts have copyright lines followed by 'Based on script by ...' lines.
33 # Include them so they are also put in the COPYRIGHT section.
34 COPYRIGHT
=$
(echo "$HEADER" |
sed -n '/^Copyright (c)/,/^$/p' \
37 # First line of the header contains the caption. Normalize it by lowercasing the
38 # start and stripping any punctuation.
39 CAPTION
=$
(echo "$HEADER" |
head -n 1 |
tr '[A-Z]' '[a-z]' |
sed 's/\.$//')
41 # Get remaining sections and carefully insert links to cogito commands when they
42 # were referenced as "`cg-command`". This way references from cg-* combos in
43 # code listings will be ignored.
44 BODY
=$
(echo "$HEADER" |
sed '0,/^$/d' \
45 |
sed 's/`\(cg-[a-z-]\+\)`/gitlink:\1[1]/g;s/^\(-.*\)::.*/\1::/')
51 section
=$
(echo "$BODY" |
sed -n '1,/^[-][-]*[-]$/p')
52 section_lines
=$
(echo "$section" |
wc -l)
53 lines
=$
(echo "$BODY" |
wc -l)
55 if [ $section_lines = $lines ]; then
59 section_end
=$
(($section_lines - 2))
60 DESCRIPTION
=$
(echo "$BODY" |
sed -n "1,${section_end}p")
61 BODY
=$
(echo "$BODY" |
sed -n "$((section_lines - 1)),\$p")
63 if [ "$(echo "$BODY" | head -n 1)" = "OPTIONS" ]; then
64 BODY
=$
(echo "$BODY" |
sed -n '3,$p')
65 section
=$
(echo "$BODY" |
sed -n "1,/^[-~][-~]*[-~]\$/p")
66 section_lines
=$
(echo "$section" |
wc -l)
67 lines
=$
(echo "$BODY" |
wc -l)
69 if [ $section_lines = $lines ]; then
72 section_end
=$
(($section_lines - 2))
73 OPTIONS
=$
(echo "$BODY" |
sed -n "1,${section_end}p")
74 MISC
=$
(echo "$BODY" |
sed -n "${section_end},\$p")
82 # cg(1) does not answer to the help options in the same way as the
83 # rest of the commands
84 if [ "$COMMAND" = "cg" ]; then
87 Print overview of Cogito commands. Same as gitlink:cg-help[1]."
94 Print user manual. The same as found in gitlink:$COMMAND[1]."
119 # Only indent the first paragraph of multi-paragraph list items.
121 echo "$OPTIONS" |
while read line
; do
130 [ "$multipara" ] || line
=" $line"
141 Print Cogito version.
152 $COMMAND is part of gitlink:${PACKAGE}[7],
153 a toolkit for managing gitlink:git[7] trees.