From fb4f206e21cc2853e904e8e548b3d4450b208c8d Mon Sep 17 00:00:00 2001 From: ben Date: Fri, 10 Jan 2025 11:14:29 -0800 Subject: added scripts --- scripts/archive/blog/ab | 442 +++++++++++++++++++++ scripts/archive/blog/doc/data.txt | 5 + .../blog/doc/templates/index-tag-template.html | 8 + .../archive/blog/doc/templates/index-template.html | 8 + .../archive/blog/doc/templates/rss-template.xml | 16 + scripts/archive/blog/doc/templates/template.html | 59 +++ scripts/archive/blog/rss-parse.sh | 46 +++ 7 files changed, 584 insertions(+) create mode 100755 scripts/archive/blog/ab create mode 100644 scripts/archive/blog/doc/data.txt create mode 100644 scripts/archive/blog/doc/templates/index-tag-template.html create mode 100644 scripts/archive/blog/doc/templates/index-template.html create mode 100644 scripts/archive/blog/doc/templates/rss-template.xml create mode 100644 scripts/archive/blog/doc/templates/template.html create mode 100755 scripts/archive/blog/rss-parse.sh (limited to 'scripts/archive/blog') diff --git a/scripts/archive/blog/ab b/scripts/archive/blog/ab new file mode 100755 index 0000000..b16c933 --- /dev/null +++ b/scripts/archive/blog/ab @@ -0,0 +1,442 @@ +#!/bin/bash + +blogdir="arachnida_blog" +imgdir="assets/img" +datafile="../doc/data.txt" +drafts="drafts" +template="../doc/templates/template.html" +index_template="../doc/templates/index-template.html" +index_tag_template="../doc/templates/index-tag-template.html" +rss_template="../doc/templates/rss-template.xml" +rss_file="~/Doc/programming/website/arachnida_blog/rss/rss.xml" + +# Colored Output +RED='\033[0;31m' +GREEN='\033[0;32m' +RESET='\033[0m' + +# Update +function update () { + #printf 'Updating: arachnida.blog\n' | dzen2 -bg white -fg black -p 2 -x -375 -y 50 -w 275 -h 50 + rsync -uvrp --delete-after "$blogdir" root@"$DOMAIN":"$REMOTE_LOCATION" + printf "\n\n${GREEN}Website has been updated.${RESET}\n\n" +} + +# Pick an entry +function listitem () { + printf "\nContents of %s:\n" "$1" + case "$(ls --hide "img" "$1" | wc -l)" in + 0) printf "${RED}No drafts made yet.\n\n${GREEN}Redirecting...${RESET}\n\n" && blog ;; + 1) number=1 && printf "Only one draft available.\n" ;; + *) ls -rc --hide "img" "$1" | awk -F '/' '{print $NF}' | sed 's/\.html$//' | nl # edited + printf "\nPick and entry by number to %s" "$2: " | sed 's/blog_//g' && read number + esac + entry="$(ls --hide "img" -rc "$1" | nl | grep -w " $number" | awk '{print $2}')" + basefile="$(basename "$entry")" && base="${basefile%.*}" +} + +# Confirm a choice +function confirm () { read -p "Really $1 \"$base\"? (y/N) " choice && echo "$choice" | grep -qi "^y$" || exit 1; } + +# Create a new blog draft +function blog_draft () { + read -e -p "Give your post a title: " title + + max=0 + while [ $max == 0 ]; do + read -e -p "Enter a description (50 words limit): " description + [ $(echo "$description" | wc -w) -ge "50" ] && echo "Description must be less than 50 words." || max=1 + done + + read -e -r -p "Input an image to reflect your post: " img + while [ ! -e $img ]; do + read -e -r -p "$img does not exist. Please try another path: " img + done + + url="$(echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")" + ( [ -f "$drafts"/$url.html ] || [ -f "$blogdir/blog/$url.html" ] ) && + printf "\nThere is already an existing draft or post with the same title.\n\n Redirecting...\n\n" && blog + $EDITOR "$drafts"/$url.html + # Select tags + tags=("informative" "care" "rant" "breeding-report" "off-topic" "seller-review") + tagged=() + menu() { + clear + echo "Choose a tag(s):" + for i in ${!tags[@]}; do + printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${tags[i]}" + done + if [[ "$msg" ]]; then echo "$msg"; fi + } + + prompt="Check an option (again to uncheck, ENTER when done): " + while menu && read -rp "$prompt" num && [[ "$num" ]]; do + [[ "$num" != *[![:digit:]]* ]] && + (( num > 0 && num <= ${#tags[@]} )) || + { msg="Invalid option: $num"; continue; } + ((num--))#; msg="${tags[num]} was ${choices[num]:+un}checked" + [[ "${choices[num]}" ]] && choices[num]="" || choices[num]="+" + done + for i in ${!tags[@]}; do + [[ "${choices[i]}" ]] && { tagged+=(${tags[i]^}); } + done + TAGS=$(printf "%s, " "${tagged[@]}") + + # image handling + img=$(echo "$img" | cut -f 1 -d '.' >&1) + + if [[ -e "$drafts"/img/$img.jpg || -e "$blogdir"/assets/img/blog/$img.jpg ]] ; then + i="2" + while [[ -e "$drafts"/img/$img-$i.jpg || -e "$blogdir"/assets/img/blog/$img-$i.jpg ]]; do ((i++)); done + mv "$img.jpg" "$img-$i.jpg" && cp "$img-$i.jpg" "$drafts"/img/ && mv "$img-$i.jpg" "$img.jpg" + img="$img-$i.jpg" + else + cp "$img.jpg" "$drafts"/img/ + img="$img.jpg" + fi + + printf "Post: %s\nTitle: %s\nDescription: %s\nImage: %s\nTags:\n" "$url" "$title" "$description" "$img" | cat - "$datafile" | sponge "$datafile" + sed -i "5s/$/ ${TAGS}\n/" "$datafile" + printf "\n${GREEN}Post was saved as a draft: Publish when you are ready to do so.${RESET}\n\n" +} + +# Edit a blog draft. Either title, description, content, or index image +function edit_draft () { + title() { + read -e -r -p "Enter a new title: " title + url="$(echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")" + old_url="$(echo $(basename "$basefile" ".html" | sed 's/ /-/g'))" + old_title="$(awk "/Post: $old_url/{getline; print}" "$datafile" | sed 's/^[^:]*: //')" + ( [ -f "$drafts"/$url.html ] || [ -f "$blogdir/blog/$url.html" ] ) && + printf "\nThere is already an existing draft or post with the same title.\n\n Redirecting...\n\n" && title + mv $drafts/$entry $drafts/$url.html + sed -i "s/Post: $old_url/Post: $url/g" "$datafile" + sed -i "s/Title: $old_title/Title: $title/g" "$datafile" + printf "\n${GREEN}Title has been updated.${RESET}\n\n" + } + + descr() { + max=0 + while [ $max == 0 ]; do + read -e -p "Enter a new description (50 words limit): " descript + [ $(echo "$descript" | wc -w) -ge "50" ] && echo "Description must be less than 50 words." || max=1 + done + sed -i "/Post: $(basename "$basefile" ".html")/{n;s/.*/Description: $descript/}" "$datafile" + sed -i "/Description: $descript/{n;d;}" "$datafile" + printf "\n${GREEN}Description has been updated.${RESET}\n\n" + } + + con() { listitem "$drafts" edit && "$EDITOR" "$drafts"/$entry && printf "\nPost's content has been updated.\n\n"; } + + image() { + entry="$(basename "$entry" ".html")" + read -e -r -p "Enter the path to a new image: " img + while [ ! -e "$img" ]; do + read -e -r -p "$img does not exist. Please try another path: " img + done + + img=$(echo "$img" | cut -f 1 -d '.' >&1) + old_img="$(cat "$datafile" | sed -n "/Post: "$entry"/{n;n;n; p}" | sed 's/^[^:]*: //')" + if [[ -e "$drafts"/img/$img.jpg || -e "$blogdir"/assets/img/blog/$img.jpg ]] ; then + i="2" + while [[ -e "$drafts"/img/$img-$i.jpg || -e "$blogdir"/assets/img/blog/$img-$i.jpg ]]; do ((i++)); done + mv "$img.jpg" "$img-$i.jpg" && cp "$img-$i.jpg" "$drafts"/img/ + else cp "$img.jpg" "$imgdir"/blog/ + fi + rm "$drafts"/img/$old_img + sed -i "s/Image: $old_img/Image: $img.jpg/g" "$datafile" + printf "\n${GREEN}Index image has been updated.${RESET}\n\n" + } + + printf "\nEditing %s:\nUsage:\n\tt = Title\n\td = Description\n\tc = Content\n\ti = Image\n\n" "$entry" + read -p "How would you like proceed? " cmd + case "$cmd" in + t) title ;; + d) descr ;; + c) con ;; + i) image ;; + *) printf "Exiting..\n\n" && exit 1 ;; + esac +} + +# Delete a draft +function del_draft () { + printf "${RED}Deleting $entry.${RESET}\n" && rm -f "$drafts"/$entry + sed -i "/Post: $(basename "$entry" ".html")/,+5d" "$datafile" +} + +# Delete a public post +function blog_pub_del () { + title="$(basename $basefile .html | sed 's/-/ /g')" + post='\' + blog_post='\' + + sed -i -n '/'"$blog_post"'/{x;d;};1h;1!{x;p;};${x;p;}' "$blogdir"/blog.html + sed -i -e '/'"$blog_post"'/,+4d' "$blogdir"/blog.html + sed -i "/Post: $(basename "$entry" ".html")/,+5d" "$datafile" + sed -i '//,//p' "$blogdir"/index.html | wc -l)" + + printf "$(cat "$index_tag_template")\n" "$basefile" "$img" "$img" "$basefile" "$title" "$webdate" "$description" "$basefile" > "$tmpdir/tagindex" + for i in "${tagsArr[@]}"; do + case ${i} in + "Informative") + printf "$i\n" | sed "s/^.*$/

