Files
linux-bin/nj

72 lines
1.8 KiB
Plaintext
Raw Normal View History

2019-02-05 19:30:40 -08:00
#!/bin/bash
if [ "$1" == "-h" ] ; then
echo "
nj - new journal, create/open markdown text file for human writing
- creates new or opens existing file in the user's preferred EDITOR
(e.g. if defined in .bashrc) else nano is used as default.
2019-03-02 19:55:19 -08:00
Usage: nj
2019-04-03 19:34:37 -07:00
nj my-title ~/projects/foo ~/bin/nj-ucsc.tmpl
The first example above would create new timestamped file in the default
notes directory.
The second example would create/open existing file called 'my-title.md'
in a directory 'projects/foo' using a custom user template called 'nj-ucsc.tmpl'
Setup .bashrc.local or other user specific config file for defining
2019-03-02 19:55:19 -08:00
variables: 'export EDITOR=vim' or set temporarily for current session:
'export author='first last'
"
echo "$(tput setaf 6)$EDITOR $(tput setaf 7)is currently set as editor"
exit 0
fi
2019-04-03 19:34:37 -07:00
set -e
2019-02-05 19:30:40 -08:00
defTitle=journal
defDir=notes
defApp=nano #nano,vim,nvim,gvim,emacs,atom,subl
defTemplate=$HOME/bin/nj.tmpl
2019-02-05 19:30:40 -08:00
title=$1
basedir=$2
mdtemplate=$3
app=$EDITOR
2019-03-02 19:55:19 -08:00
if [[ $MY_NAME ]] & [[ -z $author ]]; then
author=$MY_NAME
fi
if [[ -z $author ]]; then
2019-03-02 19:55:19 -08:00
author=$USER
fi
2019-02-05 19:30:40 -08:00
if [[ -z $title ]]; then
2019-03-02 19:55:19 -08:00
title=$defTitle
2019-02-05 19:30:40 -08:00
fi
if [[ -z $basedir ]]; then
2019-03-02 19:55:19 -08:00
basedir=$HOME/$defDir
2019-02-05 19:30:40 -08:00
fi
if [[ -z $mdtemplate ]]; then
2019-03-02 19:55:19 -08:00
mdtemplate=$defTemplate
2019-02-05 19:30:40 -08:00
fi
if [[ -z $app ]]; then
2019-03-02 19:55:19 -08:00
app=$defApp
2019-02-05 19:30:40 -08:00
fi
fn=$basedir/$(date +"%Y-%m-%d")-$title.md
if [[ -e $fn ]]
then
2019-03-02 19:55:19 -08:00
echo "opening $fn"
$app $fn
2019-02-05 19:30:40 -08:00
else
2019-03-02 19:55:19 -08:00
echo "creating $fn"
2019-04-03 19:34:37 -07:00
cat $mdtemplate | sed -E "s|(date: ).+|\1$(date --iso-8601='seconds') |" | sed -E "s|(author: ).+|\1$author |" | sed -E "s|(title: ).+|\1$title |" >> $fn
2019-03-02 19:55:19 -08:00
$app $fn
2019-02-05 19:30:40 -08:00
fi