From 171e7a112b85c103358bf9e45f60a35c71d298c3 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 31 Jul 2024 14:44:23 +0200 Subject: added Lua-based HIGHLIGHT macro and groff-ms.outlang for ms macros * the highlight.sno and highlight.lua macros are considerde deprecated in favor of pygments-groff.py. --- README.md | 23 ++++++++++++++---- groff-mom.outlang | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ groff-ms.outlang | 40 +++++++++++++++++++++++++++++++ groff.outlang | 72 ------------------------------------------------------- highlight.lua | 43 +++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 77 deletions(-) create mode 100644 groff-mom.outlang create mode 100644 groff-ms.outlang delete mode 100644 groff.outlang create mode 100755 highlight.lua diff --git a/README.md b/README.md index bfb27ec..73a9fe1 100644 --- a/README.md +++ b/README.md @@ -23,19 +23,20 @@ To build the sample `select-from.ebnf`, type something like: cat samples/select-from.ebnf | ./ebnf.sno | pic | groff -Tps >select-from.ps -## HIGHLIGHT +## HIGHLIGHT (SNOBOL4) `highlight.sno` is a small preprocessor written in [CSNOBOL4](http://www.snobol4.org/csnobol4/) that processes blocks of source code embedded in your Groff document with [GNU Source-highlight](http://www.gnu.org/software/src-highlite/) to produce syntax highlighted text. -The output is formatted according to `groff.outlang` which currently only works with -the [mom macros](http://www.schaffter.ca/mom/). +The output is formatted according to `groff.outlang`. +Versions for the [mom macros](http://www.schaffter.ca/mom/) (`groff-mom.outlang`) and +for the classic ms macros (`groff-ms.outlang`) are provided. -Example: +Example (mom): -``` +```groff .QUOTE .CODE .HIGHLIGHT c @@ -51,6 +52,18 @@ int main(int argc, char **argv) .QUOTE OFF ``` +## HIGHLIGHT (Lua) + +`highlight.lua` is a reimplementation of `highlight.sno` in Lua 5.2 and may work +better on some operating systems. + +In addition to the aforementioned syntax, the Lua version allows you to specify a filename after +the language identifier to process an external file: + +```groff +.HIGHLIGHT c hello.c +``` + ## UML `uml.sno` is a small preprocessor (again requires CSNOBOL4) that diff --git a/groff-mom.outlang b/groff-mom.outlang new file mode 100644 index 0000000..60e217e --- /dev/null +++ b/groff-mom.outlang @@ -0,0 +1,72 @@ +extension "mom" + +doctemplate +" +.NEWCOLOR green RGB #33CC00 +.NEWCOLOR red RGB #FF0000 +.NEWCOLOR darkred RGB #990000 +.NEWCOLOR blue RGB #0000FF +.NEWCOLOR brown RGB #9A1900 +.NEWCOLOR pink RGB #CC33CC +.NEWCOLOR yellow RGB #FFCC00 +.NEWCOLOR cyan RGB #66FFFF +.NEWCOLOR purple RGB #993399 +.NEWCOLOR orange RGB #FF6600 +.NEWCOLOR brightorange RGB #FF9900 +.NEWCOLOR brightgreen RGB #33FF33 +.NEWCOLOR darkgreen RGB #009900 +.NEWCOLOR black RGB #000000 +.NEWCOLOR teal RGB #008080 +.NEWCOLOR gray RGB #808080 +.NEWCOLOR darkblue RGB #000080 +.NEWCOLOR white RGB #FFFFFF +" + +" +" +end + +# NOTE: There must be a Typewrite Bold style +# I'm using the Latin Modern fonts that have this style. +# Otherwise you could use Mom's \*[BOLDER] escape. + +bold "\f[TTB]$text\fP" +italics "\f[TTI]$text\fP" +# FIXME: no way to implement underline generically +#underline + +#notfixed "\fR$text\fP" +#fixed "\fR$text\fP" + +color "\*[$style]$text\*[black]" + +colormap +"green" "green" +"red" "red" +"darkred" "darkred" +"blue" "blue" +"brown" "brown" +"pink" "pink" +"yellow" "yellow" +"cyan" "cyan" +"purple" "purple" +"orange" "orange" +"brightorange" "brightorange" +"brightgreen" "brightgreen" +"darkgreen" "darkgreen" +"black" "black" +"teal" "teal" +"gray" "gray" +"darkblue" "darkblue" +"white" "white" +default "black" +end + +# lines may be empty, so begin them with the non-spacing \& +# also, this allows "." and "'" at the beginning of the line +lineprefix "\&" + +translations +"\\" "\\e" +#"\t" " " +end diff --git a/groff-ms.outlang b/groff-ms.outlang new file mode 100644 index 0000000..c7af6db --- /dev/null +++ b/groff-ms.outlang @@ -0,0 +1,40 @@ +extension "ms" + +bold "\f[CB]$text\fP" +italics "\f[CI]$text\fP" +underline "\f[CI]$text\fP" +fixed "\f[CR]$text\fP" +color "\m[$style]$text\m[black]" + +#anchor "$infilename : $linenum - $text" +#reference "$text \(-> $infile:$linenum, page : $infilename:$linenum" + +# lines may be empty, so begin them with the non-spacing \& +# also, this allows "." and "'" at the beginning of the line +lineprefix "\&" + +colormap +"green" "green" +"red" "red" +"darkred" "darkred" +"blue" "blue" +"brown" "brown" +"pink" "pink" +"yellow" "yellow" +"cyan" "cyan" +"purple" "purple" +"orange" "orange" +"brightorange" "brightorange" +"brightgreen" "brightgreen" +"darkgreen" "darkgreen" +"black" "black" +"teal" "teal" +"gray" "gray" +"darkblue" "darkblue" +default "black" +end + +translations +"\\" "\\e" +#"\t" " " +end diff --git a/groff.outlang b/groff.outlang deleted file mode 100644 index 60e217e..0000000 --- a/groff.outlang +++ /dev/null @@ -1,72 +0,0 @@ -extension "mom" - -doctemplate -" -.NEWCOLOR green RGB #33CC00 -.NEWCOLOR red RGB #FF0000 -.NEWCOLOR darkred RGB #990000 -.NEWCOLOR blue RGB #0000FF -.NEWCOLOR brown RGB #9A1900 -.NEWCOLOR pink RGB #CC33CC -.NEWCOLOR yellow RGB #FFCC00 -.NEWCOLOR cyan RGB #66FFFF -.NEWCOLOR purple RGB #993399 -.NEWCOLOR orange RGB #FF6600 -.NEWCOLOR brightorange RGB #FF9900 -.NEWCOLOR brightgreen RGB #33FF33 -.NEWCOLOR darkgreen RGB #009900 -.NEWCOLOR black RGB #000000 -.NEWCOLOR teal RGB #008080 -.NEWCOLOR gray RGB #808080 -.NEWCOLOR darkblue RGB #000080 -.NEWCOLOR white RGB #FFFFFF -" - -" -" -end - -# NOTE: There must be a Typewrite Bold style -# I'm using the Latin Modern fonts that have this style. -# Otherwise you could use Mom's \*[BOLDER] escape. - -bold "\f[TTB]$text\fP" -italics "\f[TTI]$text\fP" -# FIXME: no way to implement underline generically -#underline - -#notfixed "\fR$text\fP" -#fixed "\fR$text\fP" - -color "\*[$style]$text\*[black]" - -colormap -"green" "green" -"red" "red" -"darkred" "darkred" -"blue" "blue" -"brown" "brown" -"pink" "pink" -"yellow" "yellow" -"cyan" "cyan" -"purple" "purple" -"orange" "orange" -"brightorange" "brightorange" -"brightgreen" "brightgreen" -"darkgreen" "darkgreen" -"black" "black" -"teal" "teal" -"gray" "gray" -"darkblue" "darkblue" -"white" "white" -default "black" -end - -# lines may be empty, so begin them with the non-spacing \& -# also, this allows "." and "'" at the beginning of the line -lineprefix "\&" - -translations -"\\" "\\e" -#"\t" " " -end diff --git a/highlight.lua b/highlight.lua new file mode 100755 index 0000000..eab9d2b --- /dev/null +++ b/highlight.lua @@ -0,0 +1,43 @@ +#!/usr/local/bin/lua52 +local pipe +local line_prefix = '' + +for line in io.input():lines() do + if pipe then + if line:match("^%. *HIGHLIGHT *") then + pipe:close() + pipe = nil + -- empty line to fix up line numbering + -- FIXME: the linefeed is necessary because source-highlight did not + -- terminate the last line + print "\n." + else + -- the last linefeed is not piped, so that we don't get empty lines + -- at the end of a code block + pipe:write(line_prefix, line) + line_prefix = '\n' + end + else + local lang = line:match("^%. *HIGHLIGHT +(.*)$") + if lang then + if lang:match(" ") then + local file + lang, file = lang:match("^([^ ]*) +(.*)$") + lang = lang == "default" and "--failsafe" or "-s "..lang + os.execute("source-highlight --outlang-def groff.outlang "..lang.." -i "..file) + -- FIXME: Emit .lf statement + -- FIXME: Omit the last character + print "" + else + lang = lang == "default" and "--failsafe" or "-s "..lang + pipe = io.popen("source-highlight --outlang-def groff.outlang "..lang, "w") + -- empty line to fix up line numbering + print "." + -- omit linefeed on first print + line_prefix = '' + end + else + print(line) + end + end +end -- cgit v1.2.3