diff options
-rw-r--r-- | webgen.sh | 61 |
1 files changed, 56 insertions, 5 deletions
@@ -1,24 +1,28 @@ #!/bin/sh + TEMPLATEPAGE="./templates/page.html" TEMPLATEBLOG="./templates/blog.html" +TEMPLATEATOM="./templates/atom.xml" CONTENTPAGE="./content/page" CONTENTBLOG="./content/blog" OUTPUTDIR="./output" STATIC="./static" -mkdir -p templates -mkdir -p content/page -mkdir -p content/blog +# Create necessary directories +mkdir -p templates content/page content/blog static rm -rf "$OUTPUTDIR" -mkdir -p output +mkdir -p "$OUTPUTDIR" +# Copy static files cp -r "$STATIC"/* "$OUTPUTDIR" +# Generate the index page sed '/{{.Content}}/ { - r './content/index.html' + r ./content/index.html d }' "$TEMPLATEPAGE" > "$OUTPUTDIR/index.html" +# Generate content pages for file in "$CONTENTPAGE"/*.html; do name=$(basename -s .html "$file") mkdir -p "$OUTPUTDIR/$name/" @@ -28,3 +32,50 @@ for file in "$CONTENTPAGE"/*.html; do }' "$TEMPLATEPAGE" > "$OUTPUTDIR/$name/index.html" done +# Generate blog posts (only if 'post' is passed as argument) +if [ "$1" = "post" ]; then + mkdir -p tmp + for file in "$CONTENTBLOG"/*.html; do + name=$(basename -s .html "$file") + mkdir -p "$OUTPUTDIR/blog/$name/" + sed '/{{.Content}}/ { + r '"$file"' + d + }' "$TEMPLATEBLOG" > "$OUTPUTDIR/blog/$name/index.html" + + # Metadata + post_date=$(sed -n 's/<!-- *date: *\([^ ]*\) *-->/\1/p' "$file") + title=$(sed -n 's/<!-- *title: *\(.*[^ ]\) *-->/\1/p' "$file") + domain=$(sed -n 's/<!-- *domain: *\(.*[^ ]\) *-->/\1/p' "$file") + + # Append to atom entry list + { + echo " <entry>" + echo " <title>$title</title>" + echo " <link href=\"https://$domain/blog/$name\" />" + echo " <id>https://$domain/blog/$name/</id>" + echo " <updated>$post_date</updated>" + echo " <content type=\"xhtml\">" + echo " <div xmlns=\"http://www.w3.org/1999/xhtml\">" + echo " $(cat "$file" | sed '/<!-- *\(date\|title\|domain\):.*-->/d' | sed 's/^/ /')" + echo " </div>" + echo " </content>" + echo " </entry>" + echo "" + } >> tmp/entry.xml + done + + # Generate atom with entries injected + sed '/{{.Content}}/ { + r tmp/entry.xml + d + }' "$TEMPLATEATOM" > "$OUTPUTDIR/atom.xml" + + date=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + sed "s/{{.Date}}/$date/" "$OUTPUTDIR/atom.xml" > "$OUTPUTDIR/atom.xml.tmp" + mv "$OUTPUTDIR/atom.xml.tmp" "$OUTPUTDIR/atom.xml" + + # Remove tmp directory + rm -r tmp +fi + |