Skip to content

Instantly share code, notes, and snippets.

@alexander-bauer
Last active December 20, 2015 16:18

Revisions

  1. alexander-bauer revised this gist Aug 5, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion blog
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ generate_template() {
    Title:
    Description:
    Author: $AUTHOR
    Date: $(date '+%Y-%M-%d %Hh')
    Date: $(date '+%Y-%m-%d %H:%M')
    Lang: $LANG
    ---
    " > "$1"
  2. alexander-bauer revised this gist Aug 5, 2013. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions blog
    Original file line number Diff line number Diff line change
    @@ -40,10 +40,14 @@ TEMPFILE="$(tempfile -p blog-)"
    # FUNCTIONS #
    #############

    # Check flags parses the the argument list for flags, and if it finds
    # any, ceases normal execution.
    check_flags() {
    if [ "$1" == "-r" ]; then
    CURFLAG="$1"
    if [ "$CURFLAG" == "-r" ]; then
    echo "Regenerating..."
    regenerate
    exit
    exit 0
    fi
    }

    @@ -95,7 +99,7 @@ exit_unfinished() {
    # exit.
    trap exit_unfinished INT

    check_flags
    check_flags $*

    generate_template "$TEMPFILE"
    compose
  3. alexander-bauer created this gist Aug 5, 2013.
    102 changes: 102 additions & 0 deletions blog
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    #!/bin/bash
    #
    ## blog
    #
    # This script creates a temporary file with [trofaf][] header data and
    # opens it with an editor. When this is saved and closed, the file is
    # moved to a particular directory with a title given either as the
    # first argument, or read on the command line after completion.
    #
    # Once the file has been successfully renamed to markdown, if REGENDIR
    # is set, the static site is regenerated with 'trofaf -g'.
    #
    # [trofaf]: https://github.com/PuerkitoBio/trofaf
    #

    #############
    # VARIABLES #
    #############

    AUTHOR="Your Name"
    LANG="en"
    EDITOR="emacs -nw -f markdown-mode"
    OUTDIR="$HOME/yourblog/posts"

    REGENDIR="$HOME/yourblog"
    REGENOPTIONS='-n "Your Blog Name" -t "Tagline" -b http://blog.you.com'

    ##################
    # SANE DEFAUALTS #
    ##################

    REGENERATE="trofaf -g"

    SUFFIX="md"
    FILENAME="$1"

    TEMPFILE="$(tempfile -p blog-)"

    #############
    # FUNCTIONS #
    #############

    check_flags() {
    if [ "$1" == "-r" ]; then
    regenerate
    exit
    fi
    }

    regenerate() {
    cd "$REGENDIR"
    $REGENERATE $REGENOPTIONS
    }

    generate_template() {
    echo "---
    Title:
    Description:
    Author: $AUTHOR
    Date: $(date '+%Y-%M-%d %Hh')
    Lang: $LANG
    ---
    " > "$1"
    }

    compose() {
    # While $FILENAME is length 0, edit the tempfile, ask for a
    # filename, and read it into $FILENAME.
    while [ -z "$FILENAME" ]; do
    $EDITOR "$TEMPFILE"

    echo -n "Enter post filename: "
    read FILENAME
    done
    }

    add_post() {
    mv "$1" "$OUTDIR/$2.$SUFFIX"
    if [ ! -z "$REGENDIR" ]; then
    echo "Regenerating..."
    regenerate
    fi
    }

    exit_unfinished() {
    echo "\nAbandoning blogpost $TEMPFILE"
    exit 1
    }

    ########
    # MAIN #
    ########

    # If the routine is interrupted, show the path of the tempfile and
    # exit.
    trap exit_unfinished INT

    check_flags

    generate_template "$TEMPFILE"
    compose
    add_post "$TEMPFILE" "$FILENAME"