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

BUILD=.build/

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

function mktemp_file() {
    name=$1
    var=$(mktemp -p $BUILD "${name}_XXXXXX")
    echo "${var}"
    return 0
}

function generate_index() {
    base_dir=$1
    echo "Generating index for $base_dir"
    page_file=$(mktemp_file "index_page")    
    content_file=$(mktemp_file "index_content")
    title=$(basename ${base_dir})
    echo "<article>
    	 <h1>${title} index</h1>
    	 <ul>" > $content_file
    find $base_dir -type f -iname "*.html" \
	 -exec echo "<li><a href=\"{}\">{}</a></li>" \; >> $content_file
    echo "</ul>
    	 </article>" >> $content_file
    sed -i "s,${base_dir},.,g" $content_file

    generate_page "index" $content_file $page_file

    # strip out the footer
    sed -i '1,/<\/footer>/{/<footer>/,$d}' $page_file
    echo "Copying $page_file to $base_dir/index.html"
    cp $page_file $base_dir/index.html
    echo "Searching for more directories to index"
    for dir in $base_dir/*; do
	if [ ! -d $dir ]; then
	    continue;
	fi
	generate_index $dir
    done
}
# generates the base page structure
# any "special" functions can go in their various code_paths
function generate_page() {
    type=$1
    content_file=$2
    page_file=$3
    echo "Using $page_file for file"
    cat _templates/page.html > $page_file
    
    echo "Using $content_file for content"
    sed -i "/^--/d" $content_file

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

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

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

    return 0
}

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_file "post")
    content_file=$(mktemp_file "content")
    updated_html_file=$(mktemp_file "updated_html")
    tags_html_file=$(mktemp_file "tags_html")

    echo "Copying $post data into $content_file"
    cp $post $content_file

    generate_page $type $content_file $post_file
    
    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


    echo "Copying temp_file to www/"
    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"
dir="www/blog"
generate_index "$dir"

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