about summary refs log tree commit diff stats
path: root/subtext.awk
blob: 09ec8824c49c7802bef72fd68aec4306ddafbb79 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# subtext -*- awk -*-
# (C) C Duckworth <acdw@acdw.net>
## Subtext is an awk program that converts roff-like input to a shell script
## that can output markup.  Lines prefixed with . are wrapped in $(...)
## constructs within the `body' function, but lines prefixed with # are
## transported to the top of the file, where they're executed within the shell
## environment.

BEGIN {
	## Tuneables
	dryrun = dryrun ? dryrun : 0
	sopath = sopath ? sopath : ".:" ENVIRON["HOME"] "/.subtext"
	shxend = shxend ? shxend : "%%end"
	## Shellfix: escape ` and $ in input
	# Ask sed to do these b/c awk has no capture groups ;_;
	shellfix = "sed -E" \
		" -e 's/`/\\\\`/g'" \
		" -e 's/(^|[^\\$])\\$([^\\$]|$)/\\1\\\\$\\2/g'" \
		" -e 's/(^|[^\\$])\\$(\\$+)([^\\$]|$)/\\1\\2\\3/g'"
}

/\\$/ { # line continuation
	getline nl
	sub(/\\$/, "")
	$0 = $0 " " nl
}

/^#so/ { # source a file
	pushpar()
	source($2)
	next
}

/^#/ { # head lines
	sub(/^#+[ 	]*/,"")
	head = head (head?"\n":"") $0
	next
}

/^\.\./ { # block
	body = body (body?"\n":"") \
		"$$(unquote + << .." \
		(length>2 ? " | " substr($1,3) " " shquote(2) : "") \
		slurp("..") \
		"..\n)"
	next
}

/^\./ { # line
	body = body (body?"\n":"") \
		"$$(" substr($1, 2) " " shquote(2) ")"
	next
}

/^$/ { # line break
	if (!pushpar()) next
}

{ # regular text
	par = par (par?"\n":"") $0
}

END {
	if (dead) exit dead

	print "#!/bin/sh"
	print "### generated with subtext"
	print "quote()(sed \"s/^/$1/\")"
	print "unquote()(sed \"s/^$1//\")"
	printf "shexpand()(eval \"$(echo 'cat<<%s';cat;echo '%s')\")\n", \
		shxend, shxend
	printf "shellfix()(%s)\n", shellfix
	printf "ST_SOPATH=%s\n", sopath
	print "### head"
	print head
	print "### body"
	print "body(){ unquote : << \\_ | shexpand"
	pushpar()
	print body | (shellfix " -e 's/^/:/'")
	close(shellfix " -e 's/^/:/'")
	print "_"
	print "}"
	if (!dryrun) print "body"
}

function shquote(begin, end,	out) {
	if (!begin) begin = 1
	if (!end) end = NF
	for (i=begin; i<=NF; i++) {
		gsub(/"/, "\\\"", $i)
		out = out (out?" ":"") "\"" $i "\""
	}
	return out
}

function slurp(to,	out, nl) {
	while (nl != to) {
		getline nl
		out = out "\n+" nl
	}
	sub("\\+"to"$", "", out)
	return out
}

function pushpar() {
	if (!par) return 0
	if (!match(par, /^[ 	]*</))
		par = "<p>" par "</p>"
	body = body (body?"\n":"") par
	par = ""
	return 1
}

function source(name,	found, sp) {
	split(sopath, asopath, ":")
	for (dir in asopath) {
		fn = asopath[dir] "/" name
		sp = asopath[dir] "\n\t" sp
		while ((getline ln < fn) > 0) {
			found = 1
			head = head (head?"\n":"") ln
		}
		if (found) break
	}
	if (!found) {
		printf "Couldn't source %s; looked in:\n\t%s", name, sp
		dead = 127
		exit
	}
}