#!/bin/sh SSG_MEDIA_DIR="${SSG_MEDIA_DIR:-media}" SSG_SRC_DIR="${SSG_SRC_DIR:-src}" SSG_TMP_DIR="${SSG_TMP_DIR:-tmp}" SSG_OUT_DIR="${SSG_OUT_DIR:-out}" SSG_TMP_FILE="${SSG_TMP_DIR}/tmp" SSG_RSS_DIR="${SSG_SRC_DIR}/b" SSG_RSS_OUT_FILE="${SSG_OUT_DIR}/rss.xml" SSG_WEBSITE_DOMAIN="${SSG_WEBSITE_DOMAIN:-example.org}" SSG_WEBSITE_TITLE="${SSG_WEBSITE_TITLE:-Example}" SSG_WEBSITE_LINK="${SSG_WEBSITE_LINK:-https://${SSG_WEBSITE_DOMAIN}}" SSG_WEBSITE_EMAIL="${SSG_WEBSITE_EMAIL:-contact@${SSG_WEBSITE_DOMAIN}}" SSG_WEBSITE_GENERATOR="${SSG_WEBSITE_GENERATOR:-${SSG_WEBSITE_DOMAIN} ssg}}" SSG_WEBSITE_DESCRIPTION="${SSG_WEBSITE_DESCRIPTION:-example}" SSG_WEBSITE_LANGUAGE="${SSG_WEBSITE_LANGUAGE:-en}" SSG_DATE_DEFAULT_HOUR="${SSG_DATE_DEFAULT_HOUR:-17:00:00}" SSG_DATE_DEFAULT_TIMEZONE="${SSG_DATE_DEFAULT_TIMEZONE:-+0200}" uname=$(uname) __sha256="sha256" if [ "${uname}" = "Linux" ]; then __sha256="sha256sum" fi __to_html_id() { cat /dev/stdin | sed 's/ /%20/g' } __get_value() { # 1: filename # 2: Attribute name # Retrieve and return the key/value of a lowdown source file. lowdown -T ms -X "${2}" "${1}" 2>/dev/null } __get_value_title() { # 1: filename # Get the title of a file and return a safe string. local _title _title="$(__get_value ${1} title || true)" if [ "${_title}" = "" ]; then _title="$(basename ${1} | rev | cut -d "." -f 2 | rev | sed 's/-/ /g')" fi echo "${_title}" } __get_value_date() { # 1: filename # Get the date of a file and return a safe string. local _date _date="$(__get_value ${1} date || true)" if [ "${_date}" = "" ]; then _date="1970-01-01" fi echo "${_date}" } __get_value_date_human() { # 1: filename # Get a human readable date from a file and return a safe string. if [ "${uname}" = "Linux" ]; then __get_value_date "${1}" | xargs date +"%d %b %Y" -d else __get_value_date "${1}" | xargs date -j -f "%Y-%m-%d" +"%d %b %Y" fi } __get_value_date_publication() { # 1: filename # Get publication date according to rfc 2822 and return a safe string. local _date _date="$(__get_value_date ${1})" _date="${_date} ${SSG_DATE_DEFAULT_HOUR} ${SSG_DATE_DEFAULT_TIMEZONE}" if [ "${uname}" = "Linux" ]; then date -d "${_date}" +"%a, %d %b %Y %H:%M:%S %z" else date -j -f "%Y-%m-%d %H:%M:%S %z" +"%a, %d %b %Y %H:%M:%S %z" "${_date}" fi } __get_out_filename() { # 1: filename # Convert the source filename to its output destination. local _filename _filename="${1}" if $(__get_value "${1}" draft || false); then _filename="$(dirname ${1})/$(basename ${1} | "${__sha256}").html" fi echo "${_filename}" | sed 's/.md$/.html/g' | sed "s/${SSG_SRC_DIR}/${SSG_OUT_DIR}/g" } __get_final_filename() { # 1: filename # Convert the source filename to its final filename for a webserver. __get_out_filename "${1}" | cut -d "/" -f 2- } __list_files() { # 1: directory # List all regular files in a directory and its subdirectories. find "${1}" -type f } __list_files_date() { # 1: directory local _files local _file local _tmp_file _files=$(__list_files "${1}") _tmp_file="${SSG_TMP_FILE}.list_files_date" cp /dev/null "${_tmp_file}" for _file in ${_files}; do echo "$(__get_value_date ${_file})" "${_file}" >> "${_tmp_file}" done sort -r "${_tmp_file}" | cut -d " " -f 2- } __install() { # 1: filename # Copy a file to the output directory. install -D -m 0644 "${1}" "${2}" } __lowdown() { # Output a file to html lowdown --html-no-skiphtml --html-no-escapehtml "${1}" } __generate_rss_body() { lowdown -tgemini "${1}" | sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g' } __apply_template() { # 1: template name or default m4 "templates/${1:-default}" } __generate_index_line() { # 1: filename where index will be displayed # 2: filename contained in line local _show_date _show_date="$(__get_value ${1} index_date || echo false)" if "${_show_date}"; then cat <<-EOF $(__get_value_date_human ${2}) EOF fi cat <<-EOF $(__get_value_title ${2}) EOF } __generate_index() { # 1: filename # Generate and output to stdout the index of a file. local _file local _index_dir _index_dir="${SSG_SRC_DIR}/$(__get_value "${1}" index)" [ -f "${_index_dir}" ] && _index_dir=$(dirname "${_index_dir}") [ ! -d "${_index_dir}" ] && return echo "" } __generate_metadata() { # 1: filename # Generate metadata with title local _title local _show_title _show_title=$(__get_value "${1}" show_title || true) if [ "${_show_title}" = "" ]; then _title=$(__get_value_title "${1}") _title_id=$(echo ${_title} | __to_html_id) cat <<-EOF

${_title}

EOF fi } __handle_md() { # 1: filename # Handle markdown files. __lowdown "${1}" > "${SSG_TMP_FILE}.body" __generate_metadata "${1}" > "${SSG_TMP_FILE}.metadata" if [ ! "$(__get_value "${1}" index)" = "" ]; then __generate_index "${1}" > "${SSG_TMP_FILE}.index" else [ -f "${SSG_TMP_FILE}.index" ] && rm "${SSG_TMP_FILE}.index" fi __apply_template "$(__get_value "${1}" template)" > "${SSG_TMP_FILE}" __install "${SSG_TMP_FILE}" "$(__get_out_filename "${1}")" } __handle() { # 1: filename # Handle file depending on its filename and filetype. case "${1}" in *.md) __handle_md "${1}" ;; *) __install "${1}" "$(__get_out_filename ${1})" ;; esac }