From bd1013f16a40be5458c0a8cd95625e28309295f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Sat, 10 Jun 2023 15:06:19 +0200 Subject: [PATCH] Parse block attribute list lines This code is of strategic importance, but its output is so far unused. --- tools/asciiman.awk | 58 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/tools/asciiman.awk b/tools/asciiman.awk index 4b5ee54..92b910d 100644 --- a/tools/asciiman.awk +++ b/tools/asciiman.awk @@ -1,6 +1,6 @@ # asciiman.awk: simplified AsciiDoc to manual page converter # -# Copyright (c) 2022, Přemysl Eric Janouch +# Copyright (c) 2022 - 2023, Přemysl Eric Janouch # SPDX-License-Identifier: 0BSD # # This is not intended to produce great output, merely useful output. @@ -92,6 +92,47 @@ NR == 1 { print ".nh" } +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 +} + 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. @@ -154,7 +195,7 @@ function inline(line) { } # Returns 1 iff the left-over $0 should be processed further. -function process(firstline) { +function process(firstline, posattrs, namedattrs) { if (readattribute(firstline)) return 0 if (getline <= 0) { @@ -162,6 +203,17 @@ function process(firstline) { return 0 } + # Block attribute list lines. + delete posattrs[0] + delete namedattrs[0] + while (readattrlist(firstline, posattrs, namedattrs)) { + firstline = $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))) "\"" @@ -199,7 +251,7 @@ function process(firstline) { return 1 } - # We generally assume these block end with a blank line. + # We generally assume these blocks end with a blank line. if (match(firstline, /^[[:space:]]*[*][[:space:]]+/)) { flushspace()