Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Automatically create a jekyll blogpost from a template. An editor is launched to edit the post, after it exits, the current date and time is added to the preamble and it is saved under a filename containing date and mangled article title into _posts. Optionally launches a jekyll server while editing the draft and commits the resulting blogpost.
# The file extension to use for the post
# Maximum length title used in filename
# Wether or not to start a jekyll server for the draft
# Wether to automatically commit the post or not
# …even when the working tree is dirty
# …even with staged changes
# Commit-message to use. $target will be replaced with the file written, $title
# with the shortened title, $fulltitle with the full title. Add more slashes to
# escape properly.
COMMIT_MSG='Add post $title\\n\\nTitle: $fulltitle'
# Automatically push the commit. Requires the repo to be set up, such that „git push“ just works™
set -e
success () {
echo $1
exit 0
fail () {
printf "$fmt\n" $@
exit 1
[ -d _drafts ] || fail "No _drafts directory!"
draft=`mktemp --tmpdir=_drafts "XXXXXXXX.$FMT"`
cat > $draft <<EOF
layout: post
title: "Sometitle"
$START_JEKYLL && jekyll serve --drafts -w . > /dev/null 2>&1 & jekyll_pid=$!
for ed in $VISUAL $EDITOR vim emacs nano
command -v $ed > /dev/null || continue
$ed $draft
$START_JEKYLL && kill -INT $jekyll_pid
end=`tail -n +2 $draft | grep -n -- --- | head -c -5`
fulltitle=`head -n $end $draft | grep -o -P '(?<=title: ")[^"]*'`
title=`echo -n "$fulltitle" | tr -cd "A-Za-z0-9 " | tr "A-Z " "a-z-" | head -c $MAXLEN`
target=`date +"_posts/%Y-%m-%d-$title.$FMT"`
head -n $end $draft > $target
echo "date:" `date +"%Y-%m-%d %H:%M:%S"` >> $target
tail -n +$(($end+1)) $draft >> $target
rm -f $draft
$AUTOCOMMIT || success $target
git diff --quiet || $FORCE_DIRTY || fail "Dirty worktree, won't commit.\nFile written to $target"
git diff --staged --quiet || $FORCE_STAGED || fail "Unstaged changes, won't commit.\nFile written to $target"
git add $target
commitmsg=`mktemp --tmpdir "jekyll-commit-msg-XXXXXXXX"`
eval "echo $COMMIT_MSG" > $commitmsg
git commit -e -F $commitmsg || git reset HEAD $target || echo $target
rm -f $commitmsg
$AUTOPUSH || exit 0
git push
tempdir=`mktemp --tmpdir -d jekyll_build-XXXXXXXX`
env -i git clone --depth 1 file://`pwd` $tempdir
jekyll build -s $tempdir -d $TARGET
rm -rf $tempdir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.