aboutsummaryrefslogtreecommitdiffstats
path: root/bin/ssg
diff options
context:
space:
mode:
authorRomain Gonçalves <me@rgoncalves.se>2021-09-27 15:08:34 +0200
committerRomain Gonçalves <me@rgoncalves.se>2021-09-27 15:08:34 +0200
commit8fe5387c47196d09063e12ca6b9ad768ad4849b0 (patch)
treedc75df123d86c4027b9fb18218d1bafdc13819fe /bin/ssg
parent17278427d7e7eecd95c8bf808501188342cd8435 (diff)
downloadrgoncalves.se-8fe5387c47196d09063e12ca6b9ad768ad4849b0.tar.gz
all: Bump to version 3 of static site generation
Diffstat (limited to 'bin/ssg')
-rwxr-xr-xbin/ssg127
1 files changed, 17 insertions, 110 deletions
diff --git a/bin/ssg b/bin/ssg
index 28eeea0..d3f2e02 100755
--- a/bin/ssg
+++ b/bin/ssg
@@ -1,117 +1,24 @@
#!/bin/sh
-TEMPLATE="templates/default.html"
-STATIC_DIRS="style img"
-TMP_FILE=".tmp"
-SRC="src"
-OUT="out"
-#BASE_URL=""
+. $(dirname "${0}")/func
-. ./bin/ssg-template
+set -xe
-_log() {
- echo [${0}] "$@"
-}
+# Check if the working directory is valid.
+echo "${0}" | grep '^./bin/ssg$'
-_to_html() {
- # 1: filename
- # stdout: content
- lowdown --html-no-skiphtml --html-no-escapehtml "${1}"
-}
+src_files="$(__list_files ${src_dir})"
+dirs="${media_dir} \
+ ${src_dir} \
+ ${tmp_dir} \
+ ${out_dir}"
-_install() {
- # 1: filename
- install -D "${TMP_FILE}" $(_get_out_path "${1}")
-}
+# Ensure directories exist.
+for dir in ${dirs}; do
+ [ ! -d "${dir}" ] && mkdir "${dir}"
+done
-_template_heredoc() {
- echo ". ${PWD}/bin/ssg-template" > "tmp/heredoc"
- echo "cat <<EOF_TEMPLATE" >> "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@<a[^>]* href=\"[/^\"]*/@<a href=\"$2/@g" "${1}"
- sed -i "s@<link[^>]* href=\"[/^\"]*/@<link href=\"$2/@g" "${1}"
- sed -i "s@<img[^>]* src=\"[/^\"]*/@<img src=\"$2/@g" "${1}"
-}
-
-main() {
- local file
- local out_path
-
- # template files
- for template in header footer; do
- _log "> ${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
- out_path=$(_get_out_path "${file}")
-
- if [ "${file}" -ot "${out_path}" ]; 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
- template=$(_get_value "${file}" "template")
- template="${template:-default}"
- m4 "templates/${template}.html" > "${TMP_FILE}"
- install -D "${TMP_FILE}" "${out_path}"
- 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
+# Generate output files from sources.
+for src_file in ${src_files}; do
+ __handle "${src_file}"
+done
remember that computers suck.