summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2011-03-29 20:56:11 +0200
committerFlorent Le Coz <louiz@louiz.org>2011-03-29 20:56:11 +0200
commit42f4d555125ef3616e630b3e502285c496d382fc (patch)
tree243f1eaaf082c4c53b0f6a7ace78c82a57e05033
parentba02b51554e2125d54de4b748838b9b0495df1e4 (diff)
downloadpoezio-42f4d555125ef3616e630b3e502285c496d382fc.tar.gz
poezio-42f4d555125ef3616e630b3e502285c496d382fc.tar.bz2
poezio-42f4d555125ef3616e630b3e502285c496d382fc.tar.xz
poezio-42f4d555125ef3616e630b3e502285c496d382fc.zip
poezio_colors to_html
-rw-r--r--src/xhtml.py65
1 files changed, 55 insertions, 10 deletions
diff --git a/src/xhtml.py b/src/xhtml.py
index 5f0e9927..bfdc1ef6 100644
--- a/src/xhtml.py
+++ b/src/xhtml.py
@@ -49,6 +49,50 @@ def get_body_from_message_stanza(message):
return shell_colors_to_poezio_colors(shell_body)
return message['body']
+number_to_color_names = {
+ 1: 'red',
+ 2: 'green',
+}
+
+def poezio_colors_to_html(string):
+ """
+ Convert poezio colors to html makups
+ (e.g. \x191: <span style='color: red'>)
+ """
+ # TODO underlined
+
+ # a list of all opened elements, e.g. ['strong', 'span']
+ # So that we know what we need to close
+ opened_elements = []
+ res = "<body xmlns='http://www.w3.org/1999/html'>"
+ next_attr_char = string.find('\x19')
+ while next_attr_char != -1:
+ 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 += '</%s>' % (elem,)
+ opened_elements = []
+ elif attr_char == 'b':
+ if 'strong' not in opened_elements:
+ opened_elements.append('strong')
+ res += '<strong>'
+ elif attr_char.isdigit():
+ number = int(attr_char)
+ if number in number_to_color_names:
+ if 'span' in opened_elements:
+ res += '</span>'
+ res += "<span style='color: %s'>" % (number_to_color_names[number])
+ opened_elements.append('span')
+ next_attr_char = string.find('\x19')
+ res += string
+ for elem in opened_elements[::-1]:
+ res += '</%s>' % (elem,)
+ res += "</body>"
+ return res
+
def shell_colors_to_poezio_colors(string):
"""
'shell colors' means something like:
@@ -82,13 +126,14 @@ def xhtml_code_to_shell_colors(string):
return result.decode('utf-8').strip()
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(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'))