#!/bin/bash #shellcheck disable=SC1090 . "$(sfpath)" || exit 3 shellfu import pretty usage() { mkusage "$@" "[options] FILE" \ -- \ "Open Markdown file FILE in vimb (no junk)." \ -o \ "-C DIR Use DIR as config directory root. Default is" \ " \$HOME/.config/mdvimb." \ "-S STYLE Add CSS style STYLE to the final HTML (this"\ " implies -w). If STYLE contains slash, it will" \ " be interpreted as path, otherwise a file called"\ " STYLE.css is looked up in 'css' directory of" \ " configuration directory. Default is to not add"\ " style." \ "-c CONV_BIN Use converter CONV_BIN, which must be" \ " a program that when called without arguments," \ " accepts Markdown on standard input and prints" \ " HTML to standard output. Default is 'Markdown'."\ "-d Enable debugging output." \ "-n Don't open, just print HTML." \ "-w Wrap the generated HTML with basic HTML5 tags" \ " such as , , and" \ " . Default is to leave the HTML as is." \ "-v Enable verbose output." \ "--help Print this help text and exit." \ -- \ "Default values of options -C, -S and -c can be changed"\ "by setting environment variables MDVIMB__CONFIG_HOME," \ "MDVIMB__STYLE and MDVIMB__CONVERTER, respectively." } MDVIMB__CONVERTER=${MDVIMB__CONVERTER:-Markdown} MDVIMB__CONFIG_HOME=${MDVIMB__CONFIG_HOME:-$HOME/.config/mdvimb} MDVIMB__STYLE=${MDVIMB__STYLE:-} browseopen() { # # Open stdin in vimb # $NoOpen && { cat; return 0; } think "opening vimb" vimb - "$@" } mkwrap() { # # Create wrapper part $1 if needed # local part=$1 case $part in link) mkcssref >/dev/null || return 0 think "adding CSS style: $Style" echo ' ' ;; title) case $MdFile in "") echo ' mdvimb:(stdin)' ;; *) echo " mdvimb:$MdFile" ;; esac ;; head) echo '' echo '' echo ' ' mkwrap title mkwrap link echo ' ' echo ' ' ;; tail) echo ' ' echo '' ;; esac } may_wrap() { # # Take HTML on stdin; wrap if $Wrap # $Wrap || { cat; return 0; } think "adding HTML5 tags" mkwrap head cat mkwrap tail } mkcssref() { # # Print CSS reference # local cssfile="" case $Style in "") return 0 ;; */*) cssfile=$Style ;; *) cssfile="$ConfigHome/css/$Style.css" ;; esac test -f "$cssfile" || { warn "CSS file not found, ignoring: $cssfile" return 3 } debug -v cssfile echo "$cssfile" } main() { local MdFile local converter=$MDVIMB__CONVERTER local ConfigHome=$MDVIMB__CONFIG_HOME local converterpath local NoOpen=false local Wrap=false local Style=$MDVIMB__STYLE #shellcheck disable=SC2034 while true; do case $1 in -C) ConfigHome=$2; shift 2 ;; -S) Style=$2; shift 2 ;; -c) converter=$2; shift 2 ;; -d) PRETTY_DEBUG=true; shift ;; -n) NoOpen=true; shift ;; -v) PRETTY_VERBOSE=true; shift ;; -w) Wrap=true; shift ;; --help) usage -k ;; -*) usage -w "unknown argument: $1" ;; *) break ;; esac done test -n "$Style" && Wrap=true MdFile=$1; shift debug -v MDVIMB__CONVERTER MDVIMB__CONFIG_HOME MDVIMB__STYLE debug -v MdFile converter NoOpen Wrap Style ConfigHome converterpath=$(command -v "$converter") \ || die "converter not available: $converter" debug -v converterpath think "using converter: $converter" case $MdFile in -|"") think "reading standard input" ;; *) think "reading file: $MdFile" ;; esac case $MdFile in "") $converterpath | may_wrap | browseopen "$@" ;; -) $converterpath | may_wrap | browseopen "$@" ;; *) <"$MdFile" $converterpath | may_wrap | browseopen "$@" esac } main "$@"