Skip to content

Instantly share code, notes, and snippets.

@maiquelleonel
Last active April 4, 2018 16:01
Show Gist options
  • Select an option

  • Save maiquelleonel/c39838d946cb6bdcc65efa95be8baed8 to your computer and use it in GitHub Desktop.

Select an option

Save maiquelleonel/c39838d946cb6bdcc65efa95be8baed8 to your computer and use it in GitHub Desktop.
A simple shell script to help create posts on jekyll
# Add it to your .bashrc.
# Usage: create_post "My post name"
# or: create_post
# The script will generate the slug, ask for author's, ask
# if you want to generate permalink and tags
function create_post {
title=$1
# if no post title was supplied
if [ ! "$title" ]; then
echo "Enter the post title: "
read title
fi
#create the slug
slug=$(echo "$title" | \
iconv -t ascii//TRANSLIT | \
sed -r s/\\W\|\\s/-/g | \
sed -r s/^-+\|-+$//g | \
tr A-Z a-z)
dir='_posts/'
# if _posts dir don't exists on current dir, test if we are inside the _posts dir
if [ ! -d "_posts" ]; then
dir=''
if [[ $(pwd) != *_posts ]]; then
echo "You must be inside the blog root or _posts dir"
exit
fi
fi
file="$dir$(date +%Y-%m-%d)-$slug.md"
if [[ -f "$file" ]]; then
echo "Post '$file' already exists"
else
#create file
touch "$file"
echo "---" >> $file
echo "title: \"$title\"" >> $file
echo "date: $(date +'%Y-%m-%d %H:%M:%S %z')" >> $file
echo "layout: post" >> $file
#echo "Inform the post author [Enter to leave empty]: "
#read author
author="Maiquel Leonel"
if [ "$author" ]; then
echo "author: $author" >> $file
fi
#while true; do
#echo "Generate permalink? [y/n]"
#read yn
#case $yn in
#[Yy]* )
echo "permalink: /$(date +%Y/%m/%d)/$slug/" >> $file #; break;;
#[Nn]* ) break;;
# * ) echo "Please answer yes or no.";;
#esac
#done
echo "Inform the post tags (separated by semicolon) [Enter to leave empty]: "
read taglist
if [ "$taglist" ]; then
echo "tags: " >> $file
echo "$taglist" | tr ';' '\n' | sed -E "s/^(.*)/ - \1/g" >> $file
fi
echo "---" >> $file
echo "Post created: '$file'"
#cat $file
unset stags
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment