wimdupont.com

Source code for www.wimdupont.com
git clone git://git.wimdupont.com/wimdupont.com.git
Log | Files | Refs | README | LICENSE

generate.sh (6934B)


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