aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2022-10-09 00:07:07 +0200
committerPřemysl Eric Janouch <p@janouch.name>2022-10-09 01:01:08 +0200
commit0e86ffe7c30a4d52eea35856b792567ca1040f56 (patch)
tree38c4560658acc0d50dbc6990443a321be9e6e4e0
parentcbeb4e31336fc6562ef4cfa782051b610d8e2198 (diff)
downloadliberty-0e86ffe7c30a4d52eea35856b792567ca1040f56.tar.gz
liberty-0e86ffe7c30a4d52eea35856b792567ca1040f56.tar.xz
liberty-0e86ffe7c30a4d52eea35856b792567ca1040f56.zip
asciiman: fixes, improve mandoc compatibility
-rw-r--r--README.adoc2
-rw-r--r--tools/asciiman.awk46
2 files changed, 36 insertions, 12 deletions
diff --git a/README.adoc b/README.adoc
index 69b036a..3c500dc 100644
--- a/README.adoc
+++ b/README.adoc
@@ -25,7 +25,7 @@ This project also hosts a number of supporting scripts written in portable AWK:
asciiman.awk::
A fallback manual page generator for AsciiDoc documents,
motivated by the hugeness of AsciiDoc's and Asciidoctor's dependency trees.
- It uses the _man_ macro package.
+ Just like them, it uses the _man_ macro package.
cmake-parser.awk::
Parses the CMake language to the extent that is necessary to reliably
diff --git a/tools/asciiman.awk b/tools/asciiman.awk
index c2dbe5f..b46c7a0 100644
--- a/tools/asciiman.awk
+++ b/tools/asciiman.awk
@@ -1,4 +1,4 @@
-# asciiman.awk: stupid AsciiDoc to manual page converter
+# asciiman.awk: simplified AsciiDoc to manual page converter
#
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
# SPDX-License-Identifier: 0BSD
@@ -11,7 +11,7 @@
# - 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.
+# 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) {
@@ -20,6 +20,10 @@ function fatal(message) {
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]
@@ -27,13 +31,15 @@ function getattribute(name) {
}
function expand(s, attr, v) {
- # TODO: This should not expand unknown attribute names.
while (match(s, /[{][^{}]*[}]/)) {
- s = substr(s, 1, RSTART - 1) \
- getattribute(substr(s, RSTART + 1, RLENGTH - 2)) \
- substr(s, RSTART + RLENGTH)
+ 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 s
+ return v s
}
function escape(s) {
@@ -109,12 +115,20 @@ function format(line, v) {
return v
}
+function flushspace() {
+ if (NeedSpace) {
+ print ".sp"
+ NeedSpace = 0
+ }
+}
+
function inline(line) {
if (!line) {
- print ".sp"
+ NeedSpace = 1
return
}
+ flushspace()
line = format(escape(expand(line)))
# Strip empty URL descriptions, otherwise useful for demarking the end.
@@ -145,15 +159,21 @@ function process(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"
@@ -172,12 +192,14 @@ function process(firstline) {
return 0
}
if (match(firstline, /^\/\//)) {
- print ".\\\" " 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 \\{\\"
@@ -198,10 +220,12 @@ function process(firstline) {
break
}
print ".RE"
- print ".sp"
+ NeedSpace = 1
return !!$0
}
if (match(firstline, /^[[:space:]]+/)) {
+ flushspace()
+
print ".if n .RS 4"
print ".nf"
print ".fam C"
@@ -233,7 +257,7 @@ function process(firstline) {
break
}
print ".RE"
- print ".sp"
+ NeedSpace = 1
return !!$0
}
inline(firstline)