Bug fixes and code cleanup
Fixed bug in bin/blog.sh with drafts folder being duplicated and included in href with post being added to blog index Fixed inappropriate date conversion with post being added to blog index in bin/blog.sh. Corrected mistake in blog/index.html. Cleaned up local variable declarations and configurations in bin/blog.sh
This commit is contained in:
67
bin/blog.sh
67
bin/blog.sh
@@ -11,12 +11,14 @@ DRAFTS_DIR="drafts"
|
|||||||
CONTENT_DIR="content"
|
CONTENT_DIR="content"
|
||||||
BLOG_DIR="$CONTENT_DIR/blog"
|
BLOG_DIR="$CONTENT_DIR/blog"
|
||||||
OUTPUT_DIR="_output"
|
OUTPUT_DIR="_output"
|
||||||
mkdir -p "$OUTPUT_DIR"
|
|
||||||
|
command -v "rsync" >/dev/null 2>&1 || { echo >&2 "!!! rsync it's not installed. Aborting."; exit 1; }
|
||||||
|
|
||||||
build_pages() {
|
build_pages() {
|
||||||
|
local OUTPUT_FILE
|
||||||
rsync -r --exclude="*.html" "$CONTENT_DIR/" "$OUTPUT_DIR/"
|
rsync -r --exclude="*.html" "$CONTENT_DIR/" "$OUTPUT_DIR/"
|
||||||
find "$CONTENT_DIR" -type f -name "*.html" | while read -r FILE; do
|
find "$CONTENT_DIR" -type f -name "*.html" | while read -r FILE; do
|
||||||
local OUTPUT_FILE="$(printf "$FILE" | sed "s/$CONTENT_DIR/$OUTPUT_DIR/")"
|
OUTPUT_FILE="$(printf %s "$FILE" | sed "s/$CONTENT_DIR/$OUTPUT_DIR/")"
|
||||||
if [ ! -f "$OUTPUT_FILE" ]; then
|
if [ ! -f "$OUTPUT_FILE" ]; then
|
||||||
printf '<!DOCTYPE html>\n' > "$OUTPUT_FILE"
|
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"
|
printf '<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">\n' >> "$OUTPUT_FILE"
|
||||||
@@ -29,8 +31,10 @@ build_pages() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
build_feed() {
|
build_feed() {
|
||||||
local BLOG_INDEX="$BLOG_DIR/index.html"
|
local BLOG_INDEX
|
||||||
local OUTPUT_FILE="$OUTPUT_DIR/index.xml"
|
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 version="1.0" encoding="UTF-8" standalone="yes"?>\n' > "$OUTPUT_FILE"
|
||||||
printf '<?xml-stylesheet href="style.xsl" type="text/xsl"?>\n' >> "$OUTPUT_FILE"
|
printf '<?xml-stylesheet href="style.xsl" type="text/xsl"?>\n' >> "$OUTPUT_FILE"
|
||||||
printf '<rss version="2.0">\n' >> "$OUTPUT_FILE"
|
printf '<rss version="2.0">\n' >> "$OUTPUT_FILE"
|
||||||
@@ -39,11 +43,15 @@ build_feed() {
|
|||||||
printf '<link>%s</link>\n' "$DOMAIN" >> "$OUTPUT_FILE"
|
printf '<link>%s</link>\n' "$DOMAIN" >> "$OUTPUT_FILE"
|
||||||
printf '<description>%s</description>\n' "$DESCRIPTION" >> "$OUTPUT_FILE"
|
printf '<description>%s</description>\n' "$DESCRIPTION" >> "$OUTPUT_FILE"
|
||||||
printf '<copyright>%s</copyright>\n' "$COPYRIGHT" >> "$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
|
grep -Eo "/blog/[a-zA-Z0-9./?=_%:-]*.html" "$BLOG_INDEX" | while read -r POST_URL; do
|
||||||
local POST_FILE="$CONTENT_DIR$POST_URL"
|
local POST_FILE
|
||||||
local POST_TITLE="$(grep "class=\"title\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
|
POST_FILE="$CONTENT_DIR$POST_URL"
|
||||||
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_TITLE
|
||||||
local POST_CONTENT="$(tail -n +6 "$POST_FILE" | head -n -1 | sed 's/^\t//g')"
|
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 '<item><author>%s</author>' "$AUTHOR" >> "$OUTPUT_FILE"
|
||||||
printf '<pubDate>%s</pubDate>' "$POST_DATE" >> "$OUTPUT_FILE"
|
printf '<pubDate>%s</pubDate>' "$POST_DATE" >> "$OUTPUT_FILE"
|
||||||
printf '<title>%s</title>' "$POST_TITLE" >> "$OUTPUT_FILE"
|
printf '<title>%s</title>' "$POST_TITLE" >> "$OUTPUT_FILE"
|
||||||
@@ -58,30 +66,38 @@ done
|
|||||||
printf '</channel></rss>\n' >> "$OUTPUT_FILE"
|
printf '</channel></rss>\n' >> "$OUTPUT_FILE"
|
||||||
}
|
}
|
||||||
draft_post() {
|
draft_post() {
|
||||||
local DRAFT_FILE="$DRAFTS_DIR/$1"
|
|
||||||
mkdir -p "$DRAFTS_DIR"
|
mkdir -p "$DRAFTS_DIR"
|
||||||
cat "$TEMPLATES_DIR/_blog_post.html" > "$DRAFT_FILE"
|
local DRAFT_FILE
|
||||||
nvim "$DRAFT_FILE"
|
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
|
||||||
read -p 'Post this draft? (Y/n) ' CHOICE
|
cp "$TEMPLATES_DIR/_blog_post.html" "$DRAFT_FILE"
|
||||||
|
"$EDITOR" "$DRAFT_FILE"
|
||||||
|
read -r -p 'Post this draft? (Y/n) ' CHOICE
|
||||||
case "$CHOICE" in
|
case "$CHOICE" in
|
||||||
[nN]*) exit 0 ;;
|
[nN]*) exit 0 ;;
|
||||||
[yY]*|*) post_draft "$DRAFT_FILE" ;;
|
[yY]*|*) post_draft "$DRAFT_FILE" ;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
edit_draft() {
|
edit_draft() {
|
||||||
local DRAFT_FILE="$DRAFTS_DIR/$1"
|
local DRAFT_FILE
|
||||||
|
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
|
||||||
"$EDITOR" "$DRAFT_FILE"
|
"$EDITOR" "$DRAFT_FILE"
|
||||||
read -p 'Post this draft? (Y/n) ' CHOICE
|
read -r -p 'Post this draft? (Y/n) ' CHOICE
|
||||||
case "$CHOICE" in
|
case "$CHOICE" in
|
||||||
[nN]*) exit 0 ;;
|
[nN]*) exit 0 ;;
|
||||||
[yY]*|*) post_draft "$(basename "${DRAFT_FILE%.*}.html")" ;;
|
[yY]*|*) post_draft "$DRAFT_FILE" ;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
post_draft() {
|
post_draft() {
|
||||||
local DRAFT_FILE="$DRAFTS_DIR/$1"
|
local DRAFT_FILE
|
||||||
local POST_TITLE="$(grep "class=\"title\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
|
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
|
||||||
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 POST_URL
|
||||||
local INDEX_LINE="<p><i class\"date\">$POST_DATE</i><br class=\"ssbr\"> <a href=\"/$(basename "$BLOG_DIR")/$DRAFT_FILE\">$POST_TITLE</a></p>"
|
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"
|
sed -i "s#<!--BREAK-->#&\n$INDEX_LINE#" "$BLOG_DIR/index.html"
|
||||||
mv "$DRAFT_FILE" "$BLOG_DIR"
|
mv "$DRAFT_FILE" "$BLOG_DIR"
|
||||||
}
|
}
|
||||||
@@ -94,7 +110,7 @@ drafts_selection() {
|
|||||||
PS3="Select a draft to post: "
|
PS3="Select a draft to post: "
|
||||||
select FILE in "${FILENAMES[@]}"; do
|
select FILE in "${FILENAMES[@]}"; do
|
||||||
if [[ -n "$FILE" ]]; then
|
if [[ -n "$FILE" ]]; then
|
||||||
printf '%s' "$(basename "${FILE%.*}.html")"
|
printf '%s' "$(basename "$FILE")"
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
printf 'Invalid selection.\n' >&2
|
printf 'Invalid selection.\n' >&2
|
||||||
@@ -106,7 +122,7 @@ drafts_dir_check() {
|
|||||||
printf 'Error: No drafts directory found\n' >&2
|
printf 'Error: No drafts directory found\n' >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
if ! find "$DRAFTS_DIR" -type f | read; then
|
if ! find "$DRAFTS_DIR" -type f | read -r; then
|
||||||
printf 'Error: No files found in %s, removing empty directory.\n' "$DRAFTS_DIR" >&2
|
printf 'Error: No files found in %s, removing empty directory.\n' "$DRAFTS_DIR" >&2
|
||||||
rmdir "$DRAFTS_DIR"
|
rmdir "$DRAFTS_DIR"
|
||||||
exit 1
|
exit 1
|
||||||
@@ -115,15 +131,16 @@ drafts_dir_check() {
|
|||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
build)
|
build)
|
||||||
|
mkdir -p "$OUTPUT_DIR"
|
||||||
build_pages
|
build_pages
|
||||||
build_feed
|
build_feed
|
||||||
;;
|
;;
|
||||||
draft)
|
draft)
|
||||||
read -p "Enter blog post file name: " INPUT
|
read -r -p "Enter blog post file name: " INPUT
|
||||||
DRAFT_FILE="${INPUT%.*}.html"
|
DRAFT_FILE="${INPUT%.*}.html"
|
||||||
if find "$DRAFTS_DIR" -name "$DRAFT_FILE" | read; then
|
if find "$DRAFTS_DIR" -name "$DRAFT_FILE" | read -r; then
|
||||||
printf 'Error: Draft already exists with that filename.\n'
|
printf 'Error: Draft already exists with that filename.\n'
|
||||||
read -p 'Edit the file? (Y/n) ' CHOICE
|
read -r -p 'Edit the file? (Y/n) ' CHOICE
|
||||||
case "$CHOICE" in
|
case "$CHOICE" in
|
||||||
[nN]*) exit 0 ;;
|
[nN]*) exit 0 ;;
|
||||||
[yY]*|*) edit_draft "$DRAFT_FILE" ;;
|
[yY]*|*) edit_draft "$DRAFT_FILE" ;;
|
||||||
|
|||||||
Reference in New Issue
Block a user