From 66d48a7a4c98383e5baef20d352a3d2e4e681a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Gon=C3=A7alves?= Date: Sat, 7 Aug 2021 18:20:38 +0200 Subject: bin: Generation scaffold --- bin/ssg | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100755 bin/ssg (limited to 'bin/ssg') diff --git a/bin/ssg b/bin/ssg new file mode 100755 index 0000000..f47f819 --- /dev/null +++ b/bin/ssg @@ -0,0 +1,113 @@ +#!/bin/sh + +TEMPLATE="templates/default.html" +STATIC_DIRS="style img" +TMP_FILE=".tmp" +SRC="src" +OUT="out" +#BASE_URL="" + +. ./bin/ssg-template + +_log() { + echo [${0}] $@ +} + +_to_html() { + # 1: filename + # stdout: content + lowdown --html-no-skiphtml --html-no-escapehtml "${1}" +} + +_install() { + # 1: filename + install -D "${TMP_FILE}" $(_get_out_path "${1}") +} + +_template_heredoc() { + echo ". ${PWD}/bin/ssg-template" > "tmp/heredoc" + echo "cat <> "tmp/heredoc" + cat "${1}" >> "tmp/heredoc" + echo EOF_TEMPLATE >> "tmp/heredoc" + + sh "tmp/heredoc" "${1}" > "${TMP_FILE}" + _to_html "${TMP_FILE}" > "${2}" +} + +_convert_urls() { + # file url + if ! echo "${2}" | grep "^http" >/dev/null; then + sed -i "s@href=/@href=$2/@g" "${1}" + fi + sed -i "s@]* href=\"[/^\"]*/@]* href=\"[/^\"]*/@]* src=\"[/^\"]*/@ ${template}" + _to_html "templates/${template}.md" > "${TMP_FILE}" + install -D "${TMP_FILE}" "tmp/${template}.html" + done + + # markdown files + for file in $(find "${SRC}" -iname "*.md"); do + if [ "${file}" -ot $(_get_out_path "${file}") ]; then + _log "! ${file}" + continue + fi + + _log "> ${file}" + + # Metadata + _cleanup_tmp_file "tmp/metadata.html" + _render_metadata "${file}" > "tmp/metadata.html" + + # Table of content + _cleanup_tmp_file "tmp/toc.html" + if [ $(_get_value "${file}" "toc") ]; then + _render_toc "${file}" > "tmp/toc.html" + fi + + # Index of directory + _cleanup_tmp_file "tmp/index.html" + if [ $(_get_value "${file}" "index") ]; then + _render_index "${file}" > "tmp/index.html" + fi + + # Render body content + _to_html "${file}" > "tmp/body.html" + + # Erase body content with templating + if basename "${file}" | grep "^_"; then + _template_heredoc "${file}" "tmp/body.html" + fi + + # Concatenate temporary files to final page + m4 templates/default.html > "${TMP_FILE}" + install -D "${TMP_FILE}" $(_get_out_path "${file}") + done + + # html files + for file in $(find "${SRC}" -iname "*.html"); do + _log "> ${file}" + _install "${file}" + done + + # convert urls + for file in $(find "${OUT}" -iname "*.html"); do + _convert_urls "${file}" "${BASE_URL}" + done + + for dir in ${STATIC_DIRS}; do + _log "> ${dir}/" + cp -R "src/${dir}" "out/${dir}" + done +} + +main -- cgit v1.2.3