summaryrefslogtreecommitdiff
path: root/docs/man/preview.sh
blob: dc0b391fef60fb4bfbe8bffd0a2763d6a38c66b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/ksh
#
# Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
# Copyright 2018 Capitar IT Group BV <info@capitar.com>
# This software is supplied under the terms of the MIT License, a
# copy of which should be located in the distribution where this
# file was obtained (LICENSE.txt).  A copy of the license may also be
# found online at https://opensource.org/licenses/MIT.
#

# This script is used to preview in HTML or man format, the documentation.
# I use it during development, YMMV.  It is probably completely useless
# on Windows.

MANSOURCE="NNG"
MANMANUAL="NNG Reference Manual"

case $(uname -s) in
Darwin)
	OPEN=open
	MAN=man
	;;
Linux)
	OPEN=xdg-open
	MAN=man
	;;
*)
	echo "No idea how to preview on this system."
	exit 2
esac

if [ -n "$DISPLAY" ]
then
	style=html
else
	style=man
fi

while getopts cs: arg
do
	case "${arg}" in
	c)	cleanup=yes;;
	s)	style=$OPTARG;;
	?)	echo "Usage: $0 [-s <style>][-c] <files...>"; exit 1 ;;
	esac
done
shift $(( $OPTIND - 1 ))

case $style in
html)
	suffix=.html
	;;
ps)
	suffix=.ps
	;;
pdf)
	suffix=.pdf
	;;
man)
	suffix=.man
	OPEN=${MAN}
	;;
*)
	echo "Unknown style, choose one of [html|man|pdf|ps]." 1>&2
	exit 2
esac

version=PREVIEW
name=nng

generate_pdf() {
	typeset input=$1
	typeset output=$2
	asciidoctor-pdf -aversion-label=${name} -arevnumber=${version} \
		-asource-highlighter=pygments -aicons=font \
		-a mansource="${MANSOURCE}" -a manmanual="${MANMANUAL}" \
		-b pdf -a notitle -d article -o ${output} $input
}

generate_html() {
	typeset input=$1
	typeset output=$2
	asciidoctor -atoc=left -aversion-label=${name} -arevnumber=${version} \
		-aicons=font -asource-highlighter=pygments \
		-a mansource="${MANSOURCE}" -a manmanual="${MANMANUAL}" \
		-b html5 -d manpage -o ${output} $input
}

generate_man() {
	typeset input=$1
	typeset output=$2
	asciidoctor -aversion-label=${name} -arevnumber=${version} \
		-a mansource="${MANSOURCE}" -a manmanual="${MANMANUAL}" \
		-d manpage -b manpage -o ${output} $input
}

generate_ps() {
	typeset input=$1
	typeset output=$2
	manpage=${2%.ps}.man
	generate_man $1 $manpage
	if [ $? -eq 0 ]; then
		man -t $manpage > $output
	fi
}


if [ -n "${cleanup}" ]
then
	tempdir=$(mktemp -d)
	clean() {
		sleep 1
		rm -rf ${tempdir}
	}
	trap clean 0
	mkdir -p ${tempdir}
else
	tempdir=/tmp/${LOGNAME}.${name}.preview
	mkdir -p ${tempdir}
fi

for input in "$@"; do
	subdir=$(dirname $input)
	parent=$(basename $subdir)
	case "${parent}" in
	man[0-9a-zA-Z_]*)
		echo doing subdir ${parent}
		subdir=${parent}/
		outdir="${tempdir}/${subdir}"
		[[ -d ${outdir} ]] || mkdir -p ${outdir}
		;;
	*)
		subdir=""
		outdir="${tempdir}"
		;;
	esac
	base=$(basename $input)
	base=${base%.adoc}
	output=${outdir}/${base}${suffix}
	generate_${style} $input $output
	$OPEN $output
done