-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygments-groff.py
executable file
·43 lines (36 loc) · 1.31 KB
/
pygments-groff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/local/bin/python3.9
from pygments.lexers import get_lexer_by_name, RawTokenLexer
from pygments.formatters import GroffFormatter
from pygments import highlight
import re
from sys import stdin, stdout, stderr
formatter = GroffFormatter(style="sas")
start_pattern = re.compile(r"\. *HIGHLIGHT +([^ ]+)( +(.*))?\n")
end_pattern = re.compile(r"\. *HIGHLIGHT *\n")
while True:
for line in stdin:
stdout.write(line)
params = start_pattern.match(line)
if params:
break
if not params: # EOF
break
lang, filename = params.group(1, 3)
lexer = RawTokenLexer() if lang == "default" else get_lexer_by_name(lang)
# NOTE: This option is broken and will result in a bogus empty line with the GroffFormatter
lexer.ensurenl = False
contents = []
if filename:
contents.append(open(filename).read())
stdout.write(".ds HIGHLIGHT-LF \\n[.c] \\n[.F]\n")
stdout.write(".lf 1 "+filename+"\n")
else:
for line in stdin:
if end_pattern.match(line):
stdout.write(line)
break
contents.append(line)
formatted = highlight("".join(contents), lexer, formatter)
stdout.write(formatted.replace("\n\n", "\n\\&\n")+"\n")
if filename:
stdout.write(".lf \\*[HIGHLIGHT-LF]\n")