aboutsummaryrefslogtreecommitdiff
path: root/scripts/gen_docs.sh
blob: 3363d42f465c95f31fdc832ce53c8523603b0852 (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
#!/usr/bin/env bash

create_vim_doc() (
	local project_name="${1:?}"
	local target="${2:?}"
	local template="${3:?}"
	local cur_dir
	cur_dir="$(pwd)"

	if [[ -d "${target}" ]]; then
		cp "${template}" "${TMP_DIR}/ldoc.ltp" || return 1
	else
		echo "No such template exists"
		return 1
	fi

	if [[ -d "${target}" ]]; then
		ldoc -p "${project_name}" -t "${project_name} Docs" -u "${target}" -l "${TMP_DIR}" -d "${TMP_DIR}" || cleanup
		cd "${TMP_DIR}/modules" || exit 1
		cat "${project_name}".html "${project_name}"*.*.html >"${project_name}".txt || cleanup
	elif [[ -f "${target}" ]]; then
		ldoc -p "${project_name}" -t "${project_name} Docs" -u "${target}" -l "${TMP_DIR}" -d "${TMP_DIR}" || cleanup
		cd "${TMP_DIR}" || exit 1
		cat index.html >"${project_name}".txt || cleanup
	else
		echo "Invalid target"
		return 1
	fi
	echo "vim:tw=80:ts=8:noet:ft=help:norl:" >>"${project_name}".txt
	# format each line to be within 80 columns
	# replace <pre> and </pre> with > and < respectively
	# Sometimes running the command one time is not enough, reason unknown
	nvim --headless +"set tw=80" \
		+'%norm gqq' \
		+'%norm gqq' \
		+'%s/<pre>/>/g' \
		+'%s/<\/pre>/</g' \
		"${project_name}".txt \
		+"wqa" || {
		echo "Coundn't format with nvim, but help file be placed"
	}
	mkdir -p "${cur_dir}/doc"
	cp "${project_name}".txt "${cur_dir}/doc/${project_name}.txt" || cleanup
	echo
	echo "${cur_dir}/doc/${project_name}.txt" created

	return 0
)

main() {
	TMP_DIR="$(mktemp -d)"

	cleanup() { rm -rf "${TMP_DIR}" exit 0; }

	project_name="colorizer"
	if command -v ldoc 1>/dev/null; then
		# html docs
		ldoc -f discount -p "${project_name}" -t "${project_name} Docs" -u lua "${@}" -s doc || cleanup

		# vim docs
		create_vim_doc "${project_name}" lua doc/ldoc_vim.ltp || cleanup
	else
		echo "Error: Install ldoc first"
	fi

	cleanup
}

main "${@}"