summaryrefslogtreecommitdiff
path: root/aux/commonmark.sh
blob: 7f56e53c4d70ce8e9d7af283a63a58888b9041c5 (plain) (blame)
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
#!/bin/sh
set -eu

. tests/lib.sh


usage() {
	cat <<-'EOF'
		Usage:
		sh aux/commonmark.sh -N NAME_UC -t TITLE -l LANG [-H HEADER]
		sh aux/commonmark.sh -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -N NAME_UC    the UpperCase name of the project
		  -t TITLE      the title of the page
		  -l LANG       the language of this page
		  -H HEADER     extra header content to be included as a file
		  -h, --help    show this message


		Consume CommonMark data from STDIN and emit HTML5 to STDOUT,
		using NAME_UC, TITLE and LANG as metadata, and allowing extra
		HTML to be injected via HEADER.


		Examples:

		  Generate `index.html` from `README.md`:

		    $ sh aux/commonmark.sh -N MyProj -t Home -l en -H extra.html < README.md > index.html
	EOF
}


for flag in "$@"; do
	case "$flag" in
		(--)
			break
			;;
		(--help)
			usage
			help
			exit
			;;
		(*)
			;;
	esac
done

while getopts 'N:t:l:H:h' flag; do
	case "$flag" in
		(N)
			NAME_UC="$OPTARG"
			;;
		(t)
			TITLE="$OPTARG"
			;;
		(l)
			THE_LANG="$OPTARG"
			;;
		(H)
			HEADER="$OPTARG"
			;;
		(h)
			usage
			help
			exit
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

eval "$(assert_arg "${NAME_UC:-}" '-N NAME_UC')"
eval "$(assert_arg "${TITLE:-}"      '-t TITLE')"
eval "$(assert_arg "${THE_LANG:-}"   '-l THE_LANG')"


THE_TITLE="$NAME_UC | $TITLE"

pandoc \
	--toc                         \
	--toc-depth=2                 \
	-s                            \
	--metadata title="$THE_TITLE" \
	--metadata "lang=$THE_LANG"   \
	-r commonmark                 \
	-w html                       \
	-H aux/favicon.html           \
	${HEADER:+-H} ${HEADER:-}