all repos — aaoth.xyz @ 49021944e3b4881486ae27cd48acf9196a0b6ed7

aaoth.xyz website

aaoth_new_post.sh (view raw)

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