From 6cd5f8d4f792a9fac50ab6a70922fc61d89d7931 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 10 Apr 2011 16:44:26 +0200 Subject: Fix the dispay of links in xhtml messages. Reenabling xhtml-im by default --- src/xhtml.py | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'src/xhtml.py') diff --git a/src/xhtml.py b/src/xhtml.py index a9f0af60..f0c55978 100644 --- a/src/xhtml.py +++ b/src/xhtml.py @@ -25,6 +25,8 @@ poezio colors to xhtml code import re import subprocess +from sleekxmpp.xmlstream import ET +from xml.etree.ElementTree import ElementTree from config import config import logging @@ -41,9 +43,10 @@ def get_body_from_message_stanza(message): poezio colors if there's an xhtml_im element, or the body (without any color) otherwise """ - if config.get('enable_xhtml_im', 'false') == 'true': + if config.get('enable_xhtml_im', 'true') == 'true': xhtml_body = message['xhtml_im'] if xhtml_body: + xhtml_body = convert_links_to_plaintext(xhtml_body) try: shell_body = xhtml_code_to_shell_colors(xhtml_body) except OSError: @@ -52,6 +55,35 @@ def get_body_from_message_stanza(message): return shell_colors_to_poezio_colors(shell_body) return message['body'] +def convert_links_to_plaintext(text): + """ + Replace + click + by + (click) + in plain text + """ + log.debug(text) + xml = ElementTree(ET.fromstring(text)) + for parent in xml.getiterator(): + previous_child = None + for child in parent: + if child.tag == '{http://www.w3.org/1999/xhtml}a': + link_text = '\n%s (%s)'%(child.attrib['href'], child.text) + if previous_child is not None: + if previous_child.tail is None: + previous_child.tail = link_text + else: + previous_child.tail += link_text + else: + if parent.text is None: + parent.text = link_text + else: + parent.text += link_text + parent.remove(child) + previous_child = child + return ET.tostring(xml.getroot()) + def clean_text(string): """ Remove all \x19 from the string @@ -180,15 +212,4 @@ def poezio_colors_to_xhtml(string): self._win.attron(common.curses_color_pair(int(attr_char))) next_attr_char = string.find('\x19') -if __name__ == '__main__': -# print(xhtml_code_to_shell_colors(""" -# -# -#

-# Wow, I'm green -# with envy! -#

-# -# -# """)) - print(poezio_colors_to_html('\x191red\x19o \x192green\x19o \x19b\x192green and bold')) + -- cgit v1.2.3