From f271ef0620ab91573bfebb196ef5db54296bab4b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 7 Nov 2011 19:43:13 +0100 Subject: Add a function to convert ncurses colors to HTML color code. (cherry picked from commit 04f103b9e67ccd1de7376be4f7b5156ec425e99c) --- src/xhtml.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/xhtml.py') diff --git a/src/xhtml.py b/src/xhtml.py index f6487905..2cedba7d 100644 --- a/src/xhtml.py +++ b/src/xhtml.py @@ -14,6 +14,7 @@ poezio colors to xhtml code import re import subprocess +import curses from sleekxmpp.xmlstream import ET from xml.etree.ElementTree import ElementTree from sys import version_info @@ -192,6 +193,29 @@ def get_body_from_message_stanza(message): return xhtml_to_poezio_colors(xhtml_body) return message['body'] +def ncurses_color_to_html(color): + """ + Takes an int between 0 and 256 and returns + a string of the form #XXXXXX representing an + html color. + """ + if color <= 15: + (r, g, b) = curses.color_content(color) + r = r / 1000 * 6 + g = g / 1000 * 6 + b = b / 1000 * 6 + elif color <= 231: + color = color - 16 + r = color % 6 + color = color / 6 + g = color % 6 + color = color / 6 + b = color % 6 + else: + color -= 232 + r = g = b = color / 24 * 6 + return '#%X%X%X' % (r*256/6, g*256/6, b*256/6) + def xhtml_to_poezio_colors(text): def parse_css(css): def get_color(value): @@ -324,7 +348,6 @@ def xhtml_to_poezio_colors(text): message += trim(elem.tail) return message - def clean_text(s): """ Remove all xhtml-im attributes (\x19etc) from the string with the -- cgit v1.2.3 From 24043586893ecfa599379c15fd7ae5fa531d2a14 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 7 Nov 2011 19:55:16 +0100 Subject: Fix the curses -> html color conversion (cherry picked from commit 689f17cfd79b152d3ca75e570f9f13f70aa6c425) --- src/xhtml.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/xhtml.py') diff --git a/src/xhtml.py b/src/xhtml.py index 2cedba7d..748c9080 100644 --- a/src/xhtml.py +++ b/src/xhtml.py @@ -201,9 +201,9 @@ def ncurses_color_to_html(color): """ if color <= 15: (r, g, b) = curses.color_content(color) - r = r / 1000 * 6 - g = g / 1000 * 6 - b = b / 1000 * 6 + r = r / 1000 * 6 - 0.01 + g = g / 1000 * 6 - 0.01 + b = b / 1000 * 6 - 0.01 elif color <= 231: color = color - 16 r = color % 6 @@ -214,7 +214,7 @@ def ncurses_color_to_html(color): else: color -= 232 r = g = b = color / 24 * 6 - return '#%X%X%X' % (r*256/6, g*256/6, b*256/6) + return '#%02X%02X%02X' % (r*256/6, g*256/6, b*256/6) def xhtml_to_poezio_colors(text): def parse_css(css): -- cgit v1.2.3 From f13d03c149a94a8316b9acb5944454357a395636 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 7 Nov 2011 19:56:03 +0100 Subject: poezio_colors_to_html now takes full colors (\x19xxx}) to generate the xhtml code (cherry picked from commit 2b9a43ce6a5fa6ad93ab570a99d4d92337a40110) --- src/xhtml.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src/xhtml.py') diff --git a/src/xhtml.py b/src/xhtml.py index 748c9080..5eda635b 100644 --- a/src/xhtml.py +++ b/src/xhtml.py @@ -402,7 +402,6 @@ def poezio_colors_to_html(string): attr_char = string[next_attr_char+1].lower() if next_attr_char != 0: res += string[:next_attr_char] - string = string[next_attr_char+2:] if attr_char == 'o': for elem in opened_elements[::-1]: res += '' % (elem,) @@ -411,17 +410,20 @@ def poezio_colors_to_html(string): if 'strong' not in opened_elements: opened_elements.append('strong') res += '' - elif attr_char in digits: - number = int(attr_char) - if number in number_to_color_names: - if 'strong' in opened_elements: - res += '' - opened_elements.remove('strong') - if 'span' in opened_elements: - res += '' - else: - opened_elements.append('span') - res += "" % (number_to_color_names[number]) + if attr_char in digits: + number_str = string[next_attr_char+1:string.find('}', next_attr_char)] + number = int(number_str) + if 'strong' in opened_elements: + res += '' + opened_elements.remove('strong') + if 'span' in opened_elements: + res += '' + else: + opened_elements.append('span') + res += "" % (ncurses_color_to_html(number),) + string = string[next_attr_char+len(number_str)+2:] + else: + string = string[next_attr_char+2:] next_attr_char = string.find('\x19') res += string for elem in opened_elements[::-1]: -- cgit v1.2.3 From d83eda6fd4fc74d5bedb1ca860c1015e7e0d3732 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 8 Nov 2011 02:07:40 +0100 Subject: escape <, > and & in xhtml-im bodies. --- src/xhtml.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/xhtml.py') diff --git a/src/xhtml.py b/src/xhtml.py index 5eda635b..44195f90 100644 --- a/src/xhtml.py +++ b/src/xhtml.py @@ -16,7 +16,11 @@ import re import subprocess import curses from sleekxmpp.xmlstream import ET + +import xml.sax.saxutils + from xml.etree.ElementTree import ElementTree + from sys import version_info from config import config @@ -401,7 +405,7 @@ def poezio_colors_to_html(string): while next_attr_char != -1: attr_char = string[next_attr_char+1].lower() if next_attr_char != 0: - res += string[:next_attr_char] + res += xml.sax.saxutils.escape(string[:next_attr_char]) if attr_char == 'o': for elem in opened_elements[::-1]: res += '' % (elem,) @@ -425,7 +429,7 @@ def poezio_colors_to_html(string): else: string = string[next_attr_char+2:] next_attr_char = string.find('\x19') - res += string + res += xml.sax.saxutils.escape(string) for elem in opened_elements[::-1]: res += '' % (elem,) res += "

" -- cgit v1.2.3