Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 113 lines (93 sloc) 3.37 KB
#!/bin/bash
# Check that we have markdown installed
METADATA_LINE_COUNT=5
WORKING_DIR=$(pwd)
HAS_MARKDOWN=$(which markdown | wc -l)
if [ $HAS_MARKDOWN -eq 0 ]; then
echo "Please install markdown"
exit -1
fi
# Create the output path
OUTPUT_PATH="out"
OUTPUT_INDEX=$OUTPUT_PATH/index.html
rm -r $OUTPUT_PATH
mkdir $OUTPUT_PATH
# We'll also use Markdown for the index
# We'll do this by keeping track of all the entries *by date* and then sorting
# them after we process them
touch dated_entries
touch temp
# Time to build the site!
# Grab all the markdown files and make them into HTML
for markdownFile in $(find . -name '*.md' | sort); do
FILENAME=$(basename $markdownFile)
OUTPUTNAME=${FILENAME%.*}.html
FULLPATH=$(dirname $markdownFile)
RELATIVE_DIR=${FULLPATH/$WORKING_DIR/$OUTPUTPATH}
RELATIVE_DIR=${RELATIVE_DIR/./}
OUTPATH="$OUTPUT_PATH$RELATIVE_DIR/$OUTPUTNAME"
TITLE=${FILENAME%.*}
PRETTY_DATE=""
PATH_RELATIVE_TO_OUTPATH="$RELATIVE_DIR/$OUTPUTNAME"
METADATA=$(cat $markdownFile | head -n $METADATA_LINE_COUNT)
HAS_METADATA=$(echo "$METADATA" | head -n 2 | tail -n 1)
if [ "$HAS_METADATA" = "<!--" ]; then
TITLE=$(echo "$METADATA" | head -n 3 | tail -n 1)
DATE=$(echo "$METADATA" | tail -n 2 | head -n 1)
# Date conversions in macOS: https://stackoverflow.com/a/43927574
PRETTY_DATE=$(date -jf '%Y%m%d %H:%M' +'%B %e, %Y' "$DATE")
echo "$DATE $markdownFile $PATH_RELATIVE_TO_OUTPATH $TITLE" >> dated_entries
fi
# Make directories until the outpath
mkdir -p $OUTPATH
# ...but delete the actual directory that was made for this filename
rm -r $OUTPATH
# Touch the file we'll output to
touch $OUTPATH
# Do the actual markdown generation
cat $markdownFile | markdown > $OUTPATH
# Do some post-processing
# Insert before.html at the beginning
cat before.html $OUTPATH > temp
# Insert after.html at the end
cat temp after.html > $OUTPATH
# Insert title
sed -i '' -e "s/TITLE_HERE/$TITLE/g" $OUTPATH
# Insert date if necessary
sed -i '' -e "s/DATE_HERE/$PRETTY_DATE/g" $OUTPATH
done
# sort dated_entries > dated_entries
sort --reverse dated_entries > temp
cp temp dated_entries
# Now process the dated entries into the index
touch the_index
echo -e "<ul id='index'>" >> the_index
while read datedEntry; do
DATE=$(echo $datedEntry | awk '{ print $1 $2}')
MARKDOWN_FILE=$(echo $datedEntry | awk '{ print $3 }')
OUTLINK=$(echo $datedEntry | awk '{ print $4 }')
TITLE=$(echo $datedEntry | cut -d " " -f 5-)
PRETTY_DATE=$(date -jf '%Y%m%d%H:%M' +'%B %e, %Y' "$DATE")
echo -e "<li class='indexItem'>\n" >> the_index
echo -e "<h2 class='title'><a href='$OUTLINK'>$TITLE</a></h2>\n" >> the_index
echo -e "<h3 class='date'>$PRETTY_DATE</h3>\n" >> the_index
cat $MARKDOWN_FILE >> the_index
echo -e "</li>\n" >> the_index
done < dated_entries
echo -e "</ul>" >> the_index
cat before.html > $OUTPUT_INDEX
cat the_index | markdown >> $OUTPUT_INDEX
cat after.html >> $OUTPUT_INDEX
# Strip out title and date from index
sed -i '' -e "s/TITLE_HERE//g" $OUTPUT_INDEX
# Insert date if necessary
sed -i '' -e "s/DATE_HERE//g" $OUTPUT_INDEX
rm temp
rm dated_entries
rm the_index
# Copy over css
cp style.css out/
# Copy over the media directory
cp -r media out/
# Finally, remove readme from output (we don't want included)
rm $OUTPUT_PATH/readme*