Created
January 25, 2017 19:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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