################################################################################################################################################################ # # SBWG - steeph's bash website generator # 2020-01-05 # Version 0.4.19 # https://log.steeph.de/entries/SBWG.html # ################################################################################################################################################################ #!/bin/bash # the directory that contains the source files/file structure - no trailing slash - can be overwritten with option -i shellbase="/home/stph/weblog" # base directory of the webserver - no trailing slash - "/" for web root webbase="/" # default output directory (where the generated web site is written to) - can be overwritten with option -o odir="$shellbase/html" # default width/height for image gallery thumbnails in pixels - I say default, but there's currently no option to overwrite this (except with styles). thumbsize="250" prepare() { echo "" > "$shellbase/tagslist" # Empty the list of tags found in the previous run (if any) rm "$shellbase"/tags/* # Remove all tag pages created in the previous run (if any) IFS=$'\n' echo "Finished preparations." } copy_styles() { mkdir -p "$odir/css" cp "$shellbase/style.css" "$odir/css/style.css" echo "Finished copying stylesheets." } copy_files() { mkdir -p "$odir/files" shopt -s dotglob # So that dotfiles (such as .htaccess) are copied by cp. cp "$shellbase"/files/* "$odir/files/" cp "$shellbase/logo.svg" "$odir/logo.svg" echo "Finished copying files and downloads." } # CREATE IMAGE GALLERY PAGES gen_galleries() { cp -r "$shellbase/galleries/" "$odir/" # Copy all the stuff. Nothing but supported image files should be in there. shopt -s nocaseglob # Should already be set, but to be save, set it again. (Because .jpg/.JPG etc.) for gallerypath in $(ls -td "$shellbase"/galleries/*/); do gallery=$(basename $gallerypath) images="" images=$(ls -td "$gallerypath"*.{jpeg,jpg,png} 2> /dev/null) if [ -n "$images" ]; # If $images is not still empty then # (In other words: if there is at least one file with one of the extensions) cat "$shellbase/head" "$shellbase/header" "$shellbase/sidebar" > "$odir/galleries/$gallery.html" for image in $images; do img=$(basename "$image") # echo "$gallery: $img" mkdir -p "$odir/galleries/$gallery/thumbnails/" convert "$image" -resize "$thumbsize"x"$thumbsize" "$odir/galleries/$gallery/thumbnails/$img" echo "" >> "$odir/galleries/$gallery.html" done cat "$shellbase/footer" >> "$odir/galleries/$gallery.html" else # If there is no file with one of the supported extensions in the directory echo "The following gallery does not contain any supported image files: $gallery" fi done echo "Finished generating image gallery pages." } # CREATE TAGSLIST gen_tagslist() { for entry in $(ls -t "$shellbase/entries/"); do # Look into each entry source file tags=$(sed -n '/-->/q;p' "$shellbase/entries/$entry"|tail -n +3) # Tag lines start at line 3 and end before the HTML comment is closed. for tag in $tags; do echo "$tag" >> "$shellbase/tagslist" # Add each found tag to the tagslist file done done sort -fn "$shellbase/tagslist" | uniq > "$shellbase/tagslist_sorted" # Sort the found tags and remove duplicates echo "Finished generating tagslist." } # CREATE SIDEBAR gen_sidebar() { sed -i '0,/^cat:.*/s/^cat:.*/Categories\n&/' "$shellbase/tagslist_sorted" # Insert "Categories" before the first occurence of "cat:". sed -i '0,/^top:.*/s/^top:.*/Topics\n&/' "$shellbase/tagslist_sorted" # Insert "Topics" before the first occurence of "top:". sed -i '0,/^lang:.*/s/^lang:.*/By Language\n&/' "$shellbase/tagslist_sorted" # Insert "By Language" before the first occurence of "lang:". # Turn the sorted list into HTML code for the sidebar echo "" >> $shellbase/sidebar echo "Finished generating sidebar file." } # CREATE TAGPAGES gen_tagpages() { for entry in $(ls -t "$shellbase/entries/"); do # Look into each entry source file. tags=$(sed -n '/-->/q;p' "$shellbase/entries/$entry"|tail -n +3) # Tag lines start at line 3 and end before the HTML comment is closed. title="$(sed -n '2p' $shellbase/entries/$entry)" # The entry's title is the second line of the source file. # For each tag found in a source file, add the source file's title and for tag in $tags; do # content to the corrosponding tagpage. echo "" >> "$shellbase/tags/$tag" # print entry content and stuff to tagpage echo "

