#!/bin/sh # site title (part of ${pagetitle} probably). sitetitle="" # site language. sitelang="en" # main site domain. sitedomain="" # relative site url, can be "/blog" or something. siteurlrel="" # full site url. siteurlfull="${sitedomain}${siteurlrel}" # site keywords (global default), don't use too many. sitekeywords="" # site description (global default). sitedescription="" # site mail used for contact "mail link". sitemail="" # site author (global). siteauthor="" # site last updated (default use date when script was run). siteupdated=$(date "+%Y-%m-%dT%H:%M:%SZ") # site owner's projects url (can be github or anything) siteprojects="" # directories containing content and metadata. # NOTE: it's recommended to use absolute paths here, use "." for current directory. pagesdir="pages" # Output dir. outputdir="output" # Markdown processor: default: is "smu". markdown="smu" #gnudate(fmt,date) gnudate() { date "+$1" -d "$2" } #bsddate(fmt,date) bsddate() { date -j "+$1" "$(printf '%s' "$2" | tr -Cd '[:digit:]')" } # added for compatibility with GNU and BSD date. alias formatdate='gnudate' if ! gnudate '%Y' '2015-01-01 00:00' 2>/dev/null; then alias formatdate='bsddate' fi #makeid(title), format "Some title" to "some-title". makeid() { printf '%s\n' "$1" | tr '[:upper:]' '[:lower:]' | \ sed -e 's@[^[:alnum:]]\{1,\}@-@g' -e 's@-*$@@g' -e 's@^-*@@g' } # initial values for page variables, use some site vars as global defaults. pagereset() { id="" title="" url="" description="${sitedescription}" keywords="${sitekeywords}" author="${siteauthor}" content="" tags="" categories="" timecreated="" datecreated="" timeupdated="" dateupdated="" } pageread() { meta="$1" . "${meta}" # source page metadata. basename=$(basename "${meta}" ".sh") datecreated=$(printf '%s' "${timecreated}" | cut -b 1-10) dateupdated=$(printf '%s' "${timeupdated}" | cut -b 1-10) # if ${id} is empty and title is set: create id based on title. if test -z "${id}" && test -n "${title}"; then id=$(makeid "${title}") fi if test -z "${id}"; then printf 'Warning: $id or $title not set in "%s", skipping...\n' "${meta}" >&2 return 1 fi if test -z "${url}"; then url="${id}.html" fi outfile="${id}.html" urlfull="${siteurlfull}/${outfile}" filename="" # ${content} not set; try data from filetypes. if test -z "${content}"; then if test -f "${pagesdir}/${basename}.html"; then filename="${pagesdir}/${basename}.html" content=$(cat "${filename}") elif test -f "${pagesdir}/${basename}.md"; then filename="${pagesdir}/${basename}.md" content=$("${markdown}" "${filename}") fi fi created="Created on: ${datecreated}
" if test "${datecreated}" != "${dateupdated}"; then created="${created}Last update on: ${dateupdated}
" fi } pageheader() { # prefix page title with site title, make sure its neatly formatted. if test -z "${title}"; then pagetitle="${sitetitle}" else pagetitle="${title} - ${sitetitle}" fi cat < ${pagetitle}
!__EOF__ } pagecontent() { pageheader cat <${title} ${created} ${content} !__EOF__ pagefooter } pagefooter() { cat <
!__EOF__ } if ! test -d "${pagesdir}"; then printf 'Error: pages directory "%s" not found.\n' "${pagesdir}" >&2 exit 1 fi # process single page as argument (handy for testing a single page). if test -n "$1"; then pagereset pageread "$1" || exit 1 pagecontent exit 0 fi # try to make output dir. mkdir -p "${outputdir}" # truncate urllist.txt (sitemap). > "${outputdir}/urllist.txt" # content for RSS, Atom sitemap and index, this is appended as a string. contentindex="" contentrss="" contentatom="" contentsitemap="" while read -r meta; do pagereset pageread "${meta}" || continue pagecontent > "${outputdir}/${outfile}" # index / posts item: append. contentindex="${contentindex}$(cat <${dateupdated} ${title} !__EOF__ )" # RSS item: append. # NOTE: GMT timezone is hard-coded because %z is not consistent, # change accordingly. contentrsspubdate=$(formatdate "%a, %d %b %Y %H:%M:%S GMT" "${timeupdated}") contentrss="${contentrss}$( cat < ${title} ${urlfull} ${contentrsspubdate} ${author} ${urlfull} !__EOF__ )" # Atom item: append. contentatomupdated=$(formatdate "%Y-%m-%dT%H:%M:%SZ" "${timeupdated}") contentatompublished=$(formatdate "%Y-%m-%dT%H:%M:%SZ" "${timecreated}") contentatom="${contentatom}$( cat < <![CDATA[${title}]]> ${urlfull} ${contentatomupdated} ${contentatompublished} ${author} ${siteurlfull} !__EOF__ )" # sitemap: sitemap.xml, append item. contentsitemap="${contentsitemap}${urlfull}" # sitemap: urllist.txt, append item, write directly to file, because # this is just a plain-text list. printf '%s\n' "${urlfull}" >> "${outputdir}/urllist.txt" done <${title} ${contentindex}
!__EOF__ pagefooter) > "${outputdir}/index.html" # RSS feed (rss.xml). cat < "${outputdir}/rss.xml" ${sitetitle} ${siteurlfull} ${sitedescription} ${sitelang} ${contentrss} !__EOF__ # Atom feed (atom.xml). cat < "${outputdir}/atom.xml" ${sitetitle} ${sitedescription} ${siteupdated} ${siteurlfull}/atom.xml ${contentatom} !__EOF__ # sitemap (sitemap.xml). cat < "${outputdir}/sitemap.xml" ${contentsitemap} !__EOF__