Skip to content

Instantly share code, notes, and snippets.

@feryll
Created June 8, 2019 14:34
Show Gist options
  • Save feryll/41a6dd9fc296aacb30c47b2b8eed997e to your computer and use it in GitHub Desktop.
Save feryll/41a6dd9fc296aacb30c47b2b8eed997e to your computer and use it in GitHub Desktop.
script to make a jekyll post with ease
#!/bin/bash
#---------/---------------------\---------#
#--------|- Jekyll Post Creator -|--------#
#---------\---------------------/---------#
#
# Source and tweaked from:
# https://gist.github.com/marcusoftnet/2eec785d3477beacf709#file-scaffold_post
#
# Simply put the script in your site directory, edit the configs to fit your setup, and run it with:
# ./post "post title"
# What is does:
# - creates the post with the correct format of date and title
# - adds YAML front-matter (layout, title, date, tags)
# - opens the post file in editor chosen
########## Configs ##########
# Post layout
layout=post
# Post text editor
editor=code
# Post directory
folder=_posts/
# Author
author="Choi Byung Hyun"
########## Program ##########
# show help with -h
if [ "$1" == "-h" ]; then
echo "Usage: `basename $0` \"Post title\""
exit 0
elif [ -z "$1" ]; then
echo "Usage: `basename $0` \"Post title\""
exit 0
fi
# Y/n ask function
function ask {
while true; do
if [ "${2:-}" = "true" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "false" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question
read -p "$1 [$prompt] " REPLY
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
##### Variables #####
# post title
title="$1"
# convert title to filename format
# echo part replaces spaces with '-'
# awk converts it to lowercase
# sed keeps only lowercase letters and '-'
filetitle=$( echo ${1// /-} | awk '{print tolower($0)}'| sed 's/[^a-z\-]*//g')
# name of file
filename="$folder`date +%F`-$filetitle.md"
echo $filename
########## Adding to file ##########
echo "---" >> $filename
echo "title: \"$title\"" >> $filename
echo "date: `date +%F\ %H:%M:%S`" >> $filename
read -p "Categories: " tags
echo "categories: $tags" >> $filename
### Adding tags on new lines with a dash in front (separated with comma)
read -p "Tags: " tags
if [ "$tags" ]; then
echo "tags:
- $tags" | sed 's/,/\
-/g' >> $filename
fi
echo "---" >> $filename
echo >> $filename
# open in chosen editor
$editor $filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment