From 5c23070292713fc04549f811211ad727f34ee066 Mon Sep 17 00:00:00 2001 From: David Tomaschik Date: Thu, 24 Mar 2016 22:46:34 -0700 Subject: [PATCH] Add oh-my-zsh plugin to manage jekyll posts. --- .../plugins/jekyll/jekyll.plugin.zsh | 95 +++++++++++++++++++ dotfiles/zshrc | 2 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 dotfiles/zsh_custom/plugins/jekyll/jekyll.plugin.zsh diff --git a/dotfiles/zsh_custom/plugins/jekyll/jekyll.plugin.zsh b/dotfiles/zsh_custom/plugins/jekyll/jekyll.plugin.zsh new file mode 100644 index 0000000..d07773c --- /dev/null +++ b/dotfiles/zsh_custom/plugins/jekyll/jekyll.plugin.zsh @@ -0,0 +1,95 @@ +JEKYLL_DIR=${JEKYLL_DIR:-${HOME}/Projects/blog} + +function _jekyll_set_date { + local FILENAME + local DATE + FILENAME=${1} + DATE=${2} + sed -i "2,/---/{s/^date:.*$/date: ${DATE}/;s/---/date: ${DATE}\n---/}" ${FILENAME} +} + +function _jekyll_find_post { + local files + local fname + if [ -f "${1}" ] ; then + printf "${1}" + return 0 + fi + fname=${2:-${1}} + files=(${JEKYLL_DIR}/_posts/*${fname}* ${JEKYLL_DIR}/_drafts/*${fname}*) + if [ ${#files} -eq "0" ] ; then + echo "No post found for ${fname}" >&2 + return 1 + fi + if [ ${#files} -gt "1" ] ; then + echo "Ambiguous results: ${files}" >&2 + return 1 + fi + printf ${files} + return 0 +} + +function jekyll { + setopt localoptions nullglob + local JTEMPLATE + local TITLE + local SLUG + local FILENAME + local DATE + local NEWNAME + + JTEMPLATE="---\n" + JTEMPLATE+="layout: post\n" + JTEMPLATE+="title: %s\n" + JTEMPLATE+="category: Blog\n" + JTEMPLATE+="---\n" + TITLE=${@[2,-1]} + SLUG=$(echo -n ${TITLE}|tr A-Z a-z|tr -c -s -- a-z -) + DATE=`date +%Y-%m-%d` + + case "${1:-help}" in + help|--help) + command jekyll help + echo "Added by oh-my-zsh plugin:" + echo " draft Create a new draft post." + echo " post Create a new post to publish immediately." + echo " publish Publish a draft post by name." + echo " edit Edit a post." + ;; + draft) + mkdir -p "${JEKYLL_DIR}/_drafts" + FILENAME="${JEKYLL_DIR}/_drafts/${SLUG}.md" + printf "${JTEMPLATE}" "${TITLE}" > "${FILENAME}" + vim "${FILENAME}" '+$' + ;; + post) + FILENAME="${JEKYLL_DIR}/_posts/${DATE}-${SLUG}.md" + printf "${JTEMPLATE}" "${TITLE}" > "${FILENAME}" + _jekyll_set_date "${FILENAME}" "${DATE}" + vim "${FILENAME}" '+$' + ;; + publish) + FILENAME=$(_jekyll_find_post "${TITLE}" "${SLUG}") + if [ $? -ne 0 ] ; then + return + fi + if ! [[ "${FILENAME}" =~ '/_drafts/' ]] ; then + echo "${FILENAME} is not a draft." >&2 + return + fi + NEWNAME=$(echo "${FILENAME}" | sed "s/_drafts\//_posts\/${DATE}-/") + mv "${FILENAME}" "${NEWNAME}" + _jekyll_set_date "${NEWNAME}" "${DATE}" + ;; + edit) + FILENAME=$(_jekyll_find_post "${TITLE}" "${SLUG}") + if [ $? -ne 0 ] ; then + return + fi + vim "${FILENAME}" '+$' + ;; + *) + command jekyll "$@" + ;; + esac +} diff --git a/dotfiles/zshrc b/dotfiles/zshrc index e22e323..bf10e38 100644 --- a/dotfiles/zshrc +++ b/dotfiles/zshrc @@ -29,7 +29,7 @@ if [ -d $HOME/.oh-my-zsh ] ; then ZSH=$HOME/.oh-my-zsh ZSH_THEME="matir" ZSH_CUSTOM="$HOME/.zsh_custom" - plugins=(git encode64 gpg-agent pep8 pip python tmux urltools extract sudo virsh virtualenv command-not-found) + plugins=(git encode64 gpg-agent pep8 pip python tmux urltools extract sudo virsh virtualenv command-not-found jekyll) test -f /usr/share/virtualenvwrapper/virtualenvwrapper.sh && plugins+=(virtualenvwrapper) source $ZSH/oh-my-zsh.sh unset ZSH_THEME