Skip to content

Instantly share code, notes, and snippets.

@guypursey
Created June 13, 2016 17:48
Show Gist options
  • Save guypursey/0b306c8883f95a26569c45542e07c813 to your computer and use it in GitHub Desktop.
Save guypursey/0b306c8883f95a26569c45542e07c813 to your computer and use it in GitHub Desktop.
A script for aiding workflow in a Jekyll blog.
# Run from within root folder.
# Check whether the file exists or not.
if [ -e $1 ]
then
re="([0-9]{12})\-(.*)\.md"
if [[ $1 =~ $re ]]; then
# Create date and time stamps and capture post filename.
datestamp=${BASH_REMATCH[1]:0:4}-${BASH_REMATCH[1]:4:2}-${BASH_REMATCH[1]:6:2}
hourstamp=${BASH_REMATCH[1]:8:2}
minutestamp=${BASH_REMATCH[1]:10:2}
postname=_posts/$datestamp-$hourstamp$minutestamp-${BASH_REMATCH[2]}.md
# Separate headers from content.
header=$(grep -E --max-count=1 '^\#[^#]*?$' $1)
contents=$(grep -Ev '^\#[^#]*?$' $1)
# Create YAML front-matter.
yaml=$(echo -e '---\ntitle: "'${header#\# }'"\ndate: '$datestamp' '$hourstamp':'$minutestamp'\n---\n')
# Check if relevant post already exists and whether this is a new post or an update
if [ -e $postname ]
then
echo "Updating existing post: $postname"
else
echo "Creating new post: $postname"
fi
# Output the content to the file.
echo -e "$yaml\n$contents" > $postname
# Report.
echo "Changes awaiting staging."
git status -s
else
echo "Could not find date in filename."
fi
else
echo "File does not exist."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment