all repos — aaoth.xyz @ 835c1d8aba6722df690dd619c571054872e5c3d7

aaoth.xyz website

aaoth_new_post.sh (view raw)

  1#!/bin/sh
  2
  3SITE_DIR="$HOME/Documents/aaoth.xyz"
  4POSTS_DIR="$SITE_DIR/_posts"
  5
  6DATE_SHORT=$(date -I)
  7DATE_LONG=$(date -Isec)
  8
  9usage() {
 10    echo "usage:"
 11    echo "  $0 [OPTIONS]"
 12    echo
 13    echo "options:"
 14    echo "  -t, --title <title>"
 15    echo "          specify new post title"
 16    echo "  -g, --tags <tags>"
 17    echo "          specify new post tags"
 18    echo "  -h, --help"
 19    echo "          print usage information"
 20}
 21
 22read_title() {
 23    echo -n "new post title: "
 24    read TITLE
 25}
 26
 27read_tags() {
 28    echo -n "new post tags: "
 29    read TAGS
 30}
 31
 32create_tag_page() {
 33    cat <<- TAG > "$SITE_DIR/tags/$1.md"
 34---
 35layout: tagsort
 36tag: $1
 37title: "tags: $1"
 38permalink: /tags/$1/
 39---
 40TAG
 41}
 42
 43cd $SITE_DIR
 44
 45# if there are no arguments specified, run interactively
 46if [ $# -gt 0 ]
 47then
 48    while [ -n "$1" ]
 49    do
 50        case "$1" in
 51            --title|-t)
 52                shift
 53                TITLE=$1
 54                ;;
 55            --tags|-g)
 56                shift
 57                TAGS=$1
 58                ;;
 59            --help|-h)
 60                usage
 61                exit 2
 62                ;;
 63            *)
 64                usage
 65                exit 1
 66                ;;
 67        esac
 68        shift
 69    done
 70fi
 71
 72[ -z "$TITLE" ] && read_title
 73[ -z "$TITLE" ] && echo "title could not be empty" && exit 1
 74
 75[ -z "$TAGS" ] && read_tags
 76[ -z "$TAGS" ] && echo "specify at least one tag" && exit 1
 77
 78TITLE_FILE=$(echo $TITLE | tr '[A-Z]' '[a-z]' | sed 's/ /-/g')
 79
 80POST_FILENAME="$POSTS_DIR/$DATE_SHORT-$TITLE_FILE.md"
 81
 82for _tag in $TAGS
 83do
 84    [ ! -f "./tags/$_tag.md" ] \
 85        && echo "tag $_tag is not present, creating one" \
 86        && create_tag_page $_tag
 87done
 88
 89# template is currently hardcoded
 90cat <<-EOF > "$POST_FILENAME"
 91---
 92title: $TITLE
 93date: $DATE_LONG
 94author: la-ninpre
 95tags: $TAGS
 96---
 97
 98<!--more-->
 99
100EOF
101
102nvim -c "normal 6jo" -c "startinsert" $POST_FILENAME