wimdupont.com

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

generate.sh (6246B)


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