Optimise site script

Now working more specifically with the site subdir of notes.
This commit is contained in:
Trevor Vallender 2023-06-05 11:15:05 +01:00
parent bd798bc35f
commit 305c257716
1 changed files with 13 additions and 9 deletions

View File

@ -12,7 +12,7 @@ if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
exit
fi
INPUT_DIR=~/notes
INPUT_DIR=~/notes/site
OUTPUT_DIR=~/public_html
pushd ~
@ -20,15 +20,15 @@ pushd ~
# Recursively iterate over directories, calling process_file on .md files
traverse_dir() {
local dir=$1
local depth=$2
for d in $dir; do
OUTPUT=$OUTPUT_DIR${d##$INPUT_DIR}
if [ -d "$d" ]; then
mkdir -p $OUTPUT
traverse_dir "$d/*"
traverse_dir "$d/*" $((depth+1))
elif [[ $d == *.md ]]; then
OUTPUT=${OUTPUT%.md}.html
#OUTPUT=${OUTPUT// /_}
process_file "$d" "$OUTPUT"
process_file "$d" "$OUTPUT" $depth
fi
done
}
@ -37,30 +37,34 @@ traverse_dir() {
process_file() {
local INPUT=$1
local OUTPUT=$2
local depth=$3
local TITLE=${INPUT%.md}
TITLE=${TITLE##*/}
if [ "$TITLE" == 'index' ]; then
TITLE='Home'
fi
echo $INPUT
echo $OUTPUT
pandoc -f markdown -t html -o "$OUTPUT" -i "$INPUT" --standalone --template ~/code/site/template.html --variable=pagetitle:"$TITLE"
replace_links "$OUTPUT"
replace_links "$OUTPUT" $depth
}
# Replace links in Markdown files with working links to the new HTML files
replace_links() {
local FILE=$1
local depth=$2
# Add .html extensions
# sed -i 's/\(href="\)\([^"]*\)/\1\2.html/g' "$FILE"
sed -Ei.bak '/https|\.[a-z]+/!s/href="[^"]*/&.html/' "$FILE"
if [ "$depth" -gt 0 ]; then
local path=$(for each in $(seq 1 $depth); do printf "..\/"; done)
local href="${path}style.css"
sed -i "s/href='style.css'/href='${href}'/g" "$FILE"
fi
}
setup_files() {
cp ~/code/site/style.css ~/public_html
}
traverse_dir $INPUT_DIR/\*
traverse_dir $INPUT_DIR/\* 0
setup_files
popd