Skip to content

Instantly share code, notes, and snippets.

@Merovius
Last active September 28, 2019 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Merovius/6736709 to your computer and use it in GitHub Desktop.
Save Merovius/6736709 to your computer and use it in GitHub Desktop.
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.
#!/bin/sh
# The file extension to use for the post
FMT="markdown"
# Maximum length title used in filename
MAXLEN=32
# Wether or not to start a jekyll server for the draft
START_JEKYLL=true
# Wether to automatically commit the post or not
AUTOCOMMIT=true
# …even when the working tree is dirty
FORCE_DIRTY=false
# …even with staged changes
FORCE_STAGED=true
# 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™
AUTOPUSH=false
set -e
success () {
echo $1
exit 0
}
fail () {
fmt=$1
shift
printf "$fmt\n" $@
exit 1
}
[ -d _drafts ] || fail "No _drafts directory!"
draft=`mktemp --tmpdir=_drafts "XXXXXXXX.$FMT"`
cat > $draft <<EOF
---
layout: post
title: "Sometitle"
---
EOF
$START_JEKYLL && jekyll serve --drafts -w . > /dev/null 2>&1 & jekyll_pid=$!
for ed in $VISUAL $EDITOR vim emacs nano
do
command -v $ed > /dev/null || continue
$ed $draft
break
done
$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
#!/bin/sh
TARGET=/var/www/vhosts/merovius.de/subdomains/blog/http
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