Fix the dispay of links in xhtml messages. Reenabling xhtml-im by default

This commit is contained in:
Florent Le Coz 2011-04-10 16:44:26 +02:00
parent 72ec428320
commit 6cd5f8d4f7
2 changed files with 35 additions and 14 deletions

View file

@ -59,7 +59,7 @@ highlight_on =
# but you can still send colored messages. You just wont be able te see # but you can still send colored messages. You just wont be able te see
# the colors, though # the colors, though
# Set to true if you want to see colored messages # Set to true if you want to see colored messages
enable_xhtml_im = false enable_xhtml_im = true
# Set a number for this setting. # Set a number for this setting.
# The join OR status-change notices will be # The join OR status-change notices will be

View file

@ -25,6 +25,8 @@ poezio colors to xhtml code
import re import re
import subprocess import subprocess
from sleekxmpp.xmlstream import ET
from xml.etree.ElementTree import ElementTree
from config import config from config import config
import logging import logging
@ -41,9 +43,10 @@ def get_body_from_message_stanza(message):
poezio colors if there's an xhtml_im element, or poezio colors if there's an xhtml_im element, or
the body (without any color) otherwise 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'] xhtml_body = message['xhtml_im']
if xhtml_body: if xhtml_body:
xhtml_body = convert_links_to_plaintext(xhtml_body)
try: try:
shell_body = xhtml_code_to_shell_colors(xhtml_body) shell_body = xhtml_code_to_shell_colors(xhtml_body)
except OSError: except OSError:
@ -52,6 +55,35 @@ def get_body_from_message_stanza(message):
return shell_colors_to_poezio_colors(shell_body) return shell_colors_to_poezio_colors(shell_body)
return message['body'] return message['body']
def convert_links_to_plaintext(text):
"""
Replace
<a href='URL'>click</a>
by
<url> (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): def clean_text(string):
""" """
Remove all \x19 from the 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))) self._win.attron(common.curses_color_pair(int(attr_char)))
next_attr_char = string.find('\x19') next_attr_char = string.find('\x19')
if __name__ == '__main__':
# print(xhtml_code_to_shell_colors("""
# <html xmlns='http://jabber.org/protocol/xhtml-im'>
# <body xmlns='http://www.w3.org/1999/xhtml'>
# <p style='font-size:large'>
# <em>Wow</em>, I&apos;m <span style='color:green'>green</span>
# with <strong>envy</strong>!
# </p>
# </body>
# </html>
# """))
print(poezio_colors_to_html('\x191red\x19o \x192green\x19o \x19b\x192green and bold'))