summaryrefslogtreecommitdiff
path: root/docs/man/publish.sh
blob: 63a1a26cb99a4b8f765285891854ad3647e0075d (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/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 program attempts to publish updated documentation to our gh-pages
# branch.
# 
# We read the .version file from ../.version.
# 
# The docs are published into the gh-pages branch, in a directory
# called man/v<version>.
# 
# This script requires asciidoctor, pygments, git, and a UNIX shell.
# 

tmpdir=$(mktemp -d)
srcdir=$(dirname $0)
dstdir=${tmpdir}/pages
cd ${srcdir}
MANMANUAL="NNG Reference Manual"
MANSOURCE="NNG"
LAYOUT=refman
name=nng


TIPVERS=$(cd ${srcdir}; git describe --always origin/master)
GITVERS=$(cd ${srcdir}; git describe --always)

if [[ -z "${VERSION}" ]]
then
	if [[ "${GITVERS}" == *-* ]]
	then
		if [[ "${GITVERS}" == "${TIPVERS}" ]]
		then
			VERSION=tip
		else
			printf "Cannot publish - sources not pushed yet.\n"
			exit 1
		fi
	else
		VERSION="${GITVERS}"
	fi
fi

printf "PUBLISHING version ${VERSION}\n"

if [ ${VERSION} == tip ]
then
	dstman=${dstdir}/man/tip
else
	dstman=${dstdir}/man/v${VERSION}
fi

giturl="${GITURL:-git@github.com:nanomsg/nng}"

cleanup() {
	printf "DELETING ${tmpdir}\n"
	rm -rf ${tmpdir}
}

getinfo() {
	PAGE=
	SECT=
	DESC=

	while read line
	do
		case "$line" in
		"//"*)
			;;
		"= "**|"="*)
			if [ -z "${PAGE}" ] && [ -z "${SECT}" ]
			then
				PAGE=${line%\(}
				PAGE=${PAGE#=}
				PAGE=${PAGE## }
				PAGE=${PAGE%\(*}
				SECT=${line#*\(}
				SECT=${SECT%\)}
			fi
			;;

		*" - "*)
			if [ -z "${DESC}" ] && [ -n "${PAGE}" ] && [ -n "${SECT}" ]
			then
				DESC=${line#*- }
				return
			fi
			;;
		esac
	done
}

mkdir -p ${tmpdir}

trap cleanup 0

typeset -A descs
typeset -A pages
echo git clone ${giturl} ${dstdir} || exit 1
git clone ${giturl} ${dstdir} || exit 1

(cd ${dstdir}; git checkout gh-pages)

[ -d ${dstman} ] || mkdir -p ${dstman}

dirty=
files=( $(find . -name '*.adoc' -print | sort ) )

printf "Processing files: [%3d%%]" 0
typeset -i num
typeset -i pct

num=0
pct=0
for input in ${files[@]}
do 
	num=$(( num + 1 ))
	pct=$(( num * 100 / ${#files[@]} ))

	printf "\b\b\b\b\b\b[%3d%%]" ${pct}
	adoc=${input#./}
	html=${adoc%.adoc}.html
	output=${dstman}/${html}

	status=$(git status -s $input )
	when=$(git log -n1 --format='%ad' '--date=format-local:%s' $input )
	cat <<EOF > ${output}
---
version: ${VERSION}
layout: ${LAYOUT}
---
EOF

	if [ -n "$when" ]
	then
		epoch="SOURCE_DATE_EPOCH=${when}"
	else
		epoch=
		dirty=yes
	fi
	if [ -n "$status" ]
	then
		printf "\nFile $adoc is not checked in!\n"
		dirty=yes
	fi

	getinfo < ${adoc}
	if [ -n "${DESC}" ]
	then
		descs[${PAGE}_${SECT}]="$DESC"
		pages[${SECT}]=( ${pages[$SECT][*]} $PAGE )
	fi

	 env ${epoch} asciidoctor \
		-dmanpage \
		-amansource="${MANSOURCE}" \
		-amanmanual="${MANMANUAL}" \
		-anofooter=yes \
		-askip-front-matter \
		-atoc=left \
		-asource-highlighter=pygments \
		-aicons=font \
		-bhtml5 \
		-o - ${adoc} >> ${output}
	chmod 0644 ${output}

	if [ $? -ne 0 ]
	then
		printf "\n$Failed to process $adoc !\n"
		fails=yes
	fi

	(cd ${dstman}; git add ${html})
done

printf "\nProcessing index: "

index=${dstman}/index.asc

cat <<EOF > ${index}
= NNG Reference Manual: ${VERSION}

The following pages are present:

EOF

typeset -A titles
titles[1]="Utilities and Programs"
titles[3]="Library Functions"
titles[7]="Protocols and Transports"

for S in $(echo ${!pages[@]} | sort )
do
	printf "\n== Section ${S}: ${titles[$S]}\n"

	printf "\n[cols=\"3,5\"]\n"
	printf "|===\n"
	for P in $(echo ${pages[$S][@]} | tr " " "\n" | sort)
	do
		printf "|<<${P}#,${P}(${S})>>\n"
		printf "|${descs[${P}_${S}]}\n"
		printf "\n"
	done
	printf "|===\n"
done >> ${index}

cat <<EOF >${dstman}/index.html
---
version: ${VERSION}
layout: ${LAYOUT}
---
EOF

env ${epoch} asciidoctor \
	-darticle \
	-anofooter=yes \
	-askip-front-matter \
	-atoc=left \
	-aicons=font \
	-bhtml5 \
	-o - ${index} >> ${dstman}/index.html
chmod 0644 ${dstman}/index.html

(cd ${dstman}; git add index.html)

if [ -n "$dirty" ]
then
	printf "Repository has uncommited documentation.  Aborting.\n"
	exit 1
fi

if [ -n "$fails" ]
then
	printf "\nFailures formatting documentation. Aborting.\n"
	exit 1
fi
printf "Done.\n"

(cd ${dstman}; git commit -m "man page updates for ${VERSION}"; git push origin gh-pages)