From 7f91cb500e1a16d3701014deddf5d6b4618beed3 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?=
ANSI color escape code highlighting
-![mosra@don-perverzo m.css]$
+![mosra@don-perverzo m.css]$
ââ ââ ââ ââ ââ ââ ââ
+
+ ââ ââ ââ ââ
diff --git a/documentation/test_doxygen/contents_code_language/console-colors.ansi b/documentation/test_doxygen/contents_code_language/console-colors.ansi
new file mode 120000
index 00000000..f10d6d62
--- /dev/null
+++ b/documentation/test_doxygen/contents_code_language/console-colors.ansi
@@ -0,0 +1 @@
+../../../plugins/m/test/code/console-colors.ansi
\ No newline at end of file
diff --git a/documentation/test_doxygen/contents_code_language/input.dox b/documentation/test_doxygen/contents_code_language/input.dox
index c8550a97..6a2033fa 100644
--- a/documentation/test_doxygen/contents_code_language/input.dox
+++ b/documentation/test_doxygen/contents_code_language/input.dox
@@ -36,6 +36,8 @@ Another paragraph.
![0;34m[[1;37mmosra@don-perverzo [0;37mm.css[0;34m][1;36m$ [0m
@endcode
+@include console-colors.ansi
+
*/
/** @page warnings Code that produces warnings
diff --git a/plugins/ansilexer.py b/plugins/ansilexer.py
index dd11d5ca..d9d12e39 100644
--- a/plugins/ansilexer.py
+++ b/plugins/ansilexer.py
@@ -22,7 +22,10 @@
# DEALINGS IN THE SOFTWARE.
#
+import re
+
from pygments.lexer import RegexLexer
+from pygments.formatters import HtmlFormatter
from pygments.token import *
class AnsiLexer(RegexLexer):
@@ -65,8 +68,35 @@ class AnsiLexer(RegexLexer):
yield (match.start(), string_to_tokentype(token), text)
+ def callback_fg_color(lexer, match):
+ token = 'Generic.AnsiForegroundColor{:02x}{:02x}{:02x}'.format(
+ int(match.group(1)), int(match.group(2)), int(match.group(3)))
+ yield (match.start, string_to_tokentype(token), match.group(4))
+
+ def callback_fg_bg_color(lexer, match):
+ token = 'Generic.AnsiForegroundBackgroundColor{:02x}{:02x}{:02x}'.format(
+ int(match.group(1)), int(match.group(2)), int(match.group(3)))
+ yield (match.start, string_to_tokentype(token), match.group(4))
+
tokens = {
'root': [
('[^\x1b]+', Text),
+ ('\x1b\\[38;2;(\\d+);(\\d+);(\\d+)m\x1b\\[48;2;\\d+;\\d+;\\d+m([^\x1b]+)\x1b\\[0m', callback_fg_bg_color),
+ ('\x1b\\[38;2;(\\d+);(\\d+);(\\d+)m([^\x1b]+)\x1b\\[0m', callback_fg_color),
('\x1b\\[(\\d+)(;\\d+)?m([^\x1b]*)', callback)]
}
+
+_ansi_fg_color_re = re.compile('class="g g-AnsiForegroundColor([0-9a-f]{6})"')
+_ansi_fg_bg_color_re = re.compile('class="g g-AnsiForegroundBackgroundColor([0-9a-f]{6})"')
+
+class HtmlAnsiFormatter(HtmlFormatter):
+ def wrap(self, source, outfile):
+ return self._wrap_code(source)
+
+ def _wrap_code(self, source):
+ for i, t in source:
+ if i == 1: # it's a line of formatted code
+ t = _ansi_fg_bg_color_re.sub('style="color: #\\1; background-color: #\\1"', t)
+ t = _ansi_fg_color_re.sub('style="color: #\\1"', t)
+ #t += 'H'
+ yield i, t
diff --git a/plugins/m/code.py b/plugins/m/code.py
index 4290e01b..4bb42740 100644
--- a/plugins/m/code.py
+++ b/plugins/m/code.py
@@ -59,7 +59,10 @@ def _highlight(code, language, options, is_block):
else:
class_ = 'm-code'
- formatter = HtmlFormatter(nowrap=True, **options)
+ if isinstance(lexer, ansilexer.AnsiLexer):
+ formatter = ansilexer.HtmlAnsiFormatter(**options)
+ else:
+ formatter = HtmlFormatter(nowrap=True, **options)
parsed = highlight(code, lexer, formatter).rstrip()
if not is_block: parsed.lstrip()
diff --git a/plugins/m/test/code/console-colors.ansi b/plugins/m/test/code/console-colors.ansi
new file mode 100644
index 00000000..ccd327f6
--- /dev/null
+++ b/plugins/m/test/code/console-colors.ansi
@@ -0,0 +1,5 @@
+[38;2;220;220;220m[48;2;220;220;220mââ[0m [38;2;165;201;234m[48;2;165;201;234mââ[0m [38;2;59;210;103m[48;2;59;210;103mââ[0m [38;2;199;207;47m[48;2;199;207;47mââ[0m [38;2;205;52;49m[48;2;205;52;49mââ[0m [38;2;47;131;204m[48;2;47;131;204mââ[0m [38;2;116;116;116m[48;2;116;116;116mââ[0m
+
+Leading zeros: [38;2;15;116;3m[48;2;15;116;3mââ[0m [38;2;15;116;3mââ[0m
+
+[38;2;59;210;103m [0m [38;2;59;210;103m [0m [38;2;59;210;103mââ[0m [38;2;59;210;103mââ[0m [38;2;59;210;103mââ[0m [38;2;59;210;103m[48;2;59;210;103mââ[0m
diff --git a/plugins/m/test/code/page.html b/plugins/m/test/code/page.html
index 27374967..7d50e386 100644
--- a/plugins/m/test/code/page.html
+++ b/plugins/m/test/code/page.html
@@ -34,6 +34,12 @@ rendered as plain monospace text: code
.
![mosra@don-perverzo m.css]$ ls CONTRIBUTING.rst CREDITS.rst doc plugins README.rst COPYING css documentation pelican-theme site+
Console colors:
+ââ ââ ââ ââ ââ ââ ââ + +Leading zeros: ââ ââ + + ââ ââ ââ ââ
// this language is not highlighted
Properly preserve backslashes: \frac{a}{b}
Don't trim leading spaces in blocks:
diff --git a/plugins/m/test/code/page.rst b/plugins/m/test/code/page.rst index 0e92fae8..e3d92548 100644 --- a/plugins/m/test/code/page.rst +++ b/plugins/m/test/code/page.rst @@ -26,6 +26,11 @@ rendered as plain monospace text: :code:`code`. .. include:: console.ansi :code: ansi +Console colors: + +.. include:: console-colors.ansi + :code: ansi + .. code:: whatthefuck // this language is not highlighted -- 2.30.2