summaryrefslogtreecommitdiff
path: root/plain.sh
blob: 530c59265897ff61c7529f40d94c36ae31442f01 (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
#!/bin/sh
set -e

# usage: ./plain.sh <input txt> <output html>

# Copyright 2019-2020 DistressNetwork° <uplink@distress.network>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# i/o variables

IN="$1"
OUT="$2"

[ -f $IN ] || { echo "---- [FATAL] $IN not found ----" ; exit 1 ; }


# initial write

cat plain-header > $2
cat $1 >> $2
printf "</pre>\n</body>\n</html>" >> $2
echo '[ok] initial assembly'

# hack to fix double <pre> when generating from nav.sh

sed -e "/^<pre>$/ {n;/^<pre>$/d}" -e "/^<\/pre>$/ {n;/^<\/pre>$/d}" -i'' $2


# metadata

sedesc() {
	sed -e 's;\&;\\\&;' -e 's/;/\\\;/' -e 's;-->;;'
}

TITLE=$(grep -e '^<!--title: ' -m 1 $1 | cut -f 2- -d ' ' | sedesc)
sed -e "/^<!--title:/d" -e "s;<title>#;<title>${TITLE};1" -i'' $2
printf '[ok] title: %s\n' "$TITLE"

MDATE=$(stat -c '%y' $1 | sed -e 's/\.[0-9]* //' -e 's/\(..\)\(..\)$/\1:\2/') # ISO 8601 format: "yyyy-mm-dd hh:mm:ss±zz:zz"
sed "/^<\/head>/i <meta http-equiv=\"last-modified\" content=\"${MDATE}\" />" -i'' $2
printf '[ok] date: %s\n' "$MDATE"


echo "---- build complete: $2 ----"