#!/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 "
" > $index_tmp
find -type f -iname "*.html" -exec echo "- {}\
" \; >> $index_tmp
echo "
" >> $index_tmp
page_tmp=$(mktemp -p $BUILD "index_page_XXXXXX.html")
cat _templates/page.html > $page_tmp
sed -i -e "/CONTENT/ {
\
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 ".+
" $content_file | sed -E "s,(.+)
,\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 " Updated at: " \
> 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 " 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