about summary refs log tree commit diff stats
path: root/html.st.sh
blob: 5f580b38e40f4b0bb100be131f8bdf94b0cf2a57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- sh -*-
html_fix() {
	sed -E \
	    -e 's#([^\\]|^)&#\1\&#g' \
	    -e 's#([^\\]|^)<#\1\&lt;#g' \
	    -e 's#([^\\]|^)>#\1\&gt;#g' \
	    -e 's#\\([&<>])#\1#g'
}

html_el(){ # el TAG [ATTRS...] [TEXT...] [< INPUT]
	tag="$1"; attrs= ; text=
	shift
	for arg
	do
		case "$arg" in
			(*=*) attrs="$attrs ${arg%%=*}=${arg#*=}"; shift ;;
			(*) break ;;
		esac
	done

	printf '<%s%s>%s' "$tag" "$attrs"
	handle_input "$@" |
		sed -E \
		    -e '$s#[ 	]([.!-;:,.?])$#</'"$tag"'>\1#' -e t \
		    -e '$s,$,</'"$tag"'>,'
}

for el in h1 h2 h3 p a blockquote ul ol li em strong b i dl dt dd
do
	alias $el="html_el $el"
done

alias @=html_el

code() {
	in="$(handle_input "$@")"
	if test $? -ne 0
	then html_el code "$@" # runs thru input twice, meh
	else echo "<pre><code>$in</code></pre>"
	fi
}

nl()(echo)