Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dg01d
Created January 25, 2017 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dg01d/5ec4bdb1cc98f745e666f808873c55b1 to your computer and use it in GitHub Desktop.
Save dg01d/5ec4bdb1cc98f745e666f808873c55b1 to your computer and use it in GitHub Desktop.
A *hillariously* kludgey script to convert my pelican markdown files to hugo-parseable YAML format
#!/bin/bash
# Save standard output and standard error
exec 3>&1 4>&2
# Redirect standard output to a log file
exec 1>/tmp/stdout.log
# Redirect standard error to a log file
exec 2>/tmp/stderr.log
mkdir mod/
for i in *.md
do [ -e "$i" ] || exit 0
OUTF=mod/${i}
# First we extract the two sets of data, Head and Body, sorting the HEAD
# This breaks badly if you have a line in your body text that starts with
# a word followed by a colon. So don't do that!
grep '^\w*:\ ' $i > TMPHEAD
grep -v '^\w*:\ ' $i > TMPBODY
sort -f TMPHEAD > SRTHEAD
# We extract the date from the data, and convert it to iso
grep -i '^date:' SRTHEAD > TMPDATE
grep -vi '^date:' SRTHEAD > TMPHEAD
sed -i .bak 's/^[Dd]ate:\ //g' TMPDATE
cat TMPDATE | sed 's/^/"/;s/$/"/' | xargs gdate --iso-8601=seconds -d > ISODATE
sed -i .bak 's/^/Date:\ "/g' ISODATE
# A somewhat repeat performance for tags, except these need individual quotes
grep -i 'tags:' TMPHEAD > TMPTAGS
grep -vi 'tags:' TMPHEAD > SRTHEAD
sed -i .bak 's/[Tt]ags://g;s/\ //g;s/^/\ "/g;s/,/",\ "/g;s/$/"\ \ ]/;s/^/Tags:\ \[/' TMPTAGS
# All remaining metadata just needs the data quoted
sed -i .bak 's/:\ /\:\ \"/;s/$/\"/' SRTHEAD
sed 's/^\s*$//g' TMPBODY > BODY
# Putting it all back together
echo "---" > $OUTF
cat ISODATE SRTHEAD TMPTAGS >> $OUTF
echo -e "---\n" >> $OUTF
cat BODY >> $OUTF
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment