Skip to content

Instantly share code, notes, and snippets.

@cgrusden
Last active January 5, 2019 14:48
Show Gist options
  • Save cgrusden/a605775ac6fab4108882299e7d559d15 to your computer and use it in GitHub Desktop.
Save cgrusden/a605775ac6fab4108882299e7d559d15 to your computer and use it in GitHub Desktop.
Shell Script that creates a Jekyll blog post file for today and includes the current time into the front-matter
#!/bin/sh
# Jekyll, the static website generator (https://jekyllrb.com/) , requires you to create blog
# posts with the "date" in their title so they are properly sorted on the website.
# The problem is, I don't want to keep typing that shit. I want it done for me for todays date and I always
# want the time that I created this blog post to be put in the "front matter" (https://jekyllrb.com/docs/front-matter/)
# so I can see how long it's been since I started writing if I wanted.
# Usage: today.sh <the entire blog post title>
# Example: today.sh its a very magical day today
#####
# Install
# It's easy, just copy the contents of this file into:
# today.sh
# on your local drive, in your Jekyll directory.
# After you created the file, run:
# $ chmod 755 today.sh
# Then you'll be able to run it with:
# $ ./today.sh
# TODO: Have this file only be able to run in a current directory that has the jekyll site and to generate the
# file into the _drafts folder if it exists, if not exists, then generate the file in the _posts directory
# Generate a new post with the time stamp and user-supplied title
DATE=`date +"%Y-%m-%d"`
TIME=`date +'%H:%M %p %Z'`
while [ -n "$1" ]; do
case "$1" in
-y) DATE=`date -v-1d +"%Y-%m-%d"` ;;
--)
shift
break
;;
esac
shift
done
# Replace white space with dashes
UNDERLINED_POST=`echo $@ | sed -e 's/ /\-/g'`
DIRECTORY="_posts"
FILENAME="${DATE}-${UNDERLINED_POST}.md"
FULL_PATH="$DIRECTORY/$FILENAME"
if [ -d $DIRECTORY ]; then
echo "Creating $UNDERLINED_POST blog post"
touch $FULL_PATH
fi
# Add front-matter
echo "---\nDate: ${DATE}\nTime: ${TIME}\n---" > $FULL_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment