wiki/build.sh

126 lines
3.7 KiB
Bash
Raw Normal View History

2022-07-29 17:32:18 +00:00
#!/bin/bash
set -euo pipefail
2021-03-21 11:40:01 +00:00
shopt -s globstar
2022-09-16 07:04:15 +00:00
tree_output=$(mktemp "/tmp/${USER:-lucidiot-wiki}.XXXXXXXXXX")
tree_file=$(mktemp "/tmp/${USER:-lucidiot-wiki}.XXXXXXXXXX")
2021-10-24 15:52:45 +00:00
trap "rm \"$tree_output\" \"$tree_file\"" EXIT
2021-03-21 16:27:10 +00:00
2021-10-24 15:52:45 +00:00
# Variables included in the pandoc template
now="$(date -Iseconds)"
generator="$(pandoc --version | head -n1)"
2021-03-21 11:40:01 +00:00
2021-03-21 16:27:10 +00:00
function convert {
2022-07-29 17:29:36 +00:00
# Usage: convert input_file output_file output_format
if [ "$3" = html ]; then
pandoc \
--read markdown \
--write html \
--standalone \
2022-07-29 17:32:36 +00:00
--table-of-contents \
2022-07-29 17:29:36 +00:00
--template template.html \
--highlight-style zenburn \
2022-09-16 07:04:15 +00:00
--css "/~${USER:-lucidiot}/theme.css" \
2022-07-29 17:29:36 +00:00
--variable generator="$generator" \
--variable now="$now" \
-o "$2" \
"$1"
else
pandoc \
--read markdown \
--write "$3" \
2022-07-29 17:32:36 +00:00
--table-of-contents \
2022-07-29 17:29:36 +00:00
-o "$2" \
"$1"
fi
2021-03-21 16:27:10 +00:00
chmod 644 "$2"
}
2022-07-29 17:29:36 +00:00
function convert_all_md {
# Usage: convert_all_md output_directory file_extension pandoc_output_format
local file
local output
echo "Converting all Markdown files to .$2 files under $1/"
for file in content/**/*.md; do
output="${file/#content/$1}"
output="${output/%.md/.$2}"
mkdir -p "$(dirname "$output")"
chmod 755 "$(dirname "$output")"
echo " $file -> $output"
convert "$file" "$output" "$3"
done
}
convert_all_md html html html
convert_all_md gopher txt plain
2021-03-21 11:40:01 +00:00
2021-03-23 21:27:11 +00:00
function link_assets {
2022-07-29 17:29:36 +00:00
echo "Copying assets from $1 to $2"
for path in $1/*; do
echo " $path -> $2"
cp -dr --no-preserve=mode "$path" "$2"
2021-03-23 21:27:11 +00:00
done
2022-07-29 17:29:36 +00:00
echo " Fixing asset attributes"
find "$1" -type d | sed "s|^$1|$2|" | xargs chmod 755
find "$1" -type f | sed "s|^$1|$2|" | xargs chmod 644
2021-03-23 21:27:11 +00:00
}
2022-07-29 17:29:36 +00:00
link_assets assets html
link_assets assets gopher
link_assets html_assets html
link_assets gopher_assets gopher
2021-03-23 22:37:00 +00:00
2021-07-24 21:40:10 +00:00
# Build the 'everything' page and CDF feed
echo 'Building article list'
pushd html >/dev/null
tree --du --si -X --prune -P '*.html' > "$tree_output"
popd >/dev/null
2021-03-23 22:37:00 +00:00
echo "---
title: everything
---
" > "$tree_file"
2021-07-24 21:40:10 +00:00
xsltproc xsl/tree-html.xsl "$tree_output" >> "$tree_file"
2022-07-29 17:29:36 +00:00
convert "$tree_file" "html/everything.html" html
2021-03-23 22:37:00 +00:00
2021-07-24 21:40:10 +00:00
xsltproc xsl/tree-cdf.xsl "$tree_output" | xmllint --format - > "html/everything.cdf"
xmllint --noout --schema "assets/xsd/cdf.xsd" "html/everything.cdf"
chmod 644 "html/everything.cdf"
2021-03-23 22:37:00 +00:00
# Build an RSS feed
2021-07-24 21:40:10 +00:00
echo 'Building RSS feed'
2021-03-23 22:37:00 +00:00
echo '<?xml version="1.0"?>
<rss
version="2.0"
xmlns:admin="http://webns.net/mvcb/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
<channel>
<title>~lucidiot'"'"'s wiki</title>
<link>https://envs.net/~lucidiot/</link>
<description>latest articles on ~lucidiot'"'"'s personal wiki</description>
<language>en-FR</language>
<copyright>Creative Commons BY 4.0 International, ~lucidiot</copyright>
<managingEditor>lucidiot@envs.net (lucidiot)</managingEditor>
<webMaster>lucidiot@envs.net (lucidiot)</webMaster>
<generator>/bin/bash</generator>
<docs>https://www.rssboard.org/rss-specification</docs>
<admin:errorReportsTo rdf:resource="mailto:lucidiot@envs.net" />
<admin:generatorAgent rdf:resource="https://tildegit.org/lucidiot/wiki/" />
<atom:link rel="self" type="application/rss+xml" href="https://envs.net/~lucidiot/rss.xml" />
' > html/rss.xml
for file in content/**/*.md; do
2021-03-24 08:23:11 +00:00
link="${file##content/}"
2021-03-23 22:37:00 +00:00
link="https://envs.net/~lucidiot/${link/%.md/.html}"
echo '<item>
2021-03-24 08:23:11 +00:00
<title>'"$(sed -n 's|\s*title:\s*\(.*\)$|\1|p' "$file")"'</title>
2021-03-23 22:37:00 +00:00
<pubDate>'"$(date -Rd "@$(stat -c%Y "$file")")"'</pubDate>
<link>'"$link"'</link>
<guid>'"$link"'</guid>
</item>' >> html/rss.xml
done
echo ' </channel>
</rss>' >> html/rss.xml
chmod 644 html/rss.xml