-
Notifications
You must be signed in to change notification settings - Fork 3
/
preview.sh
executable file
·83 lines (74 loc) · 1.61 KB
/
preview.sh
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
#!/bin/bash
#
# Copyright 2017 Garrett D'Amore <[email protected]>
# Copyright 2017 Capitar IT Group BV <[email protected]>
# 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"
diagram_html="-r asciidoctor-diagram"
diagram_man=""
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}
eval diagram=\$diagram_${style}
for input in "$@"; do
base=$(basename $input)
base=${base%.adoc}
output=${tempdir}/${base}${suffix}
asciidoctor -D ${tempdir} -a imagesoutdir=${tempdir} -b ${backend} ${diagram} $input
$view ${output}
done