kloeckner.com.ar

a backup of my entire webpage
Index Commits Files Refs README LICENSE
scripts/sync.sh (5148B)
   1 #!/bin/sh
   2 
   3 root_folder="$(pwd)"
   4 blog_folder="blog"
   5 html_folder="blog"
   6 blog_index_file="common/blog_index.shtml"
   7 latest_uploads_file="common/latest_uploads.shtml"
   8 rss_feed_file=$root_folder/"rss.xml"   # RSS feed file
   9 blog_folders=$("$root_folder"/scripts/sort_blog_index.py "$blog_folder")
  10 index_latest_uploads_count=6
  11 
  12 generate_blog_index() {
  13     update=false
  14     for i in $(ls $root_folder/$blog_folder); do
  15         # if exists and it's a directory
  16         if [ -d $root_folder/$blog_folder/$i ]; then
  17             last_modified=$(stat $root_folder/$blog_folder/$i/$i.md --format "%Y")
  18             blog_index_file_date=$(stat $blog_index_file --format "%Y")
  19 
  20             if [ $last_modified -gt $blog_index_file_date ]
  21             then
  22                 echo "     └─ $i.html is newer than $blog_index_file"
  23                 echo "+ Regenerating blog index file"
  24                 update=true
  25                 break
  26             fi
  27         fi
  28     done
  29 
  30     [ ! -z "$1" ] && update=true;
  31     [ "$update" = "false" ] && echo \"     └─ Blog index file up to date\" && return 1
  32     echo "     └─ Updating blog index"
  33 
  34     rm -rf $root_folder/$blog_index_file ||:
  35     touch $root_folder/$blog_index_file
  36 
  37     for i in ${blog_folders[@]}; do
  38         # deprecated format
  39         # article_date=$(cat $i | grep -oP '(?<=% date: \")(.*?)(?=\")')
  40         # article_title=$(cat $i | grep -oP '(?<=% title: \")(.*?)(?=\")')
  41         article_date=$(cat $i | sed '/^%%/,/^%%/!d' | grep -oP '(?<=date: \")(.*?)(?=\")')
  42         article_title=$(cat $i | sed '/^%%/,/^%%/!d' | grep -oP '(?<=title: \")(.*?)(?=\")')
  43         [ -z "$article_title" ] || [ -z "$article_date" ] && echo "error: no metadata found on file $input" && exit 1
  44 
  45         file_name=$(echo "$i" | grep -oE '[^/]*$' | cut -d '.' -f 1)
  46 
  47         printf "<li><b-time>%s</b-time> <a href=\"/$blog_folder/$file_name/$file_name.html\">%s</a></li>\n" \
  48             "${article_date}" "${article_title}" >> $root_folder/$blog_index_file
  49     done
  50 }
  51 
  52 generate_latest_uploads() {
  53     rm -rf $root_folder/$latest_uploads_file ||:
  54 
  55     head -n $index_latest_uploads_count $root_folder/$blog_index_file > $root_folder/$latest_uploads_file
  56 }
  57 
  58 generate_rss_feed() {
  59     rm $rss_feed_file &> /dev/null
  60 
  61     cp "$root_folder"/scripts/rss_feed_top.xml $rss_feed_file
  62     printf "<lastBuildDate>%s</lastBuildDate>\n" "$(date)" >> $rss_feed_file
  63 
  64     for i in ${blog_folders[@]}; do
  65         # article_date=$(cat $i | grep -oP '(?<=<meta name="article-date" content=")(.*?)(?=")')
  66         # article_title=$(cat $i | grep -oP '(?<=<meta name="article-title" content=")(.*?)(?=")')
  67         # deprecated format
  68         # article_date=$(cat $i | grep -oP '(?<=% date: \")(.*?)(?=\")')
  69         # article_title=$(cat $i | grep -oP '(?<=% title: \")(.*?)(?=\")')
  70 
  71         article_date=$(cat $i | sed '/^%%/,/^%%/!d' | grep -oP '(?<=date: \")(.*?)(?=\")')
  72         article_title=$(cat $i | sed '/^%%/,/^%%/!d' | grep -oP '(?<=title: \")(.*?)(?=\")')
  73         [ -z "$article_title" ] || [ -z "$article_date" ] && echo "error: no metadata found on file $input" && exit 1
  74 
  75         file_name=$(echo "$i" | grep -oE '[^/]*$' | cut -d '.' -f 1)
  76 
  77         article_description="$(sed '/^%%/,/^%%/d' $root_folder/md/$file_name/$file_name.md | md2html |\
  78             sed -E -e 's/ \(last\ update//g' -e 's/\{[^\}]*\}//g'\
  79                    -e 's/<code>|<\/code>//g' -e 's/<em>|<\/em>//g')"
  80 
  81         # Create RSS feed item
  82         echo "<item>" >> $rss_feed_file
  83         echo "<title>$article_title</title>" >> $rss_feed_file
  84         echo "<link>https://kloeckner.com.ar/blog/$file_name/$file_name.html</link>" >> $rss_feed_file
  85         echo "<description>
  86                 <htmlData>
  87                     <![CDATA[<html>$article_description</html>]]>
  88                 </htmlData>
  89             </description>" >> $rss_feed_file
  90         echo "<pubDate>$article_date</pubDate>" >> $rss_feed_file
  91         echo "$article_date"
  92         echo "</item>" >> $rss_feed_file
  93         echo "" >> $rss_feed_file
  94     done
  95 
  96     # Close the RSS feed file
  97     echo "" >> $rss_feed_file
  98     echo "</channel>" >> $rss_feed_file
  99     echo "</rss>" >> $rss_feed_file
 100 }
 101 
 102 check_rss_feed_last_build() {
 103     [ ! -e $rss_feed_file ] && echo "+ Regenerating rss feed" && generate_rss_feed && return
 104     updated=false
 105     for i in $(ls $root_folder/$blog_folder); do
 106         # if exists and it's a directory
 107         if [ -d $root_folder/$blog_folder/$i ]; then
 108             last_modified=$(stat $root_folder/$blog_folder/$i/$i.md --format "%Y")
 109             rss_file_date=$(stat $rss_feed_file --format "%Y")
 110 
 111             if [ $last_modified -gt $rss_file_date ]
 112             then
 113                 echo "     └─ $i.html is newer than $rss_feed_file"
 114                 echo "+ Regenerating rss feed"
 115                 generate_rss_feed && updated=true
 116                 break
 117             fi
 118         fi
 119     done
 120     $updated || echo "     └─ Rss feed file up to date" && echo ""
 121 }
 122 
 123 [ ! -d $root_folder ] && \
 124     echo "error: root_folder: $root_folder not found" && exit 1
 125 
 126 echo "* root_folder: $root_folder"
 127 echo "* blog_folder: $blog_folder"
 128 echo "* blog_index_file: $blog_index_file"
 129 echo "* latest_uploads_file: $latest_uploads_file"
 130 echo "* rss_feed_file: $rss_feed_file"
 131 echo "+ generate_blog_index"
 132 generate_blog_index
 133 
 134 if [ $? -eq 1 ]; then
 135     echo "+ generate_latest_uploads"
 136     generate_latest_uploads
 137 else
 138     echo "+ generate_latest_uploads"
 139     echo "     └─ Index file up to date"
 140 fi
 141 
 142 case "$1" in
 143     --force-update) 
 144         echo "+ generate_rss_feed"; generate_rss_feed;
 145         echo "+ generate_blog_index"; generate_blog_index "force-update";;
 146     *) echo "+ check_rss_feed_last_build"; check_rss_feed_last_build;;
 147 esac