aboutsummaryrefslogtreecommitdiff
path: root/htwrap.sh
blob: 3d68946c5fff5a44cfcea6f36925a82764b56828 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh

# htwrap -- create standalone HTML document

usage() {
	echo "usage: $0 [-t] [-C] [-c charset] [-d dir] [-l lang] [-v doctype]"\
		1>&2
	exit 1
}

args=`getopt tCc:d:l:v: $*`
[ $? -ne 0 ] && usage
set -- $args

while [ $# -gt 0 ]; do
	case "$1" in
	-t)	flagt=$1 ;;
	-C)	flagC=$1 ;;
	-c)	flagc=$2; shift ;;
	-d)	flagd=$2; shift ;;
	-l)	flagl=$2; shift ;;
	-v)	flagv=$2; shift ;;
	--)	shift; break ;;
	esac
	shift
done

case "$flagv" in
5)	echo '<!DOCTYPE html>'
	;;
4)	echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">'
	;;
4s)	echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">'
	;;
x|xhtml)
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
	;;
xs|xhtmls)
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
	;;
'')
	echo '<!DOCTYPE html>'
	;;
*)
	echo error: unknown doctype "$flagv" 1>&2
	usage
	;;
esac

echo -n '<html'
[ ! -z "$flagl" ] && echo -n ' lang='"$flagl"'"'
[ ! -z "$flagd" ] && echo -n ' dir="'"$flagd"'"'
echo '>'

if [ -z "$flagC" ]; then
	if [ ! -z "$flagc" ]; then
		echo '<meta http-equiv="Content-Type" content="text/html; charset='"$flagc"'">'
	else
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
	fi
fi

source() {
	[ $# -gt 0 ] && cat "$@" || cat
}

if [ ! -z "$flagt" ]; then # try to retrieve title from <h1> on first line
	source "$@" | sed '1s/^<h1>\(.*\)<\/h1>/<title>\1<\/title>\
&/'
else
	source "$@"
fi

echo '</html>'