utilies

A handful of (useful) scripts for unix-like systems
Log | Files | Refs | README | LICENSE

tano (1165B)


      1 #!/bin/sh
      2 
      3 # Configurations
      4 editor="$EDITOR"
      5 markdown_converter="markdown" # Preferably from package `discount`.
      6 html_browser="lynx"
      7 selector="fzf"
      8 pager="less"
      9 
     10 
     11 index_tags() {
     12 	grep -Poir --no-filename "^#\s.*\s\[.*\]$" . \
     13 		| grep -Po "\[(.*)\]" \
     14 		| sed 's/\[\(.*\)\]/\1/' \
     15 		| sed "s/,\s*/\n/g" \
     16 		| sort \
     17 		| uniq
     18 }
     19 
     20 
     21 find_file() {
     22     q="$1"
     23     while [ 1 ]; do
     24         [ -z "$q" ] \
     25             && tag="$(index_tags | "$selector")" \
     26             || tag="$1"
     27         q="" # Only check query first time.
     28 	    [ -z "$tag" ] && exit 1
     29         file="$(grep -Pir --color=auto "\[.*\b($tag)\b.*\]" . | "$selector" | awk -F: '{print $1}')"
     30         [ -n "$file" ] && echo "$file" && break
     31     done
     32 }
     33 
     34 
     35 tags_edit() {
     36 	file="$(find_file "$1")"
     37 	[ -z "$file" ] && exit 1
     38 
     39 	"$editor" "$file"
     40 }
     41 
     42 
     43 tags_view() {
     44 	file="$(find_file "$1")"
     45 	[ -z "$file" ] && exit 1
     46 
     47 	"$markdown_converter" "$file" | "$html_browser" -stdin --dump | less
     48 }
     49 
     50 
     51 # args:
     52 #     -e      edit
     53 #     -i      index
     54 #     -d dir  directory (default: ".")
     55 #     -t tag  tag search
     56 
     57 
     58 case "$1" in
     59 	"edit" |"-e") tags_edit  "$2" ;;
     60 	"index"|"-i") index_tags ;;
     61 	*)            tags_view  "$1" ;;
     62 esac