#!/bin/sh -e
txt2html() {
# Transform plain-text input into HTML and insert it into the template.
# Right now this only does some URL transformations.
# Convert all plain-text links to HTML links (X).
sed -E "s|([^\"\'\>=])(http[s]?://[^[:space:]\)]*)|\1\2|g" |
sed -E "s|^(http[s]?://[^[:space:]\)]*)|\1|g" |
# Convert all plain-text links to Gemini links (X).
sed -E "s|([^\"\'\>=])(gemini://[^[:space:]\)]*)|\1\2|g" |
sed -E "s|^(gemini://[^[:space:]\)]*)|\1|g" |
# Convert all plain-text links to IRC links (X).
sed -E "s|([^\"\'\>=])(irc://[^[:space:]\)]*)|\1\2|g" |
sed -E "s|^(irc://[^[:space:]\)]*)|\1|g" |
# Convert #/words to absolute HTML links.
# Convert %/words to absolute blog links.
# Convert @/words to relative HTML links.
sed -E "s|(#/)([^ \)]*)|\1\2|g" |
sed -E "s|(%/)([^ \)]*)|\1\2|g" |
sed -E "s|(@/)([^ \)]*)|\1\2|g" |
# Convert ~/words to Source Hut URLs.
# Convert $/words to Source Hut Repo URLs.
# Conver mail/words to Source Hut Mailing List URLs.
sed -E "s|(\\~/)([^ \)]*)|\1\2|g" |
sed -E "s|(\\$/)([^ \)]*)|\1\2|g" |
sed -E "s|(\\mail/)([^ \)]*)|\1\2|g" |
# Convert [0-9] into HTML links.
sed -E "s|^([ -]*)\[([0-9\.]*)\]|\1[\2]|g" |
sed -E "s|([^\"#])\[([0-9\.]*)\]|\1[\2]|g" |
# Insert the page into the template.
sed -E '/%%CONTENT%%/r /dev/stdin' template.html |
sed -E '/%%CONTENT%%/d' |
# Insert the page path into the source URL.
sed -E "s %%TITLE%% $title "
}
page() {
pp=${page%/*} title=${page##*/} title=${title%%.txt}
mkdir -p "docs/$pp"
# If the title is index.txt, set it to the parent directory name.
# Example: /wiki/index.txt (index) -> (wiki).
case $title in index) title=${pp##*/} ;; esac
case $title in .) title=home ;; esac
# GENERATION STEP.
case $page in
*.txt)
txt2html < "site/$page" > "docs/${page%%.txt}.html"
;;
# Copy over any non-txt files.
*)
cp -f "site/$page" "docs/$page"
;;
esac
# POST-GENERATION STEP.
case $page in
# Hardlink all .txt files to the docs/ directory.
*.txt) ln -f "site/$page" "docs/$page" ;;
esac
}
main() {
repo_url=https://git.sr.ht
mail_url=https://lists.sr.ht
sr_ht_user=ctb
rm -rf docs
mkdir -p docs
(cd site && find . -type f) | while read -r page; do
printf '%s\n' "CC $page"
page "$page"
done
}
main "$@"