From f323d77e340eeca0fafc7dd28b52a0e7fdde9a88 Mon Sep 17 00:00:00 2001 From: Mark Lodato Date: Mon, 5 Sep 2011 19:43:21 -0400 Subject: [PATCH] implement inverse attribute There is no HTML "inverse" style, so we have to fake it by explicitly swapping foreground and background colors. However, this is a problem if the character does not have explicit foreground and background colors, and if the
 tag's foreground and background were also not
set.  So, we will introduce a new option to give the fallback colors in
that case.
---
 CHANGELOG                   |  1 +
 README.rst                  | 10 ++++++++++
 TODO                        |  2 --
 test/t0220-SGR_inverse.html |  6 ++++++
 test/t0220-SGR_inverse.in   |  4 ++++
 vt100.py                    | 27 +++++++++++++++++++++++++++
 6 files changed, 48 insertions(+), 2 deletions(-)
 create mode 100644 test/t0220-SGR_inverse.html
 create mode 100644 test/t0220-SGR_inverse.in

diff --git a/CHANGELOG b/CHANGELOG
index 9b4f7ee..c3de8be 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 Version 0.4 - ????-??-??
 - Add --geometry option.
 - Add reading of configuration from ~/.vt100rc.
+- Implement the "inverse" graphics attribute.
 - Internal refactoring.  Of note, now the entire input file is read at once.
 - Fix a few bugs.
 
diff --git a/README.rst b/README.rst
index 68ca8a1..d258ba1 100644
--- a/README.rst
+++ b/README.rst
@@ -92,6 +92,16 @@ geometry = {WxH, detect}
     current terminal's geometry is detected using ``stty size``.
     Default is '80x24'.
 
+inverse_bg = COLOR
+    Background color to use for the "inverse" attribute when neither the
+    character's foreground color attribute nor the ``foreground`` option is
+    set.  Default is 'black'.
+
+inverse_fg = COLOR
+    Foreground color to use for the "inverse" attribute when neither the
+    character's background color attribute nor the ``background`` option is
+    set.  Default is 'white'.
+
 verbosity = INT
     Act as those ``-v`` or ``-q`` was given abs(INT) times, if INT positive or
     negative, respectively.  Default is '0'.
diff --git a/TODO b/TODO
index 907f205..a5baff6 100644
--- a/TODO
+++ b/TODO
@@ -38,8 +38,6 @@ Options
 Output
 ======
 
-* 'inverse' attribute for HTML
-
 * Xterm keeps track of long lines so that you can select wrapped lines as a
   single line.  This is not possible for text output, but it might be in HTML.
   It is possible to set the ``pre``\ 's style to wrap using the following::
diff --git a/test/t0220-SGR_inverse.html b/test/t0220-SGR_inverse.html
new file mode 100644
index 0000000..7218bf4
--- /dev/null
+++ b/test/t0220-SGR_inverse.html
@@ -0,0 +1,6 @@
+
+This is inverse text with default fg and bg.
+This is inverse text with red fg and default bg.
+This is inverse text with default fg and red bg.
+This is inverse text with green fg and red bg.
+
diff --git a/test/t0220-SGR_inverse.in b/test/t0220-SGR_inverse.in new file mode 100644 index 0000000..703a59a --- /dev/null +++ b/test/t0220-SGR_inverse.in @@ -0,0 +1,4 @@ +This is inverse text with default fg and bg. +This is inverse text with red fg and default bg. +This is inverse text with default fg and red bg. +This is inverse text with green fg and red bg. diff --git a/vt100.py b/vt100.py index cd9f9d0..3853c3c 100755 --- a/vt100.py +++ b/vt100.py @@ -93,6 +93,16 @@ current terminal's geometry is detected using ``stty size``. Default is '80x24'. +inverse_bg = COLOR + Background color to use for the "inverse" attribute when neither the + character's foreground color attribute nor the ``foreground`` option is + set. Default is 'black'. + +inverse_fg = COLOR + Foreground color to use for the "inverse" attribute when neither the + character's background color attribute nor the ``background`` option is + set. Default is 'white'. + verbosity = INT Act as those ``-v`` or ``-q`` was given abs(INT) times, if INT positive or negative, respectively. Default is '0'. @@ -267,6 +277,8 @@ class HtmlFormatter (TextFormatter): default_options = { 'foreground' : '', 'background' : '', + 'inverse_fg' : 'white', + 'inverse_bg' : 'black', } # [black, red, green, brown/yellow, blue, magenta, cyan, white] @@ -302,6 +314,10 @@ def set_color(self, index, value): def parse_config(self, config): self._parse_config(config, config.default_section, set()) + if self.options['foreground']: + self.options['inverse_bg'] = self.options['foreground'] + if self.options['background']: + self.options['inverse_fg'] = self.options['background'] def _parse_config(self, config, section, seen): if config.has_option(section, 'colorscheme'): @@ -328,6 +344,17 @@ def _parse_config(self, config, section, seen): def _compute_style(self, attr): # TODO implement inverse out = [] + if attr.pop('inverse', None): + fg = attr.pop('fg_color', None) + bg = attr.pop('bg_color', None) + if fg is not None: + attr['bg_color'] = fg + else: + out.append('background-color: %s' % self.options['inverse_bg']) + if bg is not None: + attr['fg_color'] = bg + else: + out.append('color: %s' % self.options['inverse_fg']) for key in sorted(attr): value = attr[key] try: