Show HN: Loop macro with user-definable clauses in C preprocessor, for Awk
kylheku.com
kylheku.com
The conversion of the special backspace character sequences to color is done by a TXR Lisp program manht.tl compiled to manht.tlo.
commit b3e2c86b6bfb05a103ddb83959ed6a99049e6022
Author: Kaz Kylheku <kaz@kylheku.com>
Date: Thu Apr 7 19:10:21 2022 -0700
cgit: format troff html files with man.
diff --git a/usr/lib/cgit/filters/manht.tl b/usr/lib/cgit/filters/manht.tl
new file mode 100755
index 0000000..ee5cff4
--- /dev/null
+++ b/usr/lib/cgit/filters/manht.tl
@@ -0,0 +1,24 @@
+#!/usr/bin/env txr
+
+(compile-only
+ (let ((cur-mode :norm)
+ (closer ""))
+ (flet ((output-text (str mode)
+ (when (neq mode cur-mode)
+ (put-string closer)
+ (caseq (set cur-mode mode)
+ (:bold (put-string "<span style='color: darkred'>")
+ (set closer "</span>"))
+ (:ital (put-string "<span style='color: green'>")
+ (set closer "</span>"))
+ (:norm (set closer ""))))
+ (put-string (html-encode str))))
+ (whilet ((line (get-line)))
+ (each ((tok (tok #/.\b./ t line)))
+ (match-case tok
+ ("")
+ (`@{x #/ +/ }` (output-text x cur-mode))
+ (`@x\b@x` (output-text x :bold))
+ (`_\b@x` (output-text x :ital))
+ (@else (output-text else :norm))))
+ (output-text "\n" :norm)))))
diff --git a/usr/lib/cgit/filters/manht.tlo b/usr/lib/cgit/filters/manht.tlo
new file mode 100755
index 0000000..6733ced
--- /dev/null
+++ b/usr/lib/cgit/filters/manht.tlo
@@ -0,0 +1,29 @@
+#!/usr/bin/env txr
+(7 0 nil)
+((14 48 #b'020002040004802c 0104812c01000304 2400008c000c0000 020002000a000000
+ 020003000c00003c 000803001f000034 0600012000000108 0300802c0300072c
+ 1600003c02040700 0800012000000304 0404812c1f000034 1c00003c05040700
+ 0900012000000604 0404812c1f000034 1f00003c00040700 0104812c04000120
+ 0100020005000120 0000040005000010 07042f2c9d00004c 000002009b000038
+ 2f0000002e000020 0200000099000038 2e0000002d000320 0300080407042e00
+ 2c00012004002d00 9500004c00000b00 0f00012005002c00 940000380f000000
+ 2d00012007002c00 00002b2c12000220 08002d0001040000 9100003c00001200
+ 2d001b2c12000120 09001b0052000038 1200000018000320 0a001b0009040704
+ 1800192c12000220 0b000a0419000000 5200003812000000 2b000218000c1900
+ 0008000091000034 2d00232c12000120 0900230079000038 120000000904222c
+ 1e0003200c002300 0b0422001e00212c 2100122c77000038 210000001d000220
+ 0d0021000c040000 1c0003200a002300 220021001c001f2c 180001200f002300
+ 1600022011001f00 0000000015000320 1000230016001d00 120002200e001800
+ 1500000079000038 120000002b000218 000c1f0002040000 0704122c9100003c
+ 000012002d00292c 1200012009002900 8e00003812000000 250002200d000904
+ 0d0400002500272c 1200032010002900 0e0409048e000038 1200000024000320
+ 0a00290027000704 2b000218000c2400 0504000091000034 2b000218000c2d00
+ 000400002c000120 06002c0034000034 0b00001008000218 000c0f0400040000
+ 2700003400002f2c 270000340000022c 0200001002000010 0200001002000010'
+ #(:norm "" :bold "<span style='color: darkred'>" "</span>" :ital
+ "<span style='color: green'>" usr:t #/.\b./ 0 #/ +/ "\b" 1 2
+ "_\b" "\n")
+ #(usr:put-string usr:html-encode usr:get-line usr:tok usr:iter-begin
+ usr:iter-more usr:iter-step usr:iter-item usr:equal usr:stringp
+ usr:sub-str usr:m^$ usr:search-str sys:b+ usr:eql usr:len usr:match-str
+ sys:fmt-simple)))
diff --git a/usr/lib/cgit/filters/syntax-highlighting.sh b/usr/lib/cgit/filters/syntax-highlighting.sh
index 52d454d..7d944d1 100755
--- a/usr/lib/cgit/filters/syntax-highlighting.sh
+++ b/usr/lib/cgit/filters/syntax-highlighting.sh
@@ -41,6 +41,9 @@ Makefile | makefile | MAKEFILE )
ChangeLog | CHANGELOG | changelog )
LANGUAGE=changelog
;;
+txr.1 )
+ LANGUAGE=none
+ ;;
*.[1-9] )
LANGUAGE=troff
;;
@@ -70,6 +73,9 @@ case $LANGUAGE in
fi
exit 0
;;
+ troff )
+ MANWIDTH=100 MAN_KEEP_FORMATTING=y man -l - | $SELFDIR/manht.tlo
+ ;;
* )
OTMPFILE=/tmp/file.$$.$LANGUAGE.html
if [ $LANGUAGE == mk ] ; then