$title

" >> "$shellbase/tags/$tag" echo >> "$shellbase/tags/$tag" cat "$shellbase/entries/$entry" >> "$shellbase/tags/$tag" echo >> "$shellbase/tags/$tag" echo >> "$shellbase/tags/$tag" done done mkdir -p "$odir/tags" # Create a HTML tagpages in the output directory from each source tagpage. for tagpage in $(ls $shellbase/tags/); do cat "$shellbase/head" "$shellbase/header" "$shellbase/sidebar" > "$odir/tags/$tagpage.html" echo "
" >> "$odir/tags/$tagpage.html" # This is just to enable styling. echo "This page shows all entries tagged with $tagpage, newest/last edited entries first." >> "$odir/tags/$tagpage.html" echo "
" >> "$odir/tags/$tagpage.html" # This is just to enable styling. cat "$shellbase/tags/$tagpage" "$shellbase/footer" >> "$odir/tags/$tagpage.html" done echo "Finished generating tagpages." } # CREATE PAGES gen_pages() { for page in $(ls -t "$shellbase/pages/"); do # Look into each file in the pages/ directory and create an HTML file from it pagefile="$odir/$page.html" # echo "" > $pagefile title="$(sed -n '2p' $shellbase/pages/$page)" # The title is used as tag and printed as <h2> in the body- # print header cat "$shellbase/head" > $pagefile echo " <title>$title | steeph's website" >> $pagefile cat "$shellbase/header" >> $pagefile # The content of the header file is included in every page, entry and tag page. # print side bar cat "$shellbase/sidebar" >> $pagefile echo "
" >> $pagefile # This is just to enable styling. # print title echo "

$title

" >> $pagefile # print page content echo "
" >> $pagefile # This is just to enable styling. cat "$shellbase/pages/$page" >> $pagefile # The content of the source file in its entirety is included in the HTML file. echo "
" >> $pagefile # /content echo "
" >> $pagefile # /body # print footer cat "$shellbase/footer" >> $pagefile # The content of the footer file is included in every page, entry and tag page. done echo "Finished generating pages." } # CREATE ENTRIES gen_entries() { mkdir -p "$odir/entries" for entry in $(ls -t "$shellbase/entries/"); do # Look into each entry source file again entryfile="$odir/entries/$entry.html" # echo "" > $entryfile title="$(sed -n '2p' $shellbase/entries/$entry)" # print header cat "$shellbase/head" > $entryfile echo " $title | steeph's website" >> $entryfile cat "$shellbase/header" >> $entryfile # The content of the header file is included in every page, entry and tag page. # print side bar cat "$shellbase/sidebar" >> $entryfile echo "
" >> $entryfile # This is just to enable styling. # print title echo "

$title

" >> $entryfile # print entry name echo "$entry" >> $entryfile tags=$(sed -n '/-->/q;p' $shellbase/entries/$entry|tail -n +3) # Tag lines start at line 3 and end before the HTML comment is closed. echo "
    " >> $entryfile for tag in $tags; do echo "
  • $tag
  • " >> $entryfile # Print a list of tags for this entry, linked to the corrosponding tagpages. done echo "
