add script to create posts

This commit is contained in:
2021-02-18 13:24:23 +01:00
parent ce06a877f7
commit c72130cea6
6 changed files with 99 additions and 1 deletions

50
scripts/check_assets.sh Executable file
View File

@@ -0,0 +1,50 @@
#!/bin/bash
BASEDIR=`dirname $0`
if [ -f $BASEDIR/config.sh ]; then
. $BASEDIR/config.sh
fi
if [ -z "$POSTS_DIR" ]
then
echo "POSTS_DIR environment variable is unset." > /dev/stderr
exit 2
fi
if [ -z "$ASSETS_DIR" ]
then
echo "ASSETS_DIR environment variable is unset." > /dev/stderr
exit 2
fi
function check(){
local rc=0
sed -rn 's|^.*\!\[.*\]\(%assets_url%/(.*)\)$|\1|p' $1 | while read line
do
if [ ! -f "$BASEDIR/$ASSETS_DIR/$line" ]
then
echo "`basename $1`: $line not found" >&2
rc=1
fi
done
return $rc
}
if [ -z "$1" ]
then
rc=0
for file in $BASEDIR/$POSTS_DIR/*
do
check $file
if [ $? -ne 0 ]
then
rc=1
fi
done
else
check $BASEDIR/$POSTS_DIR/$1
rc=$?
fi
exit $rc

65
scripts/check_post_filename.sh Executable file
View File

@@ -0,0 +1,65 @@
#!/bin/bash
BASEDIR=`dirname $0`
if [ -f $BASEDIR/config.sh ]; then
. $BASEDIR/config.sh
fi
if [ -z "$POSTS_DIR" ]
then
echo "POSTS_DIR environment variable is unset." > /dev/stderr
exit 2
fi
FIX=0
while getopts "f" option
do
case $option in
f)
FIX=1
;;
esac
done
shift $(($OPTIND - 1))
function check() {
local rc=0
post=$1
title=`sed -n 's/^title: "\?\(.*\)"\?$/\1/p' $post`
safename=`echo $title | iconv -t ascii//TRANSLIT | tr '[:upper:]' '[:lower:]' | tr -s '[:punct:] ' '-' | sed 's/-*$//g;s/^-*//g'`
if [ $post == $safename.md ]
then
rc=0
else
if [ $FIX -eq 1 ]
then
mv -v $post $safename.md
else
echo "$post should be named $safename.md" >&2
fi
rc=1
fi
return $rc
}
cd $BASEDIR/$POSTS_DIR
if [ -z "$1" ]
then
rc=0
for post in *
do
check $post
if [ $? -ne 0 ]
then
rc=1
fi
done
else
check $1
rc=$?
fi
exit $rc

2
scripts/config.sh Normal file
View File

@@ -0,0 +1,2 @@
POSTS_DIR="../content/posts"
ASSETS_DIR="../assets"

83
scripts/new_post.sh Executable file
View File

@@ -0,0 +1,83 @@
#!/bin/bash
set -e
BASEDIR=`dirname $0`
if [ -f $BASEDIR/config.sh ]; then
. $BASEDIR/config.sh
fi
if [ -z "$POSTS_DIR" ]
then
echo "POSTS_DIR environment variable is unset." > /dev/stderr
exit 2
fi
function usage {
echo -e "Usage: $0 [OPTIONS] <post title>
Options:
-h\t\tshow this help
-d DATE\tuse DATE instead of today's date
-a AUTHOR\tuse AUTHOR instead of env var \$AUTHOR
-t TAG\tadd TAG to the taglist (can be used multiple times or contain comma-separated list)
-e\t\topen new post in \$EDITOR"
}
function filename {
local unsafe_name="$1"
echo $unsafe_name | iconv -t ascii//TRANSLIT | tr '[:upper:]' '[:lower:]' | tr -s '[:punct:] ' '-' | sed 's/-*$//g;s/^-*//g'
}
DATE=$(date +%Y-%m-%d)
TAGLIST=''
edit=0
while getopts "hd:a:t:e" option
do
case $option in
h)
usage
exit
;;
d)
DATE=$(date +%Y-%m-%d "$OPTARG")
;;
a)
AUTHOR="$OPTARG"
;;
t)
if [ -z $TAGLIST ]
then
TAGLIST="$OPTARG"
else
TAGLIST="$TAGLIST,$OPTARG"
fi
;;
e)
edit=1
;;
*)
echo "Use $0 -h to show help" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
TITLE="$1"
TEMPLATE=post
FILENAME="$(filename "$TITLE").md"
export TITLE AUTHOR DATE TAGLIST TEMPLATE
cat $BASEDIR/post.tpl | envsubst > $BASEDIR/$POSTS_DIR/$FILENAME
if [ $edit == 1 ]
then
$EDITOR $BASEDIR/$POSTS_DIR/$FILENAME
else
echo $FILENAME
fi

9
scripts/post.tpl Normal file
View File

@@ -0,0 +1,9 @@
---
title: $TITLE
date: $DATE
author: $AUTHOR
template: $TEMPLATE
tags: $TAGLIST
---
(Write somthing here.)