Created
October 22, 2012 21:34
-
-
Save malexandre/3934578 to your computer and use it in GitHub Desktop.
Bash function for Jekyll to create drafts & publish them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function draft() | |
{ | |
local editor="" | |
local title="" | |
while test $# -gt 0; do | |
case "$1" in | |
-c) | |
editor="$2" | |
shift | |
shift | |
;; | |
*) | |
if [[ "$title" = "" ]]; then | |
title="$1" | |
fi | |
shift | |
;; | |
esac | |
done | |
if [[ -f ./_config.yml ]]; then | |
if [[ ! -d ./_drafts ]]; then | |
mkdir _drafts | |
fi | |
if [[ "$title" = "" ]]; then | |
title="untitled-`date +%s`.markdown" | |
elif [[ "$title" != *.markdown && "$title" != *.md ]]; then | |
title="$title.markdown" | |
fi | |
title=`echo "$title" | sed -e 's/['"'"' _]/-/g'` | |
title=`echo "$title" | sed -e 's/[éèê]/e/g'` | |
title=`echo "$title" | sed -e 's/à/a/g'` | |
title=`echo "$title" | sed -e 's/ï/i/g'` | |
title=`echo "$title" | sed -e 's/û/u/g'` | |
title=`echo "$title" | sed -e 's/ô/o/g'` | |
title=`echo "$title" | sed -e 's/[^0-9a-zA-Z.-]//g'` | |
if [[ -f ./_drafts/"$title" ]]; then | |
echo "The file '_drafts/$title' already exists, please choose a different name" | |
return 1 | |
fi | |
echo -e "\ | |
---\n\ | |
layout: post\n\ | |
title: $title\n\ | |
---\n\ | |
Content goes here\n" > _drafts/"$title" | |
if [[ "$editor" != "" ]]; then | |
"$editor" _drafts/"$title" | |
else | |
echo "File available at _drafts/$title" | |
fi | |
else | |
echo "Not in a Jekyll folder" | |
fi | |
} | |
function publish() | |
{ | |
if [[ -f ./_config.yml ]]; then | |
if [[ "$1" = "" ]]; then | |
echo "No file to publish in argument" | |
return 1 | |
fi | |
if [[ ! -f ./"$1" ]]; then | |
echo "'$1' not found" | |
return 1 | |
fi | |
if [[ "test.md" =~ \.(md|markdown)$ ]]; then | |
local currentDateYAML=`date +%Y-%m-%d\ %H:%M:%S` | |
local currentDateJekyll=`date +%Y-%m-%d` | |
local filename=$(basename $1) | |
head -n 1 "$1" > "_posts/$currentDateJekyll-$filename" | |
echo "date: $currentDateYAML" >> "_posts/$currentDateJekyll-$filename" | |
tail -n +2 "$1" >> "_posts/$currentDateJekyll-$filename" | |
rm -f "$1" | |
else | |
echo "'$1' is not a markdown file" | |
fi | |
else | |
echo "Not in a Jekyll folder" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use it, source it or paste it in your .bashrc.
draft [-c ] []
publish
The will be parsed to remove all unwanted characters. If no is given, "untitled-TIMESTAMP" will be used.
If an is given, it will be use to edit the file after creation (via the commande " file", so vim compatible).
For now, only the markdown files are managed.