123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #!/bin/bash
-
-
- . "$(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 <DOCTYPE>, <html/>, <head/> and" \
- " <body/>. 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() {
-
-
-
- $NoOpen && { cat; return 0; }
- think "opening vimb"
- vimb - "$@"
- }
-
- mkwrap() {
-
-
-
- local part=$1
- case $part in
- link)
- mkcssref >/dev/null || return 0
- think "adding CSS style: $Style"
- echo ' <style type="text/css" media="screen">'
- cat "$(mkcssref)"
- echo ' </style>'
- ;;
- title)
- case $MdFile in
- "") echo ' <title>mdvimb:(stdin)</title>' ;;
- *) echo " <title>mdvimb:$MdFile</title>" ;;
- esac
- ;;
- head)
- echo '<!DOCTYPE html>'
- echo '<html>'
- echo ' <head>'
- mkwrap title
- mkwrap link
- echo ' </head>'
- echo ' <body>'
- ;;
- tail)
- echo ' </body>'
- echo '</html>'
- ;;
- esac
- }
-
- may_wrap() {
-
-
-
- $Wrap || { cat; return 0; }
- think "adding HTML5 tags"
- mkwrap head
- cat
- mkwrap tail
- }
-
- mkcssref() {
-
-
-
- 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
-
- 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 "$@"
|