&<\/a><\/h3>/g" >> "$tmpdir/tag" + sed -i "//r $tmpdir/tagindex" "$blogdir"/blog/tags/informative.html + ;; + "Care") + printf "$i\n" | sed "s/^.*$/

&<\/a><\/h3>/g" >> "$tmpdir/tag" + sed -i "//r $tmpdir/tagindex" "$blogdir"/blog/tags/care.html + ;; + "Rant") + printf "$i\n" | sed "s/^.*$/

&<\/a><\/h3>/g" >> "$tmpdir/tag" + sed -i "//r $tmpdir/tagindex" "$blogdir"/blog/tags/rant.html + ;; + "Breeding-report") + printf "$i\n" | sed "s/^.*$/

&<\/a><\/h3>/g" >> "$tmpdir/tag" + sed -i "//r $tmpdir/tagindex" "$blogdir"/blog/tags/breeding-reports.html + ;; + "Off-topic") + printf "$i\n" | sed "s/^.*$/

&<\/a><\/h3>/g" >> "$tmpdir/tag" + sed -i "//r $tmpdir/tagindex" "$blogdir"/blog/tags/off-topic.html + ;; + "Seller-review") + printf "$i\n" | sed "s/^.*$/

&<\/a><\/h3>/g" >> "$tmpdir/tag" + sed -i "//r $tmpdir/tagindex" "$blogdir"/blog/tags/seller-reviews.html + ;; + *) ;; + esac + done + + printf "$(cat "$template")" "$title" "$description" "$title" "$webdate" > "$blogdir"/blog/"$basefile" # Append draft file to blog directory + sed -i "//r $drafts/$basefile" "$blogdir"/blog/"$basefile" # Append post in the draft + sed -i "//r $tmpdir/tag" "$blogdir"/blog/"$basefile" # Append respective tags in draft + printf "$(cat "$index_template")\n" "$basefile" "$img" "$img" "$basefile" "$title" "$webdate" "$description" "$basefile" > "$tmpdir/blogindex" + sed -i "//r $tmpdir/blogindex" "$blogdir"/blog.html + printf "$(cat "$rss_template")\n" "$title" "$title" "$(basename "$basefile" ".html")" "$(basename "$basefile" ".html")" "$rssdate" "$(echo "$post" | recode "..html")" "$title" > "$tmpdir/rss" # RSS + sed -i "//r $tmpdir/rss" "$blogdir/rss/rss.xml" # ↓ + + sed -i -e '//a '"$recent"'' "$blogdir"/index.html + [ "$numofrecent" -ge 7 ] && sed -i -n '//{x;d;};1h;1!{x;p;};${x;p;}' "$blogdir"/index.html + + cp $drafts/img/$img "$imgdir"/blog && rm $drafts/img/$img && rm "$drafts"/$basefile + printf "\n${GREEN}Draft has been published.${RESET}\n\n" && update +} + +# Edit or Revise a published blog post +function blog_edit_pub () { + title() { + read -e -r -p "Enter a new title: " title + + url="$(echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")" + old_url="$(echo $(basename "$basefile" ".html" | sed 's/ /-/g'))" + old_title="$(awk "/Post: $old_url/{getline; print}" "$datafile" | sed 's/^[^:]*: //')" + tags="$(sed -n "/$old_url/{n;n;n;n; p}" "$datafile" | sed -e 's/^[^:]*: //' -e 's/\,//g')" + tagsArr=(${tags}) + for i in "${tagsArr[@]}"; do + case ${i} in + "Informative") + sed -i -e "s/$old_title/$title/g" -e "s/$old_url/$url/g" "$blogdir"/blog/tags/informative.html + ;; + "Care") + sed -i -e "s/$old_title/$title/g" -e "s/$old_url/$url/g" "$blogdir"/blog/tags/care.html + ;; + "Rant") + sed -i -e "s/$old_title/$title/g" -e "s/$old_url/$url/g" "$blogdir"/blog/tags/rant.html + ;; + "Breeding-report") + sed -i -e "s/$old_title/$title/g" -e "s/$old_url/$url/g" "$blogdir"/blog/tags/breeding-reports.html + ;; + "Off-topic") + sed -i -e "s/$old_title/$title/g" -e "s/$old_url/$url/g" "$blogdir"/blog/tags/off-topic.html + ;; + "Seller-review") + sed -i -e "s/$old_title/$title/g" -e "s/$old_url/$url/g" "$blogdir"/blog/tags/seller-reivews.html + ;; + *) ;; + esac + done + sed -i -e "s/Post: $old_url/Post: $url/g" -e "s/Title: $old_title/Title: $title/g" "$datafile" # Update URL & Title in data file + sed -i -e "s/$old_title/$title/g" "$blogdir"/blog/$entry # Rename title & headings in $entry + sed -i -e "s/$old_title/$title/g" -e "s/$basename/$url/g" "$rss_file" # Update RSS title and URL + sed -i -e "s/"$entry"/"$url.html"/g" -e "s/$old_title/$title/g" "$blogdir"/blog.html # Update URLS & title in blog.html + sed -i -e "s/"$entry"/"$url.html"/g" -e "s/$old_title/$title/g" "$blogdir"/index.html # Update URLS & title in index.html + mv "$blogdir"/blog/$entry "$blogdir/blog/$url.html" # Rename file in /blog + printf "\n${GREEN}Title has been updated.${RESET}\n\n" && update + } + + descr() { + title="$(awk "/Post: $(basename "$basefile" ".html")/{getline; print}" "$datafile" | sed 's/^[^:]*: //')" + old_description="$(sed -n "/Title: $title/{ n; p }" $datafile | sed 's/^[^:]*: //')" + max=0 + while [ $max == 0 ]; do + read -e -p "Enter a new description (50 words limit): " new_description + [ $(echo "$new_description" | wc -w) -ge "50" ] && echo "Description must be less than 50 words." || max=1 + done + + tags="$(sed -n "/$(basename "$basefile" ".html")/{n;n;n;n; p}" "$datafile" | sed -e 's/^[^:]*: //' -e 's/\,//g')" + tagsArr=(${tags}) + for i in "${tagsArr[@]}"; do + case ${i} in + "Informative") + sed -i "s/$old_description/$new_description/g" "$blogdir"/blog/tags/informative.html + ;; + "Care") + sed -i "s/$old_description/$new_description/g" "$blogdir"/blog/tags/care.html + ;; + "Rant") + sed -i "s/$old_description/$new_description/g" "$blogdir"/blog/tags/rant.html + ;; + "Breeding-report") + sed -i "s/$old_description/$new_description/g" "$blogdir"/blog/tags/breeding-reports.html + ;; + "Off-topic") + sed -i "s/$old_description/$new_description/g" "$blogdir"/blog/tags/off-topic.html + ;; + "Seller-review") + sed -i "s/$old_description/$new_description/g" "$blogdir"/blog/tags/seller-reviews.html + ;; + *) ;; + esac + done + sed -i "s/Description: $old_description/Description: $new_description/g" "$datafile" + sed -i "s/$old_description/$new_description/g" "$blogdir"/blog/$entry + sed -i "s/$old_description/$new_description/g" "$blogdir"/blog.html + printf "\n${GREEN}Description has been updated.${RESET}\n\n" && update + } + + content() { + + title="$(basename $basefile .html | sed 's/-/ /g')" + url="$(echo $title | sed 's/ /-/g')" + old_rss="$(sed -n '//,//{//!{//!p}}' "$blogdir/blog/$entry" | recode ..html | sed 's/&/\\&/g')" + "$EDITOR" "$blogdir"/blog/$entry + new_rss="$(sed -n '//,//{//!{//!p}}' "$blogdir/blog/$entry" | recode ..html | sed 's/&/\\&/g')" + sed -i -e "s~$old_rss~~g" -e '/LETS test RSS FEED/n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; r'<(printf "$new_rss") "$rss_file" + printf "\n${GREEN}Description has been updated.${RESET}\n\n" && update + } + + image() { + read -e -r -p "Enter the path to a new image: " img + while [ ! -e "$img" ]; do + read -e -r -p "$img does not exist. Please try another path: " img + done + + old_img="$(cat "$datafile" | sed -n "/Post: $(basename "$entry" ".html")/{n;n;n; p}" | sed 's/^[^:]*: //' | cut -f 1 -d '.' >&1)" + img=$(echo "$img" | cut -f 1 -d '.' >&1) + if [[ -e "$drafts"/img/$img.jpg || -e "$blogdir"/assets/img/blog/$img.jpg ]] ; then + i="2" + while [[ -e "$drafts"/img/$img-$i.jpg || -e "$blogdir"/assets/img/blog/$img-$i.jpg ]]; do ((i++)); done + mv "$img.jpg" "$img-$i.jpg" + cp "$img-$i.jpg" "$blogdir"/assets/img/blog/ + rm "$blogdir"/assets/img/blog/$old_img.jpg + mv "$img-$i.jpg" "$img.jpg" + else cp "$img.jpg" "$imgdir"/blog/ + fi + + sed -i "s/Image: $old_img/Image: $img/g" "$datafile" + sed -i "s/$old_img/$img/g" "$blogdir"/blog.html + printf "\n${GREEN}Index image has been updated.${RESET}\n\n" && update + } + + printf "\n\nRevising a public entry: %s:\nUsage:\n\tt = Title\n\td = Description\n\tc = Content\n\ti = Image\n\n" "$entry" + read -p "How would you like proceed? " cmd + case "$cmd" in + t) title ;; + d) descr ;; + c) content ;; + i) image ;; + *) printf "Exiting..\n\n" && exit 1 ;; + esac +} + + +# Create, Revise, Publish, +function blog () { + printf "\nEditing arachnida.blog\nUsage:\n\tn = New Draft\n\tp = Publish Draft\n\te = Edit Draft\n\tr = Revise published post\n\tt = Delete draft\n\td = Delete publish post\n\n" + read -p "How would you like proceed? " cmd + case "$cmd" in + n*) blog_draft ;; + p*) listitem "$drafts" publish && blog_pub ;; + e*) listitem "$drafts" edit && edit_draft ;; + r*) listitem "$blogdir/blog" revise public entry && blog_edit_pub ;; + t*) listitem "$drafts" delete && confirm delete && del_draft ;; + d*) listitem "$blogdir/blog" delete && confirm delete && blog_pub_del ;; + *) printf "Exiting..\n\n" && exit 1 ;; + esac +} + + +function shop () { + $empty +} + +function gallery () { + $empty +} + + +# Webpick +case "$1" in + -b*) blog ;; + -s*) shop ;; + -g*) gallery ;; + *) printf "\n\nab blog system by Ben Nagy \n\nUsage:\n ab -b: blog\n ab -s: shop\n ab -g: gallery\n\n" ;; +esac + + +# TODO + +# Allow revision of tags +# calculate article read time diff --git a/scripts/archive/blog/doc/data.txt b/scripts/archive/blog/doc/data.txt new file mode 100644 index 0000000..c034039 --- /dev/null +++ b/scripts/archive/blog/doc/data.txt @@ -0,0 +1,5 @@ +Post: foo post +Title: foo title +Description: foo description +Image: /home/user/foo.jpg +Tags: Informative, Off-topic, diff --git a/scripts/archive/blog/doc/templates/index-tag-template.html b/scripts/archive/blog/doc/templates/index-tag-template.html new file mode 100644 index 0000000..bc63ce9 --- /dev/null +++ b/scripts/archive/blog/doc/templates/index-tag-template.html @@ -0,0 +1,8 @@ + diff --git a/scripts/archive/blog/doc/templates/index-template.html b/scripts/archive/blog/doc/templates/index-template.html new file mode 100644 index 0000000..64d0992 --- /dev/null +++ b/scripts/archive/blog/doc/templates/index-template.html @@ -0,0 +1,8 @@ + diff --git a/scripts/archive/blog/doc/templates/rss-template.xml b/scripts/archive/blog/doc/templates/rss-template.xml new file mode 100644 index 0000000..2022ec1 --- /dev/null +++ b/scripts/archive/blog/doc/templates/rss-template.xml @@ -0,0 +1,16 @@ + + + + %s + https://arachnida.blog/blog/%s + https://arachnida.blog/blog/%s + + %s + + + + + + diff --git a/scripts/archive/blog/doc/templates/template.html b/scripts/archive/blog/doc/templates/template.html new file mode 100644 index 0000000..c0c8afb --- /dev/null +++ b/scripts/archive/blog/doc/templates/template.html @@ -0,0 +1,59 @@ + + + + %s + + + + + + + + + + + + +
+
+

