about summary refs log tree commit diff stats
path: root/subtext.awk
blob: 07dfb76916b0bc81eabb7e2ec3a5f59cb0c2e630 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# 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 : "."
	sofile = sofile ? sofile : ""
	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
}

### AWK layer

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

/^%/ { next } # comments

### Shell layer

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

### Text layer

end[endn] && $0 == end[endn] {
	par = par "\n" end[endn--] "\n)"
	next
}

/^\.\./ { # block
	if (match($0, /[ 	]*<<[ 	]*/))
		end[++endn] = substr($0, RSTART+RLENGTH)
	else
		end[++endn] = ".."
	$0 = substr($0, 3, RSTART?RSTART-3:length)
	par = par (par?"\n":"") \
		"$$(" ($1 ? $1 " " shquote(2) : "") " << " end[endn]
	# par = par (par?"\n":"") \
	# 	"$$(unquote + << .." \
	# 	(length>2 ? " | " substr($1,3) " " shquote(2) : "") \
	# 	slurp("..") \
	# 	"..\n)"
	next
}

/^\.$/ { next } # continuation line

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

/^\\/ { # special line escape
	$0 = substr($0, 2)
}

/^$/ { # blank line
	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
	print "echo()(printf '%s\\n' \"$@\")"
	print "handle_input()("\
		"test -n \"$1\" && printf '%s' \"$*\";"\
		"if read -r first_line;"\
		"then echo \"$first_line\";cat;return 0;"\
		"else echo;return 1;fi)"
	printf "ST_SOPATH=%s\n", sopath
	if (sofile) source(sofile)
	print "### head"
	print head
	print "### body"
	print "body(){ unquote : << \\_ | shexpand"
	pushpar()
	shfq = shellfix " -e 's/^/:/'"
	print body | shfq
	close(shfq)
	print "_"
	print "}"
	if (!dryrun) print "body"
}

function shquote(begin, end,	quoted, out) {
	if (!begin) begin = 1
	if (!end) end = NF
	for (i=begin; i<=NF; i++) {
		if ($i ~ /^\$/) {
			quoted = 1
			out = out (out?" ":"") "\"" $i
			continue
		}
		if (($i ~ /\)$/) && quoted) {
			quoted = 0
			out = out (out?" ":"") $i "\""
			continue
		}
		if (!quoted) {
			gsub(/"/, "\\\"", $i)
			out = out (out?" ":"") "\"" $i "\""
		} else 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, /^[ \t\n]*[<$]/)) {
		par = "$$(p<<../p\n" par "\n../p\n)"
	}
	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
		}
		close(fn)
		if (found) break
	}
	if (!found) {
		printf "Couldn't source %s; looked in:\n\t%s", name, sp
		dead = 127
		exit
	}
}