#!/usr/bin/env bash : << '@UsageComment' ./new-entry $1 = [00|b|n|h|i] b or 0 = find md, files without extension 0h or 00 = find .htm files ./new-entry 00 b n or 1 = new entry, must give second input as title ./new-entry n 'blog title here' r,mh,h or 2 = md file convert to html, 2nd input as filename, 3rd input is not require ( default is b/) ./foo h 011 (im) i or 3 = add/edit md filename and blog title to index, 2nd input as filename ./foo i 011 (im) @UsageComment ###### blogs(){ find . -maxdepth 1 -type f ! -name '*.*' #find file with no extension } blogTitle(){ head -1 "${1}"| sed 's/.*# //'|xargs } missing(){ if [[ -z "${1}" ]];then echo "missing input!!"; exit; fi } newEntry(){ title="${1}" #title=$(echo "$title"| sed "s/\b\(.\)/\u\1/g") #captialize, now done in css new=01 [[ -z `blogs` ]] && touch 01 if [[ -f 01 ]];then #new=$(blogs|sort -V|tail -1|cut -d"/" -f2) #new=$(ls|grep -E "^[0-9]"|sort -V|tail -1) new=$(blogs|sed 's/^..//'|grep -E "^[0-9]"|sort -V|tail -1) new=$((new+1))&& touch $new fi #markdown echo -e "### $title\n----" > $new echo 'filename:' $new 'was created' } useExtra(){ input="${1}"; output="${2}"; head -2 $input |markdown_py > $output; ## get parameters for extras on line 3 of markdown file extra=$(sed '3!d' $input| awk 'BEGIN {FS="--"};{print $2}'|cut -d':' -f2|tr -d " ") #apply extra starting from line 5 tail -n +5 $input |python2 ./bin/markdown2 --extras $extra >> $output; #break; } mdtoHtml(){ input="${1}" #markdown file output="$input.htm" #[[ -z "$2" ]] && exit; missing "$2"; todir="${2}" #relative, ../$input2/ headOut=entry-template.txt #copy input to tmp cp "$input" /tmp/$input input="/tmp/$input" new_title=$(blogTitle "$input") [[ -f $input && -f $headOut ]] || exit 1; sed -i 's/[[:blank:]]*$//' $input # each line remove blank end sed -i 's/$/ /' $input # add double space at end #checkExtra=$(awk "/markdown-extras/ {print FNR}" $input) checkExtra=$(awk "/extra/ {print FNR}" $input|grep 3) if [[ -z $checkExtra ]];then markdown_py $input > $output; #extra not found/use on 3rd line else echo 'use Extra!' useExtra "$input" $output; #if detect fi sed -i -e "1 e head -n -4 $headOut" $output #prepend $headOut except last 4 line to $output sed -i "s> $output if [[ "${2}" != "b" ]];then sed -i 's/00blogdex/00imdex/g' $output;fi mv $output "../$todir/" echo 'markdown' $input 'to' "$todir/$output" 'complete' #exit } newIndex(){ input="${1}" # md filename missing "$2"; #[[ -z "$2" ]] && exit; todir="${2}" #which dir, realtively ../$todir/ # check input md or input.htm exist in=$(blogs|grep $input) in2=$(ls "../$todir"/*.htm|grep $input) missing "$in"; #[[ -z "$in" ]] && exit; [[ -z "$in2" ]] && mdtoHtml "$input" "$todir"; #output which index [[ "${2}" != "b" ]] && id=index2.md || id=index.md echo $id #exit new_title=$(blogTitle "$input") check=$(awk "/$input.htm/ {print FNR}" $id) if [[ -z $check ]];then sed -i "1i[$new_title]($input.htm)" $id #new entry echo -e '\nnew entry added' else line=$check\s sed -i "$line<\[.*]<[$new_title]<" $id #or replace ## "s/aaa=.*/aaa=xxx/g"; "63s|^.*$|$string|" echo -e '\ntitle replaced' fi [[ "${2}" != "b" ]] && dexx=im ./index-md2html.sh $dexx #final steps exit } #### exec begins here [[ $(pwd|grep md-sh) ]] || exit; missing "$1"; # [[ -z "$1" ]] && exit infile="${2}" #blog title or md file atdir="${2}" case "$1" in 0h|00) [[ -z "$2" ]] && atdir="../b" || atdir="../$atdir"; ls "$atdir"/*.htm|sort -V;exit ;; [b0]) [[ -z `blogs` ]] && echo "find none" || blogs|sort -V;exit ;; *) ;; esac missing "$2"; #[[ -z "$2" ]] && exit outdir="${3}" #default output to b/ folder [[ -z "$3" ]] && outdir="b" case "$1" in [n1]) newEntry "$infile";echo "title: $infile";; #infile = blog title r|mh|h|2) mdtoHtml "$infile" "$outdir";; # md and todir r2|h2) mdtoHtml "$infile" im;; # md and todir [i3]) newIndex "$infile" "$outdir";; # md and todir ii|i2) newIndex "$infile" im;; # md and todir *) echo NULL; exit ;; esac exit; #http://omz-software.com/pythonista/docs/ios/markdown2.html #https://github.com/trentm/python-markdown2/wiki/link-patterns #python markdown2.py --extras name1,name2 ... # spoiler #https://pastebin.com/raw/uFhhu6GL #https://stackoverflow.com/questions/255898/how-to-iterate-over-arguments-in-a-bash-script