2022-10-09 00:07:07 +02:00
|
|
|
# asciiman.awk: simplified AsciiDoc to manual page converter
|
2022-09-25 20:07:10 +02:00
|
|
|
#
|
2024-02-12 10:57:23 +01:00
|
|
|
# Copyright (c) 2022 - 2024, Přemysl Eric Janouch <p@janouch.name>
|
2022-09-25 20:07:10 +02:00
|
|
|
# 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.
|
|
|
|
#
|
2022-10-09 00:07:07 +02:00
|
|
|
# Also beware that the output has only been tested with GNU troff and mandoc.
|
2022-09-30 18:13:01 +02:00
|
|
|
# Attributes can be passed via environment variables starting with "asciidoc-".
|
2022-09-25 20:07:10 +02:00
|
|
|
|
|
|
|
function fatal(message) {
|
|
|
|
print ".\\\" " FILENAME ":" FNR ": fatal error: " message
|
|
|
|
print FILENAME ":" FNR ": fatal error: " message > "/dev/stderr"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2022-10-09 18:43:37 +02:00
|
|
|
BEGIN {
|
|
|
|
for (name in ENVIRON)
|
|
|
|
if (match(name, /^asciidoc-/))
|
|
|
|
Attrs[substr(name, RSTART + RLENGTH)] = ENVIRON[name]
|
2022-10-09 00:07:07 +02:00
|
|
|
}
|
|
|
|
|
2022-10-09 18:43:37 +02:00
|
|
|
function expand(s, attrname, v) {
|
|
|
|
while (match(s, /[{][^{}]+[}]/)) {
|
|
|
|
attrname = substr(s, RSTART + 1, RLENGTH - 2)
|
|
|
|
if (attrname in Attrs)
|
|
|
|
v = v substr(s, 1, RSTART - 1) Attrs[attrname]
|
2022-10-09 00:07:07 +02:00
|
|
|
else
|
|
|
|
v = v substr(s, 1, RSTART + RLENGTH - 1)
|
|
|
|
s = substr(s, RSTART + RLENGTH)
|
2022-09-25 20:07:10 +02:00
|
|
|
}
|
2022-10-09 00:07:07 +02:00
|
|
|
return v s
|
2022-09-25 20:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function escape(s) {
|
|
|
|
gsub(/\\/, "\\\\", s)
|
|
|
|
gsub(/-/, "\\-", s)
|
2022-09-25 20:55:51 +02:00
|
|
|
sub(/^[.']/, "\\\\\\&&", s)
|
2022-09-25 20:07:10 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-10-09 18:43:37 +02:00
|
|
|
function readattribute(line, attrname) {
|
|
|
|
if (match(line, /^:[^:]+:$/)) {
|
|
|
|
Attrs[substr(line, RSTART + 1, RLENGTH - 2)] = ""
|
|
|
|
} else if (match(line, /^:[^:]+!:$/)) {
|
|
|
|
delete Attrs[substr(line, RSTART + 1, RLENGTH - 3)]
|
|
|
|
} else if (match(line, /^:![^:]+:$/)) {
|
|
|
|
delete Attrs[substr(line, RSTART + 2, RLENGTH - 3)]
|
|
|
|
} else if (match(line, /^:[^:]+: /)) {
|
2022-09-25 20:07:10 +02:00
|
|
|
attrname = substr(line, RSTART + 1, RLENGTH - 3)
|
2022-10-09 18:43:37 +02:00
|
|
|
Attrs[attrname] = expand(substr(line, RSTART + RLENGTH))
|
|
|
|
} else {
|
|
|
|
return 0
|
2022-09-25 20:07:10 +02:00
|
|
|
}
|
2022-10-09 18:43:37 +02:00
|
|
|
return 1
|
2022-09-25 20:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-09-25 21:11:45 +02:00
|
|
|
print "'\\\" t"
|
2022-09-30 18:13:01 +02:00
|
|
|
printf ".TH \"%s\" \"%s\" \"\" \"%s\"",
|
2022-10-09 18:43:37 +02:00
|
|
|
toupper(name), section, Attrs["mansource"]
|
|
|
|
if ("manmanual" in Attrs)
|
|
|
|
printf " \"%s\"", Attrs["manmanual"]
|
2022-09-30 18:13:01 +02:00
|
|
|
print ""
|
2022-09-25 20:07:10 +02:00
|
|
|
|
|
|
|
# Hyphenation is indeed rather annoying, in particular with long links.
|
|
|
|
print ".nh"
|
|
|
|
}
|
|
|
|
|
2023-06-10 15:06:19 +02:00
|
|
|
function readattrlist(line, posattrs, namedattrs, name, value, n) {
|
|
|
|
if (!match(line, /^\[.*\]$/))
|
|
|
|
return 0
|
|
|
|
|
|
|
|
line = expand(substr(line, RSTART + 1, RLENGTH - 2))
|
|
|
|
while (line) {
|
|
|
|
name = ""
|
|
|
|
if (match(line, /^[[:alnum:]][[:alnum:]-]*/)) {
|
|
|
|
value = substr(line, RSTART, RLENGTH)
|
|
|
|
if (match(substr(line, RSTART + RLENGTH),
|
|
|
|
/^[[:space:]]*=[[:space:]]*/)) {
|
|
|
|
name = value
|
|
|
|
line = substr(line, 1 + length(name) + RLENGTH)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# The quoting syntax actually is awful like this.
|
|
|
|
if (match(line, /^"(\\.|[^"\\])*"/)) {
|
|
|
|
value = substr(line, RSTART + 1, RLENGTH - 2)
|
|
|
|
gsub(/\\"/, "\"", value)
|
|
|
|
} else if (match(line, /^'(\\.|[^'\\])*'/)) {
|
|
|
|
value = substr(line, RSTART + 1, RLENGTH - 2)
|
|
|
|
gsub(/\\'/, "'", value)
|
|
|
|
} else {
|
|
|
|
match(line, /^[^,]*/)
|
|
|
|
value = substr(line, RSTART, RLENGTH)
|
|
|
|
sub(/[[:space:]]*$/, "", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
line = substr(line, RSTART + RLENGTH)
|
|
|
|
sub(/^[[:space:]]*,[[:space:]]*/, "", line)
|
|
|
|
if (!name)
|
|
|
|
posattrs[++n] = value
|
|
|
|
else if (value == "None")
|
|
|
|
delete namedattrs[name]
|
|
|
|
else
|
|
|
|
namedattrs[name] = value
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-09-30 15:01:14 +02:00
|
|
|
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"
|
2024-02-12 10:57:23 +01:00
|
|
|
} else if (match(line, /^`[^`]+`/) &&
|
|
|
|
substr(line, RSTART + RLENGTH) !~ /^[[:alnum:]]/) {
|
|
|
|
# Manual pages are usually already rendered in monospace;
|
|
|
|
# follow others, and render this in boldface.
|
|
|
|
v = v "\\fB" substr(line, RSTART + 1, RLENGTH - 2) "\\fP"
|
2022-09-30 15:01:14 +02:00
|
|
|
} else {
|
|
|
|
v = v substr(line, 1, 1)
|
|
|
|
line = substr(line, 2)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
line = substr(line, RSTART + RLENGTH)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-10-09 00:07:07 +02:00
|
|
|
function flushspace() {
|
|
|
|
if (NeedSpace) {
|
|
|
|
print ".sp"
|
|
|
|
NeedSpace = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 20:07:10 +02:00
|
|
|
function inline(line) {
|
|
|
|
if (!line) {
|
2022-10-09 00:07:07 +02:00
|
|
|
NeedSpace = 1
|
2022-09-25 20:07:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-09 00:07:07 +02:00
|
|
|
flushspace()
|
2022-09-30 15:01:14 +02:00
|
|
|
line = format(escape(expand(line)))
|
2022-09-25 20:07:10 +02:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
}
|
|
|
|
|
2022-09-25 20:55:51 +02:00
|
|
|
# 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)
|
|
|
|
|
2022-09-25 20:07:10 +02:00
|
|
|
sub(/[[:space:]]+[+]$/, "\n.br", line)
|
|
|
|
print line
|
|
|
|
}
|
|
|
|
|
|
|
|
# Returns 1 iff the left-over $0 should be processed further.
|
2023-06-10 15:06:19 +02:00
|
|
|
function process(firstline, posattrs, namedattrs) {
|
2022-09-25 20:07:10 +02:00
|
|
|
if (readattribute(firstline))
|
|
|
|
return 0
|
|
|
|
if (getline <= 0) {
|
|
|
|
inline(firstline)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-06-10 15:06:19 +02:00
|
|
|
# Block attribute list lines.
|
|
|
|
delete posattrs[0]
|
|
|
|
delete namedattrs[0]
|
|
|
|
while (readattrlist(firstline, posattrs, namedattrs)) {
|
|
|
|
firstline = $0
|
|
|
|
if (getline <= 0) {
|
|
|
|
inline(firstline)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 00:07:07 +02:00
|
|
|
# mandoc(1) automatically precedes section headers with blank lines.
|
2022-09-25 20:07:10 +02:00
|
|
|
if (length(firstline) == length($0) && /^-+$/) {
|
|
|
|
print ".SH \"" escape(toupper(expand(firstline))) "\""
|
2022-10-09 00:07:07 +02:00
|
|
|
NeedSpace = 0
|
2022-09-25 20:07:10 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if (length(firstline) == length($0) && /^~+$/) {
|
|
|
|
print ".SS \"" escape(expand(firstline)) "\""
|
2022-10-09 00:07:07 +02:00
|
|
|
NeedSpace = 0
|
2022-09-25 20:07:10 +02:00
|
|
|
return 0
|
|
|
|
}
|
2022-10-09 00:07:07 +02:00
|
|
|
|
2024-02-12 10:57:23 +01:00
|
|
|
if (firstline ~ /^--$/) {
|
|
|
|
flushspace()
|
|
|
|
|
|
|
|
# For now, recognize, but do not process open block delimiters.
|
|
|
|
InOpenBlock = !InOpenBlock
|
|
|
|
return 1
|
|
|
|
}
|
2022-09-25 20:07:10 +02:00
|
|
|
if (firstline ~ /^(-{4,}|[.]{4,})$/) {
|
2022-10-09 00:07:07 +02:00
|
|
|
flushspace()
|
|
|
|
|
2022-09-25 20:07:10 +02:00
|
|
|
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, /^\/\//)) {
|
2022-10-09 00:07:07 +02:00
|
|
|
print ".\\\"" substr(firstline, RSTART + RLENGTH)
|
2022-09-25 20:07:10 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-06-10 15:06:19 +02:00
|
|
|
# We generally assume these blocks end with a blank line.
|
2022-09-25 20:07:10 +02:00
|
|
|
if (match(firstline, /^[[:space:]]*[*][[:space:]]+/)) {
|
2022-10-09 00:07:07 +02:00
|
|
|
flushspace()
|
|
|
|
|
2022-09-25 20:07:10 +02:00
|
|
|
# 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"
|
2022-10-09 00:07:07 +02:00
|
|
|
NeedSpace = 1
|
2022-09-25 20:07:10 +02:00
|
|
|
return !!$0
|
|
|
|
}
|
|
|
|
if (match(firstline, /^[[:space:]]+/)) {
|
2022-10-09 00:07:07 +02:00
|
|
|
flushspace()
|
|
|
|
|
2022-09-25 20:07:10 +02:00
|
|
|
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"
|
2022-10-09 00:07:07 +02:00
|
|
|
NeedSpace = 1
|
2022-09-25 20:07:10 +02:00
|
|
|
return !!$0
|
|
|
|
}
|
|
|
|
inline(firstline)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
while (process($0)) {}
|
|
|
|
}
|