123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. usage() {
  6. mkusage "$@" "[options] FILE" \
  7. -- \
  8. "Open Markdown file FILE in vimb (no junk)." \
  9. -o \
  10. "-C DIR Use DIR as config directory root. Default is" \
  11. " \$HOME/.config/mdvimb." \
  12. "-S STYLE Add CSS style STYLE to the final HTML (this"\
  13. " implies -w). If STYLE contains slash, it will" \
  14. " be interpreted as path, otherwise a file called"\
  15. " STYLE.css is looked up in 'css' directory of" \
  16. " configuration directory. Default is to not add"\
  17. " style." \
  18. "-c CONV_BIN Use converter CONV_BIN, which must be" \
  19. " a program that when called without arguments," \
  20. " accepts Markdown on standard input and prints" \
  21. " HTML to standard output. Default is 'Markdown'."\
  22. "-d Enable debugging output." \
  23. "-n Don't open, just print HTML." \
  24. "-w Wrap the generated HTML with basic HTML5 tags" \
  25. " such as <DOCTYPE>, <html/>, <head/> and" \
  26. " <body/>. Default is to leave the HTML as is." \
  27. "-v Enable verbose output." \
  28. "--help Print this help text and exit." \
  29. -- \
  30. "Default values of options -C, -S and -c can be changed"\
  31. "by setting environment variables MDVIMB__CONFIG_HOME," \
  32. "MDVIMB__STYLE and MDVIMB__CONVERTER, respectively."
  33. }
  34. MDVIMB__CONVERTER=${MDVIMB__CONVERTER:-Markdown}
  35. MDVIMB__CONFIG_HOME=${MDVIMB__CONFIG_HOME:-$HOME/.config/mdvimb}
  36. MDVIMB__STYLE=${MDVIMB__STYLE:-}
  37. browseopen() {
  38. #
  39. # Open stdin in vimb
  40. #
  41. $NoOpen && { cat; return 0; }
  42. think "opening vimb"
  43. vimb - "$@"
  44. }
  45. mkwrap() {
  46. #
  47. # Create wrapper part $1 if needed
  48. #
  49. local part=$1
  50. case $part in
  51. link)
  52. mkcssref >/dev/null || return 0
  53. think "adding CSS style: $Style"
  54. echo ' <style type="text/css" media="screen">'
  55. cat "$(mkcssref)"
  56. echo ' </style>'
  57. ;;
  58. title)
  59. case $MdFile in
  60. "") echo ' <title>mdvimb:(stdin)</title>' ;;
  61. *) echo " <title>mdvimb:$MdFile</title>" ;;
  62. esac
  63. ;;
  64. head)
  65. echo '<!DOCTYPE html>'
  66. echo '<html>'
  67. echo ' <head>'
  68. mkwrap title
  69. mkwrap link
  70. echo ' </head>'
  71. echo ' <body>'
  72. ;;
  73. tail)
  74. echo ' </body>'
  75. echo '</html>'
  76. ;;
  77. esac
  78. }
  79. may_wrap() {
  80. #
  81. # Take HTML on stdin; wrap if $Wrap
  82. #
  83. $Wrap || { cat; return 0; }
  84. think "adding HTML5 tags"
  85. mkwrap head
  86. cat
  87. mkwrap tail
  88. }
  89. mkcssref() {
  90. #
  91. # Print CSS reference
  92. #
  93. local cssfile=""
  94. case $Style in
  95. "") return 0 ;;
  96. */*) cssfile=$Style ;;
  97. *) cssfile="$ConfigHome/css/$Style.css" ;;
  98. esac
  99. test -f "$cssfile" || {
  100. warn "CSS file not found, ignoring: $cssfile"
  101. return 3
  102. }
  103. debug -v cssfile
  104. echo "$cssfile"
  105. }
  106. main() {
  107. local MdFile
  108. local converter=$MDVIMB__CONVERTER
  109. local ConfigHome=$MDVIMB__CONFIG_HOME
  110. local converterpath
  111. local NoOpen=false
  112. local Wrap=false
  113. local Style=$MDVIMB__STYLE
  114. #shellcheck disable=SC2034
  115. while true; do case $1 in
  116. -C) ConfigHome=$2; shift 2 ;;
  117. -S) Style=$2; shift 2 ;;
  118. -c) converter=$2; shift 2 ;;
  119. -d) PRETTY_DEBUG=true; shift ;;
  120. -n) NoOpen=true; shift ;;
  121. -v) PRETTY_VERBOSE=true; shift ;;
  122. -w) Wrap=true; shift ;;
  123. --help) usage -k ;;
  124. -*) usage -w "unknown argument: $1" ;;
  125. *) break ;;
  126. esac done
  127. test -n "$Style" && Wrap=true
  128. MdFile=$1; shift
  129. debug -v MDVIMB__CONVERTER MDVIMB__CONFIG_HOME MDVIMB__STYLE
  130. debug -v MdFile converter NoOpen Wrap Style ConfigHome
  131. converterpath=$(which "$converter") \
  132. || die "converter not available: $converter"
  133. debug -v converterpath
  134. think "using converter: $converter"
  135. case $MdFile in
  136. -|"") think "reading standard input" ;;
  137. *) think "reading file: $MdFile" ;;
  138. esac
  139. case $MdFile in
  140. "") $converterpath | may_wrap | browseopen "$@" ;;
  141. -) $converterpath | may_wrap | browseopen "$@" ;;
  142. *) <"$MdFile" $converterpath | may_wrap | browseopen "$@"
  143. esac
  144. }
  145. main "$@"