" >> $entryfile # print entry content echo "
" >> $entryfile # This is just to enable styling. cat "$shellbase/entries/$entry" >> $entryfile # The content of the source file in its entirety is included in the HTML file. echo "
" >> $entryfile # /content # attach gallery (if there is one for this entry) echo "
" >> $entryfile gallerypath="$shellbase/galleries/$entry" ls "$gallerypath" &> /dev/null # See if there if a gallery folder for this entry. if [[ $? -eq 0 ]]; then # If there is a gallery directory images="" images=$(ls -td "$gallerypath"/*.{jpeg,jpg,png} 2> /dev/null) if [ -n "$images" ]; # If $images is not still empty then # (In other words: if there is at least one file with one of the extensions) for image in $images; do img=$(basename "$image") echo "" >> $entryfile done fi fi echo "
" >> $entryfile # /gallery echo "
" >> $entryfile # /body # print footer cat "$shellbase/footer" >> $entryfile # The content of the footer file is included in every page, entry and tag page. done echo "Finished generating entries." } # CREATE all.html gen_all() { cat "$shellbase/head" "$shellbase/header" "$shellbase/sidebar" > "$odir/all.html" echo "
" >> "$odir/all.html" # This is just to enable styling. echo "This page shows all entries, newest/last edited entries first." >> "$odir/all.html" echo "
" >> "$odir/all.html" # This is just to enable styling. for entry in $(ls -t $shellbase/entries/); do # Look into each entry source file. title="$(sed -n '2p' $shellbase/entries/$entry)" # The entry's title is the second line of the source file. echo "" >> "$odir/all.html" # print entry content and stuff to tagpage echo "

$title

" >> "$odir/all.html" echo >> "$odir/all.html" cat "$shellbase/entries/$entry" >> "$odir/all.html" echo >> "$odir/all.html" echo >> "$odir/all.html" done cat "$shellbase/tags/$tagpage" "$shellbase/footer" >> "$odir/all.html" echo "Finished generating all.html." } # CREATE RSS FEED gen_rss() { echo "RSS feed generation is not implemented, yet." } prepare if [ $# -eq 0 ]; # If no arguments are provided, only generate tags, sidebar and entries, then # as this is the combination I use most. echo "No options provided. Assuming -tear" gen_tagslist gen_sidebar gen_tagpages gen_entries gen_all gen_rss exit 0 else # If there is at least one argument, let getopts handle the options. shopt -s nocasematch # We need to differentiate between options -h and -H while getopts ":i:o:tcfgpearsnhH" opt; do case ${opt} in i) # input directory shellbase="$OPTARG" ;; o) # output directory odir="$OPTARG" mkdir -p "$odir" ;; c) # complete copy_styles copy_files gen_galleries gen_tagslist gen_sidebar gen_tagpages gen_pages gen_entries gen_all gen_rss exit 0 ;; g) # galleries gen_galleries ;; t) # tags gen_tagslist gen_sidebar gen_tagpages ;; p) # pages gen_pages ;; e) # entries gen_entries ;; a) # all.html gen_all ;; r) # rss gen_rss ;; s) # styles copy_styles ;; f) # files copy_files ;; n) # new echo "Option $arg is not implemented, yet." ;; h) # help echo "Option $arg is not implemented, yet. Maybe there is a README file by now." exit 0 ;; H) # much help more "$shellbase/README" exit 0 ;; \?) echo "Unfamiliar option: -$OPTARG. Available options are: -i -o -t -c -f -p -e -a -r -s -n -h -H (Try -h for more help.)" exit 1 ;; :) echo "Misused option: -$OPTARG needs an argument. Try -h for more help." exit 1 ;; esac done fi exit 1 ################################################################################################################################################################ # todo: # - readme.txt # - image galery generator # - gallery page html and styling # - create index.html in html/galleries/ with list of galleries? # - RSS(/ATOM?) feed(s?) # - categories sorted chronologically newest first, topics sorted chronologically oldest first # - print "last generated" date at least as HTML comment # - add webbase to src="/ and href="/ and the like ################################################################################################################################################################