26 Aug 2014

Convert Literate BASH to Jekyll Markdown

Tags: text-processing

What the title says. This script powers this repository. If you’re viewing this as a webpage, it was generated by running this script with it’s own source code as input.

make-md.sh

Literate BASH Requirements

We require bash scripts that are intended to be converted to be formatted roughly as follows, with the first 10 lines being the essential part:

#!/bin/bash

# Human readable title on this line
# =================================
# space-separated tags go here
#
# Descriptive excerpt goes here. These end up on the Jekyll home page.
#
# First subheading marks end of excerpt
# -------------------------------------

# We might put some code we actually want executed here...

echo "Hello World!"

# We can even put code in the comments in our code:
#
#    echo "Infinite recursion happens here. Except it doesn't. Or does it?"
#
# Scary, huh?

exit 1

Here’s an actual example of Literate BASH in source code form.

And now, teh codez that make that^^ into Markdown that Jekyll can make into HTML.

Get the date, from the command-line, or from today.

day="$2"
if [[ -z $day ]]
then
  day=`date +%Y-%m-%d`
fi

Store to the Jekyll posts folder.

outfile="_posts/$day-$1.md"

Strip the hash-bang.

tail -n +3 $1 > $outfile

Indent everything, so the code gets indented.

sed -i 's/\(^.*$\)/    \1/' $outfile

Dedent and strip leading # from comments.

sed -i 's/^\s*#$//' $outfile
sed -i 's/^\s*# \(.*$\)/\1/' $outfile

Add Jekyll front-matter. If we just wanted Markdown and not Jekyll posts, we could exit here.

sed -i "1s;^;---\nlayout: post\ntitle: ;" $outfile

Treat the first line after the Heading as tags, and close out the front-matter.

sed -i '/^===/N;s/=\+\n\(.*\)/tags: \1\n---/' $outfile

Comments

Let's experiment with Facebook Comments. I may remove this any time, so behave yourselves, or I will turn around the car and we'll never go to Disneyland ever again and I mean it!


This website is generated from the source code it describes by this script, using Literate Programming and Jekyll. Fork me.

CARD.com encourages me to share my work like this. ShareBear wonders if maybe you'd like to work for them too?

All Content by Matt Chapman. The views are my own, and not necessarily those of of anyone who's ever employed me. The SQL VIEWS are a terrible idea; don't do that shit. Views is an awesome module, not necessarily as awesome as anyone who's ever employed me. The View is an embarrassment to humanity.