aboutsummaryrefslogtreecommitdiffstats
path: root/bin/ssg-template
blob: 4da43648e8448b2d746852b56106bd73ac3d593c (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
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
#!/bin/sh

SRC_FILE="${1}"

_echo_ifset() {
	[ ! -z "${1}" ] && echo "${1}"
}

_get_value() {
	# 1: filename
	# 2: key
	# stdout: value
	local value
	local ret

	value=$(lowdown -T ms -X "${2}" "${1}" 2>/dev/null)
	ret="${?}"

	echo "${value:-${3}}"

	return "${ret}"
}

_get_value_date() {
	_get_value "${1}" "${2}" "1970-01-01"
}

_get_out_path() {
	# 1: filename
	# stdout: filename
	echo "${1}" | sed -e 's/^src/out/g' -e 's/.md$/.html/g' -e 's/_//g'
}

_get_final_path() {
	# 1: filename
	# stdout: filename
	_get_out_path "${1}" | sed 's/^out\///g'
}

_get_title() {
	# 1: filename
	basename "${1}" | sed 's/\..*//g' | tr - " "
}

_get_date_human() {
	# 1: filename
	date -j -f "%Y-%m-%d" $(_get_value_date ${1} "date") +"%B %d, %Y"
}

_get_index_files() {
	# 1: filename
	local path

	path=$(_get_value "${1}" index)
	find "src/${path}" -mindepth 1 -maxdepth 1
}

_sort_index_per_date() {
	# 1: filename
	local files
	local file
	local date

	for file in $(_get_index_files "${1}"); do
		date=$(_get_value_date "${file}" "date")
		files="${files} ${date}@${file}"
	done

	echo ${files} | tr " " "\n" | sort -rn | cut -d "@" -f 2
}

_get_filetype() {
	# 1: filename
	local filetype

	filetype=$(ls -ld "${1}" | cut -c 1)

	case "${filetype}" in
		-)
			filetype="file"
			;;
		d)
			filetype="directory"
			;;
	esac

	echo "${filetype}"
}

_get_chapters() {
	# 1: filename
	grep "^#" "${1}" | sed 's/# /#_/g'
}

_get_chapter_title() {
	# 1: chapter
	echo "${1}" | sed -e 's/#_//' -e 's/#//g'
}

_parse_option() {
	# 1: metadata
	# 2: key
	echo "${metadata}" | grep "^${2}"
}

_cleanup_tmp_file() {
	[ -f "${1}" ] && rm "${1}"
}

_parse_metadata() {
	# 1: filename
	sed -n '/^$/q;p' "${file}" | grep "^.*: .*$"
}

_render_metadata() {
	# 1: filename
	# 2: output
	_ENABLED_METADATA="date author"
	local key
	local value

	echo "<ul class=\"metadata\">"
	for key in ${_ENABLED_METADATA}; do
		value=$(lowdown -X "${key}" "${1}" 2>/dev/null)
		[ -z "${value}" ] && continue

		echo "<li>${key}: ${value}</li>"
	done
	echo "</ul>"
}

_render_index() {
	# 1: filename
	# 2: out
	# 3: path
	local file
	local path
	local title

	echo "<ul class=\"index\">"
	for file in $(_sort_index_per_date "${1}"); do
		path=$(_get_final_path "${file}")
		[ "${path}" = "$(_get_final_path ${1})" ] && continue

		title=$(_get_value "${file}" "title" "$(_get_title ${file})")

		echo "<li>"
		if _get_value "${1}" "index_show_date" >/dev/null; then
			echo "<span>$(_get_date_human ${file})</span>"
		fi
		echo "<a href=\"${path}\">${title}</a>"
		echo "</li>"
	done
	echo "</ul>"
}

_render_toc() {
	local chapter
	local chapter_title

	echo "<ul class=\"toc\">"
	for chapter in $(_get_chapters "${1}"); do
		chapter_title=$(_get_chapter_title "${chapter}")

		cat <<-EOF
		<li>
		<a href=#${chapter_title}>${chapter_title}</a>
		</li>
		EOF
	done
	echo "</ul>"
}
remember that computers suck.