Aller au contenu principal

nikoblog_script (Source)

#!/bin/bash
# Version 0.1
# Author: echolib {xmpp:echolib@a-lec.org}
# file: nikoblog_script
# Description:
# Script for Nikola's blogs.
# Checking directive '.. updated' and update/add in all posts from ls time-style
# License:
# GPLv3 > https://codeberg.org/echolib/bash-tools/src/branch/main/LICENSE
niko() {
unset ${!niko_@}
# Set your posts directory
niko_dir_blog="$HOME/my-nikola-blog/"
niko_dir_posts="$HOME/my-nikola-blog/posts/"
! [[ -d $niko_dir_blog ]] \
   && echo "! Error directory: Missing $niko_dir_blog" \
   && return
! [[ -d $niko_dir_posts ]] \
   && echo "! Error directory: Missing $niko_dir_posts" \
   && return
   
niko_current_dir=${PWD}
   
case "$1" in
   -d|date)          niko__newdate ;;
   -s|start-server)  niko__start_server ;;
   -h|help|*)        niko__help ;;
esac
cd "$niko_current_dir"
unset ${!niko_@}
}
niko__newdate() {
niko_term_upd=".. updated:"
cd "$niko_dir_posts"
! (( `ls | wc -l` > 0 )) \
   && echo "! Nohting here: skipping" \
   && return
echo "# Sending new update time to post:"
while read -r "niko_post"
do
# Getting new date fom ls command
niko_new_date=`echo "$niko_post" | awk '{print $6}'`
niko_directive_new_date="$niko_term_upd $niko_new_date"
niko_filename=`echo "$niko_post" | awk '{print $8}'`
# Checking if basic ".. date:" directive, else skipping file
niko_cat_date=`
      cat -n "$niko_dir_posts$niko_filename" 2>/dev/null \
         | grep ".. date:"`
if [[ $niko_cat_date ]];then
   niko_cat_date_line_nbr=`echo $niko_cat_date | awk '{print $1}'`
   niko_cat_updated_line_nbr=$(( niko_cat_date_line_nbr + 1 ))
else
   echo "! Error date: $niko_filename | Skipping..."
   continue
fi
# Getting line ".. updated:" from filename
niko_cat_updated=`
   cat -n "$niko_dir_posts$niko_filename" 2>/dev/null \
      | grep "$niko_term_upd" `
# No directive ".. updated:"
if ! [[ $niko_cat_updated ]];then
   printf   '%s%s\n' \
            "+ Add update: $niko_filename | " \
            "Line $niko_cat_updated_line_nbr = .. updated: $niko_new_date"
   sed -i   "${niko_cat_updated_line_nbr}i.. updated: $niko_new_date" \
            "$niko_filename"
   continue
fi
# Getting line and old time from filename
niko_cat_updated_line_nbr=`echo $niko_cat_updated | awk '{print $1}'`
niko_old_date=`echo $niko_cat_updated | awk '{print $4}'`
   
# Compare if new date
[[ $niko_old_date == $niko_new_date ]] \
   && echo "= No changes: $niko_filename" \
   && continue
# Printing new updated line
printf   '%s%s\n' \
         "+ New update: $niko_filename | Line $niko_cat_updated_line_nbr : " \
         "$niko_old_date > $niko_new_date"
niko_directive_old_date="$niko_term_upd $niko_old_date"
sed -i   "${niko_cat_updated_line_nbr}s|$niko_directive_old_date|$niko_directive_new_date|" \
         "$niko_filename"
done < <(ls -l --time-style=long-iso | tail -n +2)
}
niko__start_server() {
cd "$niko_dir_blog"
nikola build && nikola serve
}
niko__help() {
cat << EOH
# niko [OPTION]
   -d | date         : Add/Change posts updated date
   -s | start-server : Short for local build && serve; whereever you are  
EOH
}