wimdupont.com

Code generator of https://wimdupont.com
git clone git://git.wimdupont.com/wimdupont.com.git
Log | Files | Refs | README | LICENSE

generate.sh (5866B)


      1 #!/bin/bash
      2 
      3 readonly NGINX_DIR=/usr/share/nginx/html
      4 readonly ARTICLES_DEST_DIR=$NGINX_DIR/articles
      5 readonly GUIDE_DEST_DIR=$NGINX_DIR/guide
      6 readonly RSS_DEST_FILE=$NGINX_DIR/rss.xml
      7 
      8 readonly REPO_PATH=$(echo "$(dirname -- "$(readlink -f "${BASH_SOURCE}")")")
      9 readonly PAGES_REPO_DIR=$REPO_PATH/pages
     10 readonly BLOG_REPO_DIR=$PAGES_REPO_DIR/blog
     11 readonly GUIDE_REPO_DIR=$PAGES_REPO_DIR/guides
     12 readonly ERROR_REPO_DIR=$PAGES_REPO_DIR/error
     13 readonly BOOKLIST_FILE=$REPO_PATH/content/booklist.csv
     14 readonly LINKS_FILE=$REPO_PATH/content/links.csv
     15 readonly HEADER_FILE=$PAGES_REPO_DIR/header/header.adoc
     16 readonly FOOTER_FILE=$PAGES_REPO_DIR/footer/footer.adoc
     17 
     18 rm -rf $NGINX_DIR/*
     19 
     20 mkdir -p $NGINX_DIR/files
     21 mkdir -p $NGINX_DIR/stylesheets
     22 mkdir -p $ARTICLES_DEST_DIR
     23 mkdir -p $GUIDE_DEST_DIR
     24 
     25 cp -rf $REPO_PATH/images "$NGINX_DIR/files"
     26 cp $REPO_PATH/pubkey.gpg "$NGINX_DIR/files"
     27 cp $REPO_PATH/main.css $NGINX_DIR/stylesheets
     28 
     29 get_page_header() {
     30 	declare -r TITLE="${1^}"
     31 	declare -r ROOT_NAV=$(echo $2 | sed 's/\//\\\//g')
     32 	declare -r SUB_TITLE=$3
     33 
     34 	cat $HEADER_FILE | sed "s/{root_nav}/${ROOT_NAV}/g" | sed "s/{title}/${TITLE}/g" && test "$SUB_TITLE" == 1 && echo -e "[.subheader]\n--\n${TITLE}\n-- "
     35 }
     36 
     37 get_page_footer() {
     38 	declare -r ROOT_NAV=$(echo $1 | sed 's/\//\\\//g')
     39 
     40 	cat $FOOTER_FILE | sed "s/{root_nav}/${ROOT_NAV}/g"
     41 }
     42 
     43 generate_pages() {
     44 	repo_path=$1
     45 	dest_path=$2
     46 	root_nav=$3
     47 	sub_title=$4
     48 
     49 	for file in $repo_path/* ; do
     50 		if [[ -f $file ]]; then
     51 			cd ${repo_path}
     52 			date=$(git log -1 --pretty="format:%ci" "$file")
     53 
     54 			cd ${dest_path}
     55 			filename=$(basename "${file}" .adoc)
     56 			get_page_header "$filename" "$root_nav" "$sub_title" >> "$dest_path/$filename.adoc"
     57 			cat "$file" >> "$dest_path/$filename.adoc"
     58 			get_page_footer "$root_nav" >> "$dest_path/$filename.adoc"
     59 
     60 			asciidoctor "$dest_path/$filename.adoc"
     61 			rm "$dest_path/$filename.adoc"
     62 
     63 			touch -a -m -d "${date}" "$dest_path/$filename.html"
     64 			touch -a -m -d "${date}" "$repo_path/$filename.adoc"
     65 		fi
     66 	done	
     67 }
     68 
     69 as_href() {
     70 	dir_ref=$1
     71 	filename=$2
     72 	date=$3
     73 	filelink=$(echo "$filename" | sed 's/ /%20/g')
     74 
     75 	echo -n "link:$dir_ref/$filelink[$filename]" && test -n "$date" && echo -n " _- ${date}_"
     76 }
     77 
     78 generate_href_page() {
     79 	name=$1
     80 	root_ref=$2
     81 	ref_dir=$3
     82 	with_date=$4
     83 
     84 	cd $ref_dir
     85 	get_page_header "${name}" > "$NGINX_DIR/$name.adoc"
     86 
     87 	readarray -t files < <(stat -c '%Y %n' * | sort -n -r)
     88 
     89 	for file in "${files[@]#* }" ; do
     90 		filename=$(echo "${file##*/}" | sed 's/.html//')
     91 		if [[ $with_date -eq 1 ]]; then
     92 			date=$(date -r "$file" "+%Y-%m-%d")
     93 			art_fileref=$(as_href "$root_ref" "$filename" "$date") 
     94 		else
     95 			art_fileref=$(as_href "$root_ref" "$filename") 
     96 		fi
     97 		echo "* $art_fileref" >> "$NGINX_DIR/$name.adoc"
     98 	done
     99 	cd $NGINX_DIR
    100 	get_page_footer "" >> "$NGINX_DIR/$name.adoc"
    101 	asciidoctor "$name.adoc"
    102 	rm "$name.adoc"
    103 }
    104 
    105 generate_error_pages() {
    106 	for file in "$ERROR_REPO_DIR"/* ; do
    107 		cp $file "$NGINX_DIR/"
    108 		cd $NGINX_DIR
    109 		filename=$(basename "${file}" .adoc)
    110 		htmlfile=$(echo "$file" | sed 's/.adoc$/.html/')
    111 		asciidoctor $filename.adoc
    112 		rm "$filename.adoc"
    113 	done
    114 }
    115 
    116 generate_books_page() {
    117 	declare -r DEST_FILE=/usr/share/nginx/html/books.adoc
    118 
    119 	get_page_header "Books" > $DEST_FILE
    120 	echo -e "[.subheader]\nThis is the collection of books of which I own a physical copy\n" >> $DEST_FILE
    121 	
    122 	sort -t';' -k4,4 -k2,2 -k5,5 -k6,6 -o $BOOKLIST_FILE $BOOKLIST_FILE
    123 	
    124 	category_check=
    125 	while IFS=';' read title author isbn category series series_number; do
    126 		if [[ $category_check != $category ]]; then
    127 			test -n "$category_check" && echo "|===" >> $DEST_FILE
    128 			echo -e "\n$category\n\n[cols=\"<70%,>30%\"]\n|===" >> $DEST_FILE
    129 			category_check=$category
    130 		fi
    131 		if [[ -z "$series" ]]; then
    132 			echo "|link:https://openlibrary.org/isbn/$isbn[$title]|$author" >> $DEST_FILE
    133 		else
    134 			series=$(test -z $series_number && echo -n $series || echo -n "$series #$series_number")
    135 			echo "|link:https://openlibrary.org/isbn/$isbn[$title] [.series]#($series)#|$author" >> $DEST_FILE
    136 		fi
    137 	done < "$BOOKLIST_FILE"
    138 
    139 	echo "|===" >> $DEST_FILE
    140 	cd $NGINX_DIR
    141 	get_page_footer >> $DEST_FILE
    142 	asciidoctor $DEST_FILE
    143 	rm $DEST_FILE
    144 }
    145 
    146 generate_links_page() {
    147 	declare -r DEST_FILE=/usr/share/nginx/html/links.adoc
    148 
    149 	get_page_header "Links" > $DEST_FILE
    150 	echo -e "[.subheader]\nLinks to websites on various topics\n" >> $DEST_FILE
    151 
    152 	sort -t';' -k4,4 -k1,1 -o $LINKS_FILE $LINKS_FILE
    153 	
    154 	category_check='1'
    155 	while IFS=';' read title url description category image ; do
    156 		if [[ $category_check != "$category" ]]; then
    157 			test -n "$category" && echo -e "\n== $category\n" >> $DEST_FILE
    158 			category_check=$category
    159 		fi
    160 		echo "* link:$url[$title] - $description" >> $DEST_FILE
    161 
    162 	done < "$LINKS_FILE"
    163 	cd $NGINX_DIR
    164 	get_page_footer >> $DEST_FILE
    165 	asciidoctor $DEST_FILE
    166 	rm $DEST_FILE
    167 }
    168 
    169 generate_rss() {
    170 	cd $BLOG_REPO_DIR
    171 	echo '<rss version="2.0"><channel><title>Wim Dupont - Blog</title><link>https://wimdupont.com/blog</link><language>en</language>' > $RSS_DEST_FILE
    172 	readarray -t files < <(stat -c '%Y %n' * | sort -n -r)
    173 
    174 	for file in "${files[@]#* }" ; do
    175 		filename=$(basename "${file}" .adoc)
    176 		date=$(stat -c '%y' "$file")
    177 
    178 		asciidoctor -e "$file"
    179 		content=$(cat "${filename}.html" | sed 's/</\&lt;/g')
    180 
    181 		rm "${filename}.html"
    182 
    183 		echo "<item>
    184 <title>$filename</title>
    185 <description>$content</description>
    186 <link>https://wimdupont.com/articles/$filename</link>
    187 <author>Wim Dupont</author>
    188 <pubDate>$date</pubDate>
    189 <guid>$filename-$date</guid>
    190 </item>" >> $RSS_DEST_FILE
    191 	done
    192 	echo '</channel></rss>' >> $RSS_DEST_FILE
    193 }
    194 
    195 generate_error_pages
    196 
    197 generate_books_page
    198 generate_links_page
    199 
    200 generate_pages $PAGES_REPO_DIR $NGINX_DIR
    201 generate_pages $GUIDE_REPO_DIR $GUIDE_DEST_DIR "../" 1
    202 generate_pages $BLOG_REPO_DIR $ARTICLES_DEST_DIR "../" 1
    203 
    204 generate_href_page "guides" "guide" $GUIDE_DEST_DIR
    205 generate_href_page "blog" "articles" $ARTICLES_DEST_DIR 1
    206 
    207 generate_rss