183 lines
6.3 KiB
Bash
Executable File
183 lines
6.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
command -v "just" >/dev/null 2>&1 || { echo >&2 "!!! just is not installed, do not run this without just. Aborting."; exit 1; }
|
|
command -v "rsync" >/dev/null 2>&1 || { echo >&2 "!!! rsync is not installed. Aborting."; exit 1; }
|
|
|
|
source "config.env"
|
|
|
|
build_pages() {
|
|
local OUTPUT_FILE
|
|
rsync -r --exclude="*.html" "$CONTENT_DIR/" "$OUTPUT_DIR/"
|
|
find "$CONTENT_DIR" -type f -name "*.html" | while read -r FILE; do
|
|
OUTPUT_FILE="$(printf %s "$FILE" | sed "s/$CONTENT_DIR/$OUTPUT_DIR/")"
|
|
if [ ! -f "$OUTPUT_FILE" ]; then
|
|
printf '<!DOCTYPE html>\n' > "$OUTPUT_FILE"
|
|
printf '<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">\n' >> "$OUTPUT_FILE"
|
|
sed "s!<title>!&$SITE_TITLE!" "templates/_head.html" >> "$OUTPUT_FILE"
|
|
printf '<body>\n' >> "$OUTPUT_FILE"
|
|
cat "templates/_nav.html" "$FILE" "templates/_footer.html" >> "$OUTPUT_FILE"
|
|
printf '</body>\n' >> "$OUTPUT_FILE"
|
|
printf '</html>\n' >> "$OUTPUT_FILE"
|
|
fi
|
|
done
|
|
}
|
|
build_feed() {
|
|
local BLOG_INDEX
|
|
BLOG_INDEX="$BLOG_DIR/index.html"
|
|
local OUTPUT_FILE
|
|
OUTPUT_FILE="$OUTPUT_DIR/index.xml"
|
|
printf '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' > "$OUTPUT_FILE"
|
|
printf '<?xml-stylesheet href="style.xsl" type="text/xsl"?>\n' >> "$OUTPUT_FILE"
|
|
printf '<rss version="2.0">\n' >> "$OUTPUT_FILE"
|
|
printf '<channel>' >> "$OUTPUT_FILE"
|
|
printf '<title>%s</title>\n' "$SITE_TITLE" >> "$OUTPUT_FILE"
|
|
printf '<link>%s</link>\n' "$DOMAIN" >> "$OUTPUT_FILE"
|
|
printf '<description>%s</description>\n' "$DESCRIPTION" >> "$OUTPUT_FILE"
|
|
printf '<copyright>%s</copyright>\n' "$COPYRIGHT" >> "$OUTPUT_FILE"
|
|
grep -Eo "/blog/[a-zA-Z0-9./?=_%:-]*.html" "$BLOG_INDEX" | while read -r POST_URL; do
|
|
local POST_FILE
|
|
POST_FILE="$CONTENT_DIR$POST_URL"
|
|
local POST_TITLE
|
|
POST_TITLE="$(grep "class=\"title\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
|
|
local POST_DATE
|
|
POST_DATE="$(date -d "$(grep "class=\"date\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" '+%a, %d %b %Y %H:%M:%S %Z')"
|
|
local POST_CONTENT
|
|
POST_CONTENT="$(tail -n +6 "$POST_FILE" | head -n -1 | sed 's/^\t//g')"
|
|
printf '<item><author>%s</author>' "$AUTHOR" >> "$OUTPUT_FILE"
|
|
printf '<pubDate>%s</pubDate>' "$POST_DATE" >> "$OUTPUT_FILE"
|
|
printf '<title>%s</title>' "$POST_TITLE" >> "$OUTPUT_FILE"
|
|
printf '<guid>%s' "$DOMAIN" >> "$OUTPUT_FILE"
|
|
printf '%s</guid>' "$POST_URL" >> "$OUTPUT_FILE"
|
|
printf '<link>%s' "$DOMAIN" >> "$OUTPUT_FILE"
|
|
printf '%s</link>' "$POST_URL" >> "$OUTPUT_FILE"
|
|
printf '<description><![CDATA[\n' >> "$OUTPUT_FILE"
|
|
printf '%s' "$POST_CONTENT" >> "$OUTPUT_FILE"
|
|
printf ']]></description></item>\n' >> "$OUTPUT_FILE"
|
|
done
|
|
if [[ -d "$NOW_DIR" ]]; then
|
|
local POST_FILE
|
|
POST_FILE="$NOW_DIR/index.html"
|
|
local POST_LINK
|
|
POST_LINK="$(basename "$NOW_DIR")"
|
|
local POST_GUID
|
|
POST_GUID="$POST_LINK/#$(grep "<p id=" $NOW_DIR/index.html | sed 's!^.*<p id="!!' | sed 's!"><i class.*$!!')"
|
|
local POST_DATE
|
|
POST_DATE="$(date -d "$(grep "class=\"date\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" '+%a, %d %b %Y %H:%M:%S %Z')"
|
|
local POST_CONTENT
|
|
POST_CONTENT="$(tail -n +3 "$POST_FILE" | head -n -1 | sed 's/^\t//g')"
|
|
printf '<item><author>%s</author>' "$AUTHOR" >> "$OUTPUT_FILE"
|
|
printf '<pubDate>%s</pubDate>' "$POST_DATE" >> "$OUTPUT_FILE"
|
|
printf '<title>%s has updated Now</title>' "$AUTHOR" >> "$OUTPUT_FILE"
|
|
printf '<guid>%s' "$DOMAIN" >> "$OUTPUT_FILE"
|
|
printf '/%s</guid>' "$POST_GUID" >> "$OUTPUT_FILE"
|
|
printf '<link>%s' "$DOMAIN" >> "$OUTPUT_FILE"
|
|
printf '/%s/</link>' "$POST_LINK" >> "$OUTPUT_FILE"
|
|
printf '<description><![CDATA[\n' >> "$OUTPUT_FILE"
|
|
printf '%s\n' "$POST_CONTENT" >> "$OUTPUT_FILE"
|
|
printf ']]></description></item>\n' >> "$OUTPUT_FILE"
|
|
fi
|
|
printf '</channel></rss>\n' >> "$OUTPUT_FILE"
|
|
}
|
|
draft_post() {
|
|
mkdir -p "$DRAFTS_DIR"
|
|
local DRAFT_FILE
|
|
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
|
|
cp "$TEMPLATES_DIR/_blog_post.html" "$DRAFT_FILE"
|
|
"$EDITOR" "$DRAFT_FILE"
|
|
}
|
|
edit_draft() {
|
|
local DRAFT_FILE
|
|
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
|
|
"$EDITOR" "$DRAFT_FILE"
|
|
read -r -p 'Post this draft? (Y/n) ' CHOICE
|
|
case "$CHOICE" in
|
|
[nN]*) exit 0 ;;
|
|
[yY]*|*) post_draft "$DRAFT_FILE" ;;
|
|
esac
|
|
}
|
|
post_draft() {
|
|
local DRAFT_FILE
|
|
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
|
|
local POST_URL
|
|
POST_URL="/$(basename "$BLOG_DIR")/$(basename "$DRAFT_FILE")"
|
|
local POST_TITLE
|
|
POST_TITLE="$(grep "class=\"title\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
|
|
local POST_DATE
|
|
POST_DATE="$(grep "class=\"date\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
|
|
local INDEX_LINE
|
|
INDEX_LINE="<p><i class=\"date\">$POST_DATE</i><br class=\"ssbr\"> <a href=\""$POST_URL"\">$POST_TITLE</a></p>"
|
|
sed -i "s#<!--BREAK-->#&\n$INDEX_LINE#" "$BLOG_DIR/index.html"
|
|
mv "$DRAFT_FILE" "$BLOG_DIR"
|
|
}
|
|
drafts_selection() {
|
|
FILEPATHS=("$DRAFTS_DIR"/*)
|
|
FILENAMES=()
|
|
for FILE in "${FILEPATHS[@]}"; do
|
|
FILENAMES+=("$(basename "$FILE")")
|
|
done
|
|
PS3="Select a draft to post: "
|
|
select FILE in "${FILENAMES[@]}"; do
|
|
if [[ -n "$FILE" ]]; then
|
|
printf '%s' "$(basename "$FILE")"
|
|
break
|
|
else
|
|
printf 'Invalid selection.\n' >&2
|
|
fi
|
|
done
|
|
}
|
|
drafts_dir_check() {
|
|
if [ ! -d "$DRAFTS_DIR" ]; then
|
|
printf 'Error: No drafts directory found\n' >&2
|
|
exit 1
|
|
fi
|
|
if ! find "$DRAFTS_DIR" -type f | read -r; then
|
|
printf 'Error: No files found in %s, removing empty directory.\n' "$DRAFTS_DIR" >&2
|
|
rmdir "$DRAFTS_DIR"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
case "$1" in
|
|
build)
|
|
mkdir -p "$OUTPUT_DIR"
|
|
build_pages
|
|
build_feed
|
|
;;
|
|
draft)
|
|
read -r -p "Enter blog post file name: " INPUT
|
|
DRAFT_FILE="${INPUT%.*}.html"
|
|
if find "$DRAFTS_DIR" -name "$DRAFT_FILE" | read -r; then
|
|
printf 'Error: Draft already exists with that filename.\n'
|
|
read -r -p 'Edit the file? (Y/n) ' CHOICE
|
|
case "$CHOICE" in
|
|
[nN]*) exit 0 ;;
|
|
[yY]*|*) edit_draft "$DRAFT_FILE" ;;
|
|
esac
|
|
else
|
|
draft_post "$DRAFT_FILE"
|
|
fi
|
|
read -r -p 'Post this draft? (Y/n) ' CHOICE
|
|
case "$CHOICE" in
|
|
[nN]*) exit 0 ;;
|
|
[yY]*|*) post_draft "$DRAFT_FILE" ;;
|
|
esac
|
|
;;
|
|
edit)
|
|
drafts_dir_check
|
|
edit_draft "$(drafts_selection)"
|
|
;;
|
|
post)
|
|
drafts_dir_check
|
|
post_draft "$(drafts_selection)"
|
|
;;
|
|
help|*)
|
|
printf 'Usage: %s [SUBCOMMANDS]\n\n' "$0"
|
|
printf 'Sub-commands:\n'
|
|
printf '\tdraft\n'
|
|
printf '\tedit\n'
|
|
printf '\tpost\n'
|
|
printf '\tbuild\n'
|
|
printf '\thelp\n'
|
|
;;
|
|
esac
|