about summary refs log tree commit diff stats
path: root/subtext.awk
blob: ee04238aac955abded0e4e5dac29ae6e3943d425 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# subtext -- text substitutor -*- awk -*-
# (C) C Duckworth <acdw@acdw.net>

BEGIN {
	true = 1; false = 0
	## Tuneables
	runbody = bool(runbody ? runbody : true)
	postproc = postproc ? postproc : "shexpand"
	bodyfunc = bodyfunc ? bodyfunc : "body"
	sopath = get_value(sopath, ".:"ENVIRON["HOME"]"/.subtext")
	## Globals
	## Wrap the text in a function
	pretext = "### begin text\n"bodyfunc"(){\n"
	posttext = "}\n### end text"
	## Prelude function strings
	# 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'"
	shxwrap = " -e 's/^/:/'"
	htmlfix = "sed -E" \
		" -e 's#([^\\\\]|^)&#\\1\\&amp;#g'" \
		" -e 's#([^\\\\]|^)<#\\1\\&lt;#g'" \
		" -e 's#([^\\\\]|^)>#\\1\\&gt;#g'" \
		" -e 's#\\\\([&<>])#\\1#g'";
	## Prelude
	par = "#!/bin/sh\n### kernel\n" \
		"preface()(sed \"s/^/$1/\")\n" \
		"unpreface()(sed \"s/^$1//\")\n" \
		"shexpand()(eval \"$( (echo 'cat<<.';preface +;echo .)" \
		" | unpreface + )\")\n" \
		"### library\n" \
		"shellfix()(" shellfix ")\n" \
		"htmlfix()(" htmlfix ")\n" \
		"### variables\n" \
		"ST_POSTPROC=" postproc "\n" \
		"ST_BODYFUNC=" bodyfunc "\n" \
		"ST_SOPATH=" sopath "\n" \
		"### header\n"
}

### End a block

end[endn] && $0 == end[endn] {
	pushpar(end[endn--] "\n)", true)
	printpar()
	subdocp = false
	next
}

### Line continuation

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

### Special commands
## These call subtext-internal functions

/^#so/ {			# Insert $2 verbatim (if in sopath), else error
	pushpar($0)
	source($2)
	next
}

/^###$/ {			# Delimit document
	printpar()
	docp = !docp
	if (docp) {
		print pretext "unpreface ':'<<'_'|eval \"$ST_POSTPROC\""
	} else {
		end_text()
	}
	next
}

### Escape sequences

/^\.\./ && docp {		# Begin a heredoc
	# ..[<command>] [<options>] [<<delim]
	## ends with DELIM or '..'
	subdocp = true
	end[++endn] = (match($0, "<<") ? substr($0, RSTART + RLENGTH) : "..")
	command = substr($0, 3, RSTART ? RSTART - 2 : length($0))
	$0 = "$$(" (command ? command : "cat") "<<" end[endn]
}

/^\./ && docp {			# One-line command
	# .<command> [<parameters>]
	## wraps the line in $( ... ), basically (also quotes)
	specialp = 2
	gsub(/"/, "\\\\&", $0)
	ln = "$$(" substr($1, 2)
	for (f=2; f<=NF; f++) {
		ln = ln " \"" $f "\""
	}
	ln = ln ")"
	$0 = ln
}

/^\\/ && docp { # \ at the beginning of a line escapes the next character
	$0 = substr($0, 2)
}

### Book-keeping

/^$/ {
	if (!par)
		next
	printpar()
}

{
	pushpar($0)
	if (--specialp < 0)
		specialp = 0
}

END {
	if (dead)
		exit dead
	if (par)
		printpar()
	while (endn > 0)
		print "\n" end[endn--] "\n)"
	if (docp)
		end_text()
	if (runbody)
		print bodyfunc
}

function end_text() {
	print "_\n" posttext
}

function pushpar(text, force_newline) {
	par = par ((par || force_newline) ? "\n" : "") text
}

function printpar() {
	specialp = specialp || (match(par, /^[ 	]*</))
	if (docp) {
		if (!subdocp && !specialp)
			par = "<p>" par "</p>"
		shx = shellfix shxwrap
		print par | shx
		close(shx)
	} else
		print par
	par = ""
}

function get_value(var, env_var, default) {
	if (var)
		return var
	else
		return default
}

function bool(var, default) {
	if (var=="false" || var=="no" || var=="0")
		return false
	else if (var=="true" || var=="yes" || var=="1")
		return true
	else
		return var || default
}

function source(name) {
	found = false
	sp = ""
	split(sopath, asopath, ":")
	for (dir in asopath) {
		fn = asopath[dir] "/" name
		sp = asopath[dir] "\n\t" sp
		while ((getline ln < fn) > 0) {
			found = true
			pushpar(ln)
		}
		if (found)
			break
	}
	if (!found)
		die("Couldn't source " name "; looked in:\n\t" sp, 9)
}

function die(message, code) {
	dead = code
	print "!!" FILENAME ":" NR ": " message > "/dev/stderr"
	exit
}