From 5e3381f6d41a61e54fdedeacbcb52be5ce58d4d9 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 29 Mar 2011 16:11:37 +0200 Subject: Add files for xhtml containing xhtml->shell colors and shell colors->poezio colors --- src/xhtml.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/xhtml.py diff --git a/src/xhtml.py b/src/xhtml.py new file mode 100644 index 00000000..71a3ad3e --- /dev/null +++ b/src/xhtml.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# +# Copyright 2010-2011 Le Coz Florent +# +# This file is part of Poezio. +# +# Poezio is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# Poezio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Poezio. If not, see . + +""" +Various methods to convert +shell colors to poezio colors, +xhtml code to shell colors, +poezio colors to xhtml code +""" + +import re +import subprocess + +shell_colors_re = re.compile(r'(\[(?:\d+;)*(?:\d+m))') + +def shell_colors_to_poezio_colors(string): + """ + 'shell colors' means something like: + + Bonjour ^[[0;32msalut^[[0m + + The current understanding of this syntax is: + n = 0: reset all attributes to defaults + n >= 30 and n <= 37: set the foreground to n-30 + + """ + def repl(matchobj): + exp = matchobj.group(0)[2:-1] + numbers = [int(nb) for nb in exp.split(';')] + res = '' + for num in numbers: + if num == 0: + res += r'\x19o' + elif num >= 30 and num <= 37: + res += r'\x19%s' % (num-30,) + return res + return shell_colors_re.sub(repl, string) + +def xhtml_code_to_shell_colors(string): + """ + Use a console browser to parse the xhtml and + make it return a shell-colored string + """ + process = subprocess.Popen(["elinks", "-dump", "-dump-color-mode", "2"], stdout=subprocess.PIPE, stdin=subprocess.PIPE) + result = process.communicate(input=string.encode('utf-8'))[0] + return result.decode('utf-8').strip() + +if __name__ == '__main__': + print(xhtml_code_to_shell_colors(""" + + +

+ Wow, I'm green + with envy! +

+ + +""")) -- cgit v1.2.3