Last active
September 5, 2020 12:59
-
-
Save mslinn/eb59ce9149ff70e04bbd20b6ccd3cd2c to your computer and use it in GitHub Desktop.
Make new Jekyll draft post
This file contains hidden or 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
| #!/bin/bash | |
| # This bash script will setup a new HTML Jekyll draft blog post and open it for editing in Notepad++ | |
| # | |
| # See https://www.mslinn.com/blog/2020/08/16/new-jekyll-post.html | |
| # | |
| # Original by Katie Harron - @pibby from https://gist.github.com/pibby/6911493 | |
| # Modified by Mike Slinn - @mslinn | |
| # - added length checks for title and description | |
| # - added reprompt for when length check fails | |
| # - launch notepad instead of Mac editor | |
| # - modified front matter | |
| # - Added optional date as command line parameter | |
| function checkLength { | |
| # $1 - minimum length | |
| # $2 - maximum length | |
| # $3 - string to test | |
| length=${#3} | |
| if (( length < $1 )); then | |
| >&2 echo "$(($1 - $length)) characters too short, please edit" | |
| return 1 | |
| elif (( length > $2 )); then | |
| >&2 echo "$(( $length - $2 )) characters too long, please edit" | |
| return 2 | |
| else | |
| >&2 echo "$length characters, excellent!" | |
| return 0 | |
| fi | |
| } | |
| function reprompt { | |
| # $1 - name of front matter variable | |
| # $2 - minimum length of user-provided value | |
| # $3 - maximum length of user-provided value | |
| if [ -z "$1" ]; then 2> echo "Error: no front matter variable provided"; exit 1; fi | |
| if [ -z "$2" ]; then 2> echo "Error: no minimum length provided"; exit 1; fi | |
| if [ -z "$3" ]; then 2> echo "Error: no maximum length provided"; exit 1; fi | |
| NAME="$1" | |
| MIN="$2" | |
| MAX="$3" | |
| LEADIN="'_%0.s'" | |
| SPACES="$( eval $(echo printf "$LEADIN" {1..$MIN}) )" | |
| (( COUNT= (($MAX * 10) - ($MIN * 10) + 5) / 10 )) | |
| NUMBERS="$( eval $(echo printf '"0123456789%0.s"' {1..$COUNT}) )" | |
| (( CHARS= $MAX - $MIN )) | |
| VALUE="" | |
| #>&2 printf "SPACES='$SPACES'\n" | |
| #>&2 printf "NUMBERS='$NUMBERS'\n" | |
| while : | |
| do | |
| >&2 printf "Post $1 (30-60 characters):\n$SPACES${NUMBERS:1:$CHARS}\n" | |
| read -e -i "$VALUE" VALUE | |
| if $(checkLength "$MIN" "$MAX" "$VALUE"); then break; fi | |
| done | |
| >&2 echo | |
| echo "$VALUE" | |
| } | |
| title="$( reprompt Title 30 60 )" | |
| ptitle=${title// /-} # convert spaces in title to hyphens | |
| # Convert title to lowercase and remove slashes | |
| plc="$( echo "$ptitle" | tr '[:upper:]' '[:lower:]' | tr -d '/' )" | |
| if [ "$1" ]; then | |
| pdate="$1" # TODO verify $1 is YYYY-mm-dd | |
| else | |
| pdate="$( date +%Y-%m-%d )" # create date as YYYY-mm-dd | |
| fi | |
| filename=./_drafts/$pdate-$plc.html # location to create the new file as year-month-day-title.md | |
| mkdir -p ./_drafts | |
| touch "$filename" # create the new blank post | |
| desc="$( reprompt Description 60 150 )" | |
| printf "Post Categories (comma delimited): " | |
| read categories | |
| printf "Post Tags (comma delimited): " | |
| read tags | |
| printf "Post Keywords (comma delimited): " | |
| read keyw | |
| printf "Banner image (bg_ .jpg): " | |
| read img | |
| cat << EOF > "$filename" # fill out YAML Front Matter and insert into the new file | |
| --- | |
| categories: [$categories] | |
| description: $desc | |
| image: $img | |
| keywords: [$keyw] | |
| last_modified_at: $pdate | |
| layout: blog | |
| title: $title | |
| tags: [$tags] | |
| --- | |
| EOF | |
| echo "Created '$filename'" | |
| notepad "$filename" # Invokes mslinn's notepad++ script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment