aboutsummaryrefslogtreecommitdiff
path: root/docs/preview.sh
blob: 57e106a78fff61b64f728448aff59c4e177fe836 (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
#!/bin/bash
#
# Copyright 2017 Garrett D'Amore <garrett@damore.org>
# Copyright 2017 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.

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 hmc arg
do
	case "${arg}" in
	h)	style=html;;
	m)	style=man;;
	c)	cleanup=yes;;
	?)	echo "Usage: $0 [-h|-m] <files...>"; exit 1 ;;
	esac
done
shift $(( $OPTIND - 1 ))

open_man=${MAN}
open_html=${OPEN}
suffix_html=".html"
suffix_man=".man"
backend_html="html5"
backend_man="manpage"
version=$(cat $(dirname $0)/../.version)
name=nng

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

eval backend=\$backend_${style}
eval suffix=\$suffix_${style}
eval view=\$open_${style}

for input in "$@"; do
	base=$(basename $input)
	base=${base%.adoc}
	output=${tempdir}/${base}${suffix}
	asciidoctor -aversion-label=${name} -arevnumber=${version} \
		-b ${backend} -o ${output} $input
	$view $output
done