summaryrefslogtreecommitdiff
path: root/build.sh
diff options
context:
space:
mode:
authorSteph Enders <steph@senders.io>2024-03-07 15:17:29 -0500
committerSteph Enders <steph@senders.io>2024-03-07 15:17:29 -0500
commit1f689fd039533801842ae241671f2437ddbe0044 (patch)
tree50d3db88f2c7e676d6679696a101e6ae2b25448f /build.sh
parent80f5dacf988b1cddd04eea6c4a6f70b165376764 (diff)
Copy old files and update build.sh to generate it all!
This is a huge messy commit but :) sue me. I'm not at work I can do git badly for once!
Diffstat (limited to 'build.sh')
-rwxr-xr-xbuild.sh247
1 files changed, 192 insertions, 55 deletions
diff --git a/build.sh b/build.sh
index 20d232b..307c377 100755
--- a/build.sh
+++ b/build.sh
@@ -1,11 +1,10 @@
#!/usr/bin/env bash
-
BUILD=.build/
function extract_metadata() {
- name=$1
- file=$2
- val=$(sed -nE "/^--${name}: .+$/s/^--${name}: (.+)$/\1/p" $file)
+ local name=$1
+ local file=$2
+ local val=$(sed -nE "/^--${name}: .+$/s/^--${name}: (.+)$/\1/p" $file)
if [ -z "$val" ]; then
echo ""
return 1
@@ -16,27 +15,42 @@ function extract_metadata() {
}
function mktemp_file() {
- name=$1
- var=$(mktemp -p $BUILD "${name}_XXXXXX")
+ local name=$1
+ local var=$(mktemp -p $BUILD "${name}_XXXXXX")
echo "${var}"
return 0
}
+function extract_title() {
+ sed -n -E "1,/<\/h1>/s/.*<h1>(.+)<\/h1>.*/\1/p1" $1
+}
+
function generate_index() {
- base_dir=$1
+ local 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})
+ local page_file=$(mktemp_file "index_page")
+ local content_file=$(mktemp_file "index_content")
+ local indexed_files=$(mktemp_file "indexed_files")
+ local 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
+ find $base_dir -type f -iname "*.html" | sort -hr > $indexed_files
+ while read post; do
+ post_date=$(sed -n -E "1,/datetime=/s/Posted:.*datetime=.([0-9-]+).*/\1/p1" $post)
+ post_title=$(extract_title $post)
+ if [ -z "${post_date}" ]; then
+ echo "<li><a href=\"${post}\">${post_title}</a></li>" >> $content_file
+ else
+ echo "<li><a href=\"${post}\">${post_date} — ${post_title}</a></li>" >> $content_file
+ fi
+
+
+ done <$indexed_files
+ echo "</ul></article>" >> $content_file
+ sed -i "s,${base_dir},.,g" $content_file
+
generate_page "index" $content_file $page_file
# strip out the footer
@@ -54,11 +68,11 @@ function generate_index() {
# 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
+ local type=$1
+ local content_file=$2
+ local page_file=$3
echo "Using $page_file for file"
- cat _templates/page.html > $page_file
+ cat templates/page.html > $page_file
echo "Using $content_file for content"
sed -i "/^--/d" $content_file
@@ -70,12 +84,12 @@ function generate_page() {
echo "Injecting CSS into $page_file"
sed -i -e "/STYLE/ {
- r _templates/index.css
+ 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
+ sed -i -E "s/[{]PAGE_TITLE[}]/${page_title/\//\\/}/1" $page_file
echo "Setting type"
if [ -z "$type" ]; then
@@ -87,6 +101,89 @@ function generate_page() {
return 0
}
+function set_tags() {
+ local tags=$1
+ local post_file=$2
+ if [ -z "$tags" ]; then
+ sed -i -E "s/[{]TAGS[}]//" $post_file
+ else
+ local tags_html_file=$(mktemp_file "tags_html")
+ echo "Using tags file $tags_html_file"
+ echo "<li>Tags: [ ${tags} ]</li>" > $tags_html_file
+ sed -i -e "/TAGS/ {
+ r $tags_html_file
+ d}" $post_file
+ fi
+
+}
+
+function set_post_date () {
+ local post_date=$1
+ local post_file=$2
+ match=$(echo $post_date | sed -n -E "s/^([0-9-]+)$/\1/p")
+ if [ -z "$match" ]; then
+ sed -i -E "s/[{]POST_DATE[}]//" $post_file
+ else
+ local post_date_html=$(mktemp_file "post_date")
+ display_date=$(date -d "${post_date}" +"%B %d, %Y")
+ echo "<li>
+ Posted: <time datetime=\"${post_date}\">${display_date}</time>
+ </li>" > $post_date_html
+ sed -i -e "/POST_DATE/ {
+ r $post_date_html
+ d}" $post_file
+
+ fi
+}
+
+function set_updated_at() {
+ local updated_at=$1
+ local post_file=$2
+ if [ -z "$updated_at" ]; then
+ sed -i -E "s/[{]UPDATED[}]//" $post_file
+ else
+ updated_html_file=$(mktemp_file "updated_html")
+ updated_disp=$(date -d "${updated_at}" +"%B %d, %Y")
+ echo "<li>
+ Updated on: <time datetime=\"${updated_at}\">${updated_disp}</time>
+ </li>" > $updated_html_file
+ sed -i -e "/UPDATED/ {
+ r ${updated_html_file}
+ d}" $post_file
+ fi
+}
+
+function push_pin() {
+ local post_date=$1
+ local post_url=$2
+ local post_title=$3
+ local pin_file=$4
+ local pin_text="<li><a href=\"${post_url}\">"
+ matched=$(echo "${post_date}" | sed -n -E "s/^([0-9-]+)$/\1/p")
+ if [ ! -z "${matched}" ]; then
+ pin_text="${pin_text}${post_date} — "
+ fi
+ pin_text="${pin_text}${post_title}</a></li>"
+ echo $pin_text >> $pin_file
+}
+
+function add_to_rss() {
+ local post_date=$1
+ local post_url=$2
+ local post_title=$3
+ local content_file=$4
+ local rss_data=$5
+ local pub_date=$(date -d "${post_date}" -R)
+ sed -E -e "{
+ s/[{]TITLE[}]/${post_title}/1;
+ s,[{]LINK[}],${post_url},g;
+ s/[{]PUBDATE[}]/${pub_date}/1;
+ /CONTENT/ {
+ r ${content_file}
+ d}
+ }" templates/rss-item.xml >> ${rss_data}
+}
+
echo "Clearing build-cache"
rm -rf $BUILD
mkdir $BUILD
@@ -101,8 +198,11 @@ rm -rf gemini
mkdir gemini
echo "Building html"
-# TODO
-for post in _posts/*.html; do
+pinned_html=$(mktemp_file "pinned")
+rss_data=$(mktemp_file "rss_data")
+echo "Using pinned file: $pinned_html"
+for post in posts/*.html; do
+ echo "Processing $post"
post_date=$(extract_metadata "post-date" $post)
if [ -z "$post_date" ]; then
# post is not ready to be posted
@@ -110,14 +210,15 @@ for post in _posts/*.html; do
echo "No data"
continue;
fi
+
+ echo "Extracting metadata"
updated_at=$(extract_metadata "updated-at" $post)
tags=$(extract_metadata "tags" $post)
type=$(extract_metadata "type" $post)
+ pinned=$(extract_metadata "pinned" $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
@@ -125,55 +226,94 @@ for post in _posts/*.html; do
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
-
+ set_post_date "$post_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
+ set_updated_at "$updated_at" $post_file
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
-
+ set_tags "$tags" $post_file
echo "Copying temp_file to www/"
filename=$(basename $post)
+ url=""
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
+ title="$(extract_title $post_file)"
+ if [ ! -z "$pinned" ]; then
+ push_pin "$post_date" \
+ "/blog/$year/$month/$day/$filename" \
+ "${title}" \
+ $pinned_html
+ fi
+ add_to_rss "$post_date" \
+ "/blog/$year/$month/$day/$filename" \
+ "${title}" \
+ ${content_file} \
+ ${rss_data}
else
- cp $post_file www/$filename
+ path=$(extract_metadata "path" $post)
+ mkdir -p www/$path
+ cp $post_file www/$path/$filename
+ dest=$(echo "/$path/$filename" | sed -E "s,/+,/,g")
+ if [ ! -z "$pinned" ]; then
+ push_pin "$post_date" \
+ "$dest" \
+ "$(extract_title $post_file)" \
+ $pinned_html
+ fi
fi
done
+function set_pinned() {
+ local pinned_html=$1
+ local file=$2
+ if [ -z "$pinned_html" ]; then
+ sed -i -E "s/[{]PINNED[}]//" $file
+ else
+ sort -r -o $pinned_html $pinned_html
+ sed -i -e "/PINNED/ {
+ r $pinned_html
+ d}" $file
+ fi
+}
+
+echo "Generate homepage"
+homepage=$(mktemp_file "homepage")
+generate_page "home" posts/index.html $homepage
+# clear extra tag content
+set_post_date "" $homepage
+set_updated_at "$(date --iso)" $homepage
+set_tags "" $homepage
+set_pinned "${pinned_html}" $homepage
+cp $homepage www/index.html
+
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
+feed=$(mktemp_file "rss_feed")
+year="$(date +%Y)"
+build_date="$(date -R)"
+sed -E -e "{
+ s/[{]YEAR[}]/$year/1;
+ s/[{]BUILDDATE[}]/$build_date/g;
+ /ITEMS/ {
+ r ${rss_data}
+ d}
+}" templates/feed.rss > $feed
+
+cp $feed www/blog/feed.rss
echo "Generating index files"
-dir="www/blog"
-generate_index "$dir"
+generate_index "www/blog"
+generate_index "www/music"
+
+echo "Tidying HTML files"
+./tidy.sh
echo "Building gmi"
# TODO
@@ -183,6 +323,3 @@ echo "Copying res to gemini"
cp -r res/ gemini/media/
echo "Building gemini atom"
# TODO
-
-
-