aboutsummaryrefslogtreecommitdiff
path: root/tools/asciiman.awk
blob: b46c7a05d6d05bbe01c6445ff19802876591202d (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# asciiman.awk: simplified AsciiDoc to manual page converter
#
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
# SPDX-License-Identifier: 0BSD
#
# This is not intended to produce great output, merely useful output.
# As such, input documents should restrict themselves as follows:
#
#  - In-line formatting sequences must not overlap,
#    cannot be escaped, and cannot span lines.
#  - Heading underlines must match in byte length exactly.
#  - Only a small subset of syntax is supported overall.
#
# Also beware that the output has only been tested with GNU troff and mandoc.
# Attributes can be passed via environment variables starting with "asciidoc-".

function fatal(message) {
	print ".\\\" " FILENAME ":" FNR ": fatal error: " message
	print FILENAME ":" FNR ": fatal error: " message > "/dev/stderr"
	exit 1
}

function haveattribute(name) {
	return name in Attrs || ("asciidoc-" name) in ENVIRON
}

function getattribute(name) {
	if (!(name in Attrs) && ("asciidoc-" name) in ENVIRON)
		Attrs[name] = ENVIRON["asciidoc-" name]
	return Attrs[name]
}

function expand(s,   attr, v) {
	while (match(s, /[{][^{}]*[}]/)) {
		attr = substr(s, RSTART + 1, RLENGTH - 2)
		if (haveattribute(attr))
			v = v substr(s, 1, RSTART - 1) getattribute(attr)
		else
			v = v substr(s, 1, RSTART + RLENGTH - 1)
		s = substr(s, RSTART + RLENGTH)
	}
	return v s
}

function escape(s) {
	gsub(/\\/, "\\\\", s)
	gsub(/-/, "\\-", s)
	sub(/^[.']/, "\\\\\\&&", s)
	return s
}

function readattribute(line,    attrname, attrvalue) {
	if (match(line, /^:[^:]*: /)) {
		attrname = substr(line, RSTART + 1, RLENGTH - 3)
		attrvalue = substr(line, RSTART + RLENGTH)
		Attrs[attrname] = expand(attrvalue)
		return 1
	}
}

NR == 1 {
	nameline = $0
	if (match(nameline, /[(][[:digit:]][)]$/)) {
		name = substr(nameline, 1, RSTART - 1)
		section = substr(nameline, RSTART + 1, RLENGTH - 2)
	} else {
		fatal("invalid header line")
	}

	getline
	if (length(nameline) != length($0) || /[^=]/)
		fatal("invalid header underline")

	getline
	while (readattribute($0))
		getline
	if ($0)
		fatal("expected an empty line after the header")

	# Requesting tbl(1), even though we currently do not support tables.
	print "'\\\" t"
	printf ".TH \"%s\" \"%s\" \"\" \"%s\"",
		toupper(name), section, getattribute("mansource")
	if (getattribute("manmanual"))
		printf " \"%s\"", getattribute("manmanual")
	print ""

	# Hyphenation is indeed rather annoying, in particular with long links.
	print ".nh"
}

function format(line,    v) {
	# Pass-through, otherwise useful for hacks, is a bit of a lie here,
	# and formatting doesn't fully respect word boundaries.
	while (line) {
		if (match(line, /^[+][+][+][^+]+[+][+][+]/)) {
			v = v substr(line, RSTART + 3, RLENGTH - 6)
		} else if (match(line, /^__[^_]+__/)) {
			v = v "\\fI" substr(line, RSTART + 2, RLENGTH - 4) "\\fP"
		} else if (match(line, /^[*][*][^*]+[*][*]/)) {
			v = v "\\fB" substr(line, RSTART + 2, RLENGTH - 4) "\\fP"
		} else if (match(line, /^_[^_]+_/) &&
			substr(line, RSTART + RLENGTH) !~ /^[[:alnum:]]/) {
			v = v "\\fI" substr(line, RSTART + 1, RLENGTH - 2) "\\fP"
		} else if (match(line, /^[*][^*]+[*]/) &&
			substr(line, RSTART + RLENGTH) !~ /^[[:alnum:]]/) {
			v = v "\\fB" substr(line, RSTART + 1, RLENGTH - 2) "\\fP"
		} else {
			v = v substr(line, 1, 1)
			line = substr(line, 2)
			continue
		}
		line = substr(line, RSTART + RLENGTH)
	}
	return v
}

function flushspace() {
	if (NeedSpace) {
		print ".sp"
		NeedSpace = 0
	}
}

function inline(line) {
	if (!line) {
		NeedSpace = 1
		return
	}

	flushspace()
	line = format(escape(expand(line)))

	# Strip empty URL descriptions, otherwise useful for demarking the end.
	while (match(line, /[^[:space:]]+\[\]/)) {
		line = substr(line, 1, RSTART + RLENGTH - 3) \
			 substr(line, RSTART + RLENGTH)
	}

	# Enable double-spacing after the end of a sentence.
	gsub(/[.][[:space:]]+/, ".\n", line)
	gsub(/[!][[:space:]]+/, "!\n", line)
	gsub(/[?][[:space:]]+/, "?\n", line)

	# Quote commands resulting from that, as well as from expand().
	gsub(/\n[.]/, "\n\\\\\\&.", line)
	gsub(/\n[']/, "\n\\\\\\&'", line)

	sub(/[[:space:]]+[+]$/, "\n.br", line)
	print line
}

# Returns 1 iff the left-over $0 should be processed further.
function process(firstline) {
	if (readattribute(firstline))
		return 0
	if (getline <= 0) {
		inline(firstline)
		return 0
	}

	# mandoc(1) automatically precedes section headers with blank lines.
	if (length(firstline) == length($0) && /^-+$/) {
		print ".SH \"" escape(toupper(expand(firstline))) "\""
		NeedSpace = 0
		return 0
	}
	if (length(firstline) == length($0) && /^~+$/) {
		print ".SS \"" escape(expand(firstline)) "\""
		NeedSpace = 0
		return 0
	}

	if (firstline ~ /^(-{4,}|[.]{4,})$/) {
		flushspace()

		print ".if n .RS 4"
		print ".nf"
		print ".fam C"
		do {
			print escape($0)
		} while (getline > 0 && $0 != firstline)
		print ".fam"
		print ".fi"
		print ".if n .RE"
		return 0
	}
	if (firstline ~ /^\/{4,}$/) {
		do {
			print ".\\\" " $0
		} while (getline > 0 && $0 != firstline)
		return 0
	}
	if (match(firstline, /^\/\//)) {
		print ".\\\"" substr(firstline, RSTART + RLENGTH)
		return 1
	}

	# We generally assume these block end with a blank line.
	if (match(firstline, /^[[:space:]]*[*][[:space:]]+/)) {
		flushspace()

		# Bullet magic copied over from AsciiDoc/Asciidoctor generators.
		print ".RS 4"
		print ".ie n \\{\\"
		print "\\h'-04'\\(bu\\h'+03'\\c"
		print ".\\}"
		print ".el \\{\\"
		print ".sp -1"
		print ".IP \\(bu 2.3"
		print ".\\}"

		inline(substr(firstline, RSTART + RLENGTH))
		while ($0) {
			sub(/^[[:space:]]+/, "")
			sub(/^[+]$/, "")
			if (!process($0) && getline <= 0)
				fatal("unexpected EOF")
			if (match($0, /^[[:space:]]*[*][[:space:]]+/))
				break
		}
		print ".RE"
		NeedSpace = 1
		return !!$0
	}
	if (match(firstline, /^[[:space:]]+/)) {
		flushspace()

		print ".if n .RS 4"
		print ".nf"
		print ".fam C"
		do {
			print escape(substr(firstline, RLENGTH + 1))
			firstline = $0
		} while ($0 && getline > 0)
		print ".fam"
		print ".fi"
		print ".if n .RE"
		return 1
	}
	if (match(firstline, /::$/)) {
		inline(substr(firstline, 1, RSTART - 1))
		while (match($0, /::$/)) {
			print ".br"
			inline(substr($0, 1, RSTART - 1))
			if (getline <= 0)
				fatal("unexpected EOF")
		}

		print ".RS 4"
		while ($0) {
			sub(/^[[:space:]]+/, "")
			sub(/^[+]$/, "")
			if (!process($0) && getline <= 0)
				fatal("unexpected EOF")
			if (match($0, /::$/))
				break
		}
		print ".RE"
		NeedSpace = 1
		return !!$0
	}
	inline(firstline)
	return 1
}

{
	while (process($0)) {}
}