Arachnida blog

+
+ + + +
+
+

%s

+

%s

+
+ + + +
+
+ +
+

Tagged

+
+ +
+
+
+ +
+
+

arachnida.blog

+ +
+ XMR + RSS + Email +
+
+ + diff --git a/scripts/archive/blog/rss-parse.sh b/scripts/archive/blog/rss-parse.sh new file mode 100755 index 0000000..9f635d4 --- /dev/null +++ b/scripts/archive/blog/rss-parse.sh @@ -0,0 +1,46 @@ +#!/bin/sh + +[ ! -f "$1" ] && + echo "Give \`sup\` a page which has been added/updated." && + exit 1 + +website="https://domain.com" +rssfile="../rss/blog.xml" +replchar='^' + +link="$website/$1" +title="$(sed -n 's/\(.*\)<\/title>/\1/Ip' "$1")" + +if grep -q "<guid.*>$link</guid>" "$rssfile"; then + guid="$link#$(date '+%y%m%d%H%M%S')" + title="$title (Updated)" + echo "Explain the nature of the update:" + read -r content + [ -z "$content" ] && content="New updates to $link" +else + + guid=$link + content="$(tr '\n' $replchar < "$1" | sed " + s/.*<body>// + s/<footer>.*<\/footer>// + s/<nav>.*<\/nav>// + s/<\/body>.*// + " | tr -s $replchar '\n')" +fi + +rssdate="$(LC_TIME=en_US date '+%a, %d %b %Y %H:%M:%S %z')" + +temp="$(mktemp)"; +trap 'rm -f "$temp"' 0 1 2 3 15 +echo " +<item> +<title>$title +$guid +$link +$rssdate + + +" > "$temp" + +sed -i "//r $temp" "$rssfile" -- cgit v1.2.3