20251207172749 push
This commit is contained in:
152
bin/blog.sh
Executable file
152
bin/blog.sh
Executable file
@@ -0,0 +1,152 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Site specific settings
|
||||
DOMAIN="https://iiogama.0x212.com"
|
||||
SITE_TITLE="iiogama@0x212.com"
|
||||
DESCRIPTION="Just some dude"
|
||||
COPYRIGHT="Copyright $(date +%Y), Thai Noodles"
|
||||
AUTHOR="Thai Noodles, (iiogama@0x212.com)"
|
||||
TEMPLATES_DIR="templates"
|
||||
DRAFTS_DIR="drafts"
|
||||
CONTENT_DIR="content"
|
||||
BLOG_DIR="$CONTENT_DIR/blog"
|
||||
OUTPUT_DIR="_output"
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
|
||||
build_pages() {
|
||||
rsync -r --exclude="*.html" "$CONTENT_DIR/" "$OUTPUT_DIR/"
|
||||
find "$CONTENT_DIR" -type f -name "*.html" | while read -r FILE; do
|
||||
local OUTPUT_FILE="$(printf "$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_DIR/index.html"
|
||||
local 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 POST_URL; do
|
||||
local POST_FILE="$CONTENT_DIR$POST_URL"
|
||||
local POST_TITLE="$(grep "class=\"title\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
|
||||
local 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="$(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
|
||||
printf '</channel></rss>\n' >> "$OUTPUT_FILE"
|
||||
}
|
||||
draft_post() {
|
||||
local DRAFT_FILE="$DRAFTS_DIR/$1"
|
||||
mkdir -p "$DRAFTS_DIR"
|
||||
cat "$TEMPLATES_DIR/_blog_post.html" > "$DRAFT_FILE"
|
||||
nvim "$DRAFT_FILE"
|
||||
read -p 'Post this draft? (Y/n) ' CHOICE
|
||||
case "$CHOICE" in
|
||||
[nN]*) exit 0 ;;
|
||||
[yY]*|*) post_draft "$DRAFT_FILE" ;;
|
||||
esac
|
||||
}
|
||||
edit_draft() {
|
||||
local DRAFT_FILE="$DRAFTS_DIR/$1"
|
||||
"$EDITOR" "$DRAFT_FILE"
|
||||
read -p 'Post this draft? (Y/n) ' CHOICE
|
||||
case "$CHOICE" in
|
||||
[nN]*) exit 0 ;;
|
||||
[yY]*|*) post_draft "$(basename "${DRAFT_FILE%.*}.html")" ;;
|
||||
esac
|
||||
}
|
||||
post_draft() {
|
||||
local DRAFT_FILE="$DRAFTS_DIR/$1"
|
||||
local POST_TITLE="$(grep "class=\"title\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
|
||||
local POST_DATE="$(date -d "$(grep "class=\"date\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" '+%a, %d %b %Y %H:%M:%S %Z')"
|
||||
local INDEX_LINE="<p><i class\"date\">$POST_DATE</i><br class=\"ssbr\"> <a href=\"/$(basename "$BLOG_DIR")/$DRAFT_FILE\">$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%.*}.html")"
|
||||
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; 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)
|
||||
build_pages
|
||||
build_feed
|
||||
;;
|
||||
draft)
|
||||
read -p "Enter blog post file name: " INPUT
|
||||
DRAFT_FILE="${INPUT%.*}.html"
|
||||
if find "$DRAFTS_DIR" -name "$DRAFT_FILE" | read; then
|
||||
printf 'Error: Draft already exists with that filename.\n'
|
||||
read -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
|
||||
;;
|
||||
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
|
||||
Reference in New Issue
Block a user