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