blob: f8ef010157906aa522adbe92a198afe4ff1957ec (
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
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
247
|
#!/bin/sh
media_dir="media"
src_dir="src"
tmp_dir="tmp"
out_dir="out"
tmp_file="${tmp_dir}/tmp"
rss_dir="${src_dir}/b"
rss_out_file="${out_dir}/rss.xml"
website_domain="rgoncalves.se"
website_title="${website_domain}"
website_link="https://${website_domain}"
website_email="contact@${website_domain}"
website_generator="${website_domain}'s ssg"
website_description="devops and hacker D:"
website_language="en"
date_default_hour="17:00:00"
date_default_timezone="+0200"
uname=$(uname)
__to_html_id() {
cat /dev/stdin | sed 's/ /%20/g'
}
__get_value() {
# 1: filename
# 2: Attribute name
# Retrieve and return the key/value of a lowdown source file.
lowdown -T ms -X "${2}" "${1}" 2>/dev/null
}
__get_value_title() {
# 1: filename
# Get the title of a file and return a safe string.
local _title
_title="$(__get_value ${1} title || true)"
if [ "${_title}" = "" ]; then
_title="$(basename ${1} |
rev |
cut -d "." -f 2 |
rev |
sed 's/-/ /g')"
fi
echo "${_title}"
}
__get_value_date() {
# 1: filename
# Get the date of a file and return a safe string.
local _date
_date="$(__get_value ${1} date || true)"
if [ "${_date}" = "" ]; then
_date="1970-01-01"
fi
echo "${_date}"
}
__get_value_date_human() {
# 1: filename
# Get a human readable date from a file and return a safe string.
if [ "${uname}" = "Linux" ]; then
__get_value_date "${1}" | xargs date +"%B %d, %Y" -d
else
__get_value_date "${1}" | xargs date -j -f "%Y-%m-%d" +"%B %d, %Y"
fi
}
__get_value_date_publication() {
# 1: filename
# Get publication date according to rfc 2822 and return a safe string.
local _date
_date="$(__get_value_date ${1})"
_date="${_date} ${date_default_hour} ${date_default_timezone}"
if [ "${uname}" = "Linux" ]; then
date -d "${_date}" +"%a, %d %b %Y %H:%M:%S %z"
else
date -j -f "%Y-%m-%d %H:%M:%S %z" +"%a, %d %b %Y %H:%M:%S %z" "${_date}"
fi
}
__get_out_filename() {
# 1: filename
# Convert the source filename to its output destination.
local _filename
_filename="${1}"
if $(__get_value "${1}" draft || false); then
_filename="$(dirname ${1})/$(basename ${1} | sha256).html"
fi
echo "${_filename}" |
sed 's/.md$/.html/g' |
sed "s/${src_dir}/${out_dir}/g"
}
__get_final_filename() {
# 1: filename
# Convert the source filename to its final filename for a webserver.
__get_out_filename "${1}" | cut -d "/" -f 2-
}
__list_files() {
# 1: directory
# List all regular files in a directory and its subdirectories.
find "${1}" -type f
}
__list_files_date() {
# 1: directory
local _files
local _file
local _tmp_file
_files=$(__list_files "${1}")
_tmp_file="${tmp_file}.list_files_date"
cp /dev/null "${_tmp_file}"
for _file in ${_files}; do
echo "$(__get_value_date ${_file})" "${_file}" >> "${_tmp_file}"
done
sort -r "${_tmp_file}" | cut -d " " -f 2-
}
__install() {
# 1: filename
# Copy a file to the output directory.
install -D -m 0644 "${1}" "${2}"
}
__lowdown() {
# Output a file to html
lowdown --html-no-skiphtml --html-no-escapehtml "${1}"
}
__apply_template() {
# 1: template name or default
m4 "templates/${1:-default}"
}
__generate_index_line() {
# 1: filename where index will be displayed
# 2: filename contained in line
local _show_date
_show_date="$(__get_value ${1} index_date || echo false)"
if "${_show_date}"; then
cat <<-EOF
<span>
$(__get_value_date_human ${2})
</span>
EOF
fi
cat <<-EOF
<a href="/$(__get_final_filename ${2})">
$(__get_value_title ${2})
</a>
EOF
}
__generate_index() {
# 1: filename
# Generate and output to stdout the index of a file.
local _file
local _index_dir
_index_dir="${src_dir}/$(__get_value "${1}" index)"
[ -f "${_index_dir}" ] && _index_dir=$(dirname "${_index_dir}")
[ ! -d "${_index_dir}" ] && return
echo "<ul class=\"index\">"
for _file in $(__list_files_date "${_index_dir}"); do
[ "${1}" = "${_file}" ] && continue
__get_value "${_file}" "draft" >/dev/null && continue
echo "<li>"
__generate_index_line "${1}" "${_file}"
echo "</li>"
done
echo "</ul>"
}
__generate_metadata() {
# 1: filename
# Generate metadata with title
local _title
local _show_title
_show_title=$(__get_value "${1}" show_title || true)
if [ "${_show_title}" = "" ]; then
_title=$(__get_value_title "${1}")
_title_id=$(echo ${_title} | __to_html_id)
cat <<-EOF
<h1 id="${_title_id}">
${_title}
</h1>
EOF
fi
}
__handle_md() {
# 1: filename
# Handle markdown files.
__lowdown "${1}" > "${tmp_file}.body"
__generate_metadata "${1}" > "${tmp_file}.metadata"
if [ ! "$(__get_value ${1} index)" = "" ]; then
__generate_index "${1}" > "${tmp_file}.index"
else
[ -f "${tmp_file}.index" ] && rm "${tmp_file}.index"
fi
__apply_template "$(__get_value ${1} template)" > "${tmp_file}"
__install "${tmp_file}" "$(__get_out_filename ${1})"
}
__handle() {
# 1: filename
# Handle file depending on its filename and filetype.
case "${1}" in
*.md)
__handle_md "${1}"
;;
*)
__install "${1}" "$(__get_out_filename ${1})"
;;
esac
}
|