Optimise site script
Now working more specifically with the site subdir of notes.
This commit is contained in:
parent
bd798bc35f
commit
305c257716
|
@ -12,7 +12,7 @@ if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
INPUT_DIR=~/notes
|
INPUT_DIR=~/notes/site
|
||||||
OUTPUT_DIR=~/public_html
|
OUTPUT_DIR=~/public_html
|
||||||
|
|
||||||
pushd ~
|
pushd ~
|
||||||
|
@ -20,15 +20,15 @@ pushd ~
|
||||||
# Recursively iterate over directories, calling process_file on .md files
|
# Recursively iterate over directories, calling process_file on .md files
|
||||||
traverse_dir() {
|
traverse_dir() {
|
||||||
local dir=$1
|
local dir=$1
|
||||||
|
local depth=$2
|
||||||
for d in $dir; do
|
for d in $dir; do
|
||||||
OUTPUT=$OUTPUT_DIR${d##$INPUT_DIR}
|
OUTPUT=$OUTPUT_DIR${d##$INPUT_DIR}
|
||||||
if [ -d "$d" ]; then
|
if [ -d "$d" ]; then
|
||||||
mkdir -p $OUTPUT
|
mkdir -p $OUTPUT
|
||||||
traverse_dir "$d/*"
|
traverse_dir "$d/*" $((depth+1))
|
||||||
elif [[ $d == *.md ]]; then
|
elif [[ $d == *.md ]]; then
|
||||||
OUTPUT=${OUTPUT%.md}.html
|
OUTPUT=${OUTPUT%.md}.html
|
||||||
#OUTPUT=${OUTPUT// /_}
|
process_file "$d" "$OUTPUT" $depth
|
||||||
process_file "$d" "$OUTPUT"
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
@ -37,30 +37,34 @@ traverse_dir() {
|
||||||
process_file() {
|
process_file() {
|
||||||
local INPUT=$1
|
local INPUT=$1
|
||||||
local OUTPUT=$2
|
local OUTPUT=$2
|
||||||
|
local depth=$3
|
||||||
local TITLE=${INPUT%.md}
|
local TITLE=${INPUT%.md}
|
||||||
TITLE=${TITLE##*/}
|
TITLE=${TITLE##*/}
|
||||||
if [ "$TITLE" == 'index' ]; then
|
if [ "$TITLE" == 'index' ]; then
|
||||||
TITLE='Home'
|
TITLE='Home'
|
||||||
fi
|
fi
|
||||||
echo $INPUT
|
|
||||||
echo $OUTPUT
|
|
||||||
pandoc -f markdown -t html -o "$OUTPUT" -i "$INPUT" --standalone --template ~/code/site/template.html --variable=pagetitle:"$TITLE"
|
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 in Markdown files with working links to the new HTML files
|
||||||
replace_links() {
|
replace_links() {
|
||||||
local FILE=$1
|
local FILE=$1
|
||||||
|
local depth=$2
|
||||||
# Add .html extensions
|
# Add .html extensions
|
||||||
# sed -i 's/\(href="\)\([^"]*\)/\1\2.html/g' "$FILE"
|
|
||||||
sed -Ei.bak '/https|\.[a-z]+/!s/href="[^"]*/&.html/' "$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() {
|
setup_files() {
|
||||||
cp ~/code/site/style.css ~/public_html
|
cp ~/code/site/style.css ~/public_html
|
||||||
}
|
}
|
||||||
|
|
||||||
traverse_dir $INPUT_DIR/\*
|
traverse_dir $INPUT_DIR/\* 0
|
||||||
setup_files
|
setup_files
|
||||||
|
|
||||||
popd
|
popd
|
||||||
|
|
Loading…
Reference in New Issue