summaryrefslogtreecommitdiff
path: root/mk.sh
blob: d46f93c054815614002644465ef5bf8ea8598dd2 (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
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
#!/bin/bash

SCRIPTDIR=$(cd $(dirname "$0") && pwd)
PAGEDIR="$SCRIPTDIR/pages"
IMAGEDIR="$SCRIPTDIR/img"
TEMPLATEDIR="$SCRIPTDIR/templates"
UTILDIR="$SCRIPTDIR/util"
SITEMAP="$(pwd)/sitemap.xml"

source "$SCRIPTDIR/pagecfg.sh"

###############################################################################

pagemenu() {
	local current="$1"
	local basedir="$(dirname $current)"

	cat "$basedir/menu.csv" | while read line; do
		local name=$(echo $line | cut -d, -f1)
		local target=$(echo $line | cut -d, -f2)
		local template="$TEMPLATEDIR/menu_inactive.html"

		if [[ "$target" =~ ^\./.* ]]; then
			target=$(echo $target | sed "s#^./#/${basedir}/#g")
		fi

		if [ "$(basename $target)" == "$(basename $current)" ]; then
			template="$TEMPLATEDIR/menu_active.html"
		fi

		if [[ "$target" =~ \.md$ ]]; then
			target=$(echo $target | sed 's/\.md/.html/g')
		fi

		sed -e "s#TARGET#$target#g" -e "s#NAME#${name^}#g" "$template"
	done
}

generate_page() {
	local mdpath="$1"
	local pagename="${2^}"
	local longtitle="${3^}"

	sed -e "s#LONG_TITLE#$longtitle#g" \
	    -e "s#SHORT_TITLE#$pagename#g" \
            -e "s#STYLESHEET#/$STYLESHEET#g" \
            -e "s#FAVICON#/$FAVICON#g" \
            -e "s#LOGO#/$LOGO#g" \
            -e "s#URL#$URL#g" "$TEMPLATEDIR/page.html" | \
	while read pageline; do
		case "$pageline" in
		MENU)
			pagemenu "$mdpath"
			;;
		CONTENT)
			markdown "$mdpath"
			;;
		COPYRIGHT)
			"$UTILDIR/copyright.sh" "$mdpath"
			;;
		*)
			echo "$pageline"
			;;
		esac
	done
}

###############################################################################

sitemap_begin() {
	sed '/ENTRIES/Q' "$TEMPLATEDIR/sitemap.xml"
}

sitemap_entry() {
	"$UTILDIR/sitemap.sh" "$URL" "$1" "$2"

}

sitemap_end() {
	sed -e '1,/ENTRIES/ d' "$TEMPLATEDIR/sitemap.xml"
}

###############################################################################
cp "$TEMPLATEDIR/$STYLESHEET" .
cp -r "$IMAGEDIR" .

sitemap_begin > "$SITEMAP"

find "$PAGEDIR" -name pages.csv -printf "%P\n" |\
while read cfgfile; do
	cat "$PAGEDIR/$cfgfile" |\
	while read line; do
		mdpath="$(dirname $cfgfile)/$(echo $line | cut -d, -f1)"
		pagename="$(echo $line | cut -d, -f2)"
		longtitle="$(echo $line | cut -d, -f3)"
		htmlpath="$(echo $mdpath | sed 's/\.md/.html/g')"
		htmlpath_abs="$(pwd)/$htmlpath"
		abs_link_path=$(echo "$htmlpath" | sed -e 's#^./##' \
						       -e "s#^#/#")

		mkdir -p "$(dirname $htmlpath_abs)"

		pushd "$PAGEDIR" > /dev/null
		sitemap_entry "$mdpath" "$abs_link_path" >> "$SITEMAP"

		generate_page "$mdpath" "$pagename" "$longtitle" \
			      > "$htmlpath_abs"
		popd > /dev/null
	done
done

sitemap_end >> "$SITEMAP"

cp "$TEMPLATEDIR/robots.txt" .
echo "Sitemap: ${URL%/}/sitemap.xml" >> "robots.txt"