summaryrefslogtreecommitdiff
path: root/build.sh
blob: 3a2cd6b5fbdcd3614d03cd4a6a49d98de11fe23b (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
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
#!/usr/bin/env bash

BUILD=.build/

function extract_metadata() {
    name=$1
    file=$2
   # val=$(grep -oE "^--${name}: .+$" $file | sed -E "s/^--${name}: (.+)$/\1/")
    val=$(sed -nE "/^--${name}: .+$/s/^--${name}: (.+)$/\1/p" $file)
    if [ -z "$val" ]; then
	echo ""
	return 1
    else
	echo "$val"
	return 0
    fi
}

function generate_index() {
    base_dir=$1
    index_tmp=$(mktemp -p $BUILD "index_${base_dir}_XXXXXX.html")
    echo "<ul>" > $index_tmp
    find -type f -iname "*.html" -exec echo "<li><a href=\"{}\">{}\</a></li>" \; >> $index_tmp
    echo "</ul>" >> $index_tmp
    page_tmp=$(mktemp -p $BUILD "index_page_XXXXXX.html")
    cat _templates/page.html > $page_tmp
    sed -i -e "/CONTENT/ {
    	\<ul\>
    	r $index_tmp
	\<\/ul\>
	d}" $page_tmp
}

echo "Clearing build-cache"
rm -rf $BUILD
mkdir $BUILD

echo "Cleaning up prod folders"
echo "Deleting www"
rm -rf www
mkdir www

echo "Deleting gemini"
rm -rf gemini
mkdir gemini

echo "Building html"
# TODO
for post in _posts/*.html; do
    post_date=$(extract_metadata "post-date" $post)
    if [ -z "$post_date" ]; then
	# post is not ready to be posted
	# requires a post date
	echo "No data"
	continue;
    fi
    updated_at=$(extract_metadata "updated-at" $post)
    tags=$(extract_metadata "tags" $post)
    type=$(extract_metadata "type" $post)
    
    post_file=$(mktemp -p $BUILD "post_XXXXXXX.html")
    content_file=$(mktemp -p $BUILD "content_XXXXXX.html")
    updated_html_file=$(mktemp -p $BUILD "updated_html_XXXXXX.html")
    tags_html_file=$(mktemp -p $BUILD "tags_html_XXXXXX.html")

    echo "Using $post_file for file"
    cat _templates/page.html > $post_file
    
    echo "Using $content_file for content"
    grep -vE "^--.+: .+$" $post > $content_file

    echo "Injecting content from $content_file into $post_file"
    sed -i -e "/CONTENT/ {
    	r ${content_file}
	d}" $post_file

    echo "Injecting CSS into $post_file"
    sed -i -e "/STYLE/ {
    	r _templates/index.css
	d}" $post_file
 
    echo "Extracting page title"
    page_title=$(grep -oE -m 1 "<h1>.+</h1>" $content_file | sed -E "s,<h1>(.+)</h1>,\1,")
    sed -i -E "s/[{]PAGE_TITLE[}]/$page_title/1" $post_file

    echo "Setting type"
    if [ -z "$type" ]; then
	sed -i -E "s/[{]TYPE[}]//" $post_file
    else
	sed -i -E "s,[{]TYPE[}],/${type}," $post_file
    fi

    echo "Setting post date"
    sed -i -E "s/[{]POST_DATE_ISO[}]/${post_date}/" $post_file
    display_date=$(date -d "${post_date}" +"%B %d, %Y")
    sed -i -E "s/[{]POST_DATE_DISP[}]/${display_date}/" $post_file

    echo "Setting updated at"
    if [ -z "$updated_at" ]; then
	sed -i -E "s/[{]UPDATED[}]//" $post_file
    else
	updated_disp=$(date -d "${updated_at}" +"%B %d, %Y")
	echo "&emsp;Updated at: <time datetime=\"${updated_at}\">${updated_disp}</time>" \
	     > updated_html_file
	sed -i -e "/UPDATED/{ r ${updated_html_file} d}" $post_file
    fi
    echo "Setting tags"
    if [ -z "$tags" ]; then
	sed -i -E "s/[{]TAGS[}]//" $post_file
    else
	echo "Using tags file $tags_html_file"
	echo "&emsp; Tags: [ ${tags} ]" > $tags_html_file
	sed -i -e "/TAGS/ {
	    r $tags_html_file
	    d}" $post_file
    fi

    filename=$(basename $post)
    if [ "$type" == "blog" ]; then
	year=$(date -d "$post_date" +%Y)
	month=$(date -d "$post_date" +%m)
	day=$(date -d "$post_date" +%d)
    	mkdir -p www/blog/$year/$month/$day
	cp $post_file www/blog/$year/$month/$day/$filename
    else
	cp $post_file www/$filename
    fi
done

echo "Copying static-html"
cp -r static-html/* www/
echo "Copying res to www"
cp -r res/ www/media/
echo "Building http rss"
# TODO
./tidy.sh

echo "Generating index files"
for [ obj in www/blog/* ]; do
    if [! -d $obj ]; then
	continue;
    fi
    generate_index "$obj"
	

echo "Building gmi"
# TODO
echo "Copying static-gemini"
cp -r static-gemini/ gemini/
echo "Copying res to gemini"
cp -r res/ gemini/media/
echo "Building gemini atom"
# TODO