Add a function to convert ncurses colors to HTML color code.
(cherry picked from commit 04f103b9e6
)
This commit is contained in:
parent
2c9cbc9f58
commit
f271ef0620
1 changed files with 24 additions and 1 deletions
25
src/xhtml.py
25
src/xhtml.py
|
@ -14,6 +14,7 @@ poezio colors to xhtml code
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import curses
|
||||||
from sleekxmpp.xmlstream import ET
|
from sleekxmpp.xmlstream import ET
|
||||||
from xml.etree.ElementTree import ElementTree
|
from xml.etree.ElementTree import ElementTree
|
||||||
from sys import version_info
|
from sys import version_info
|
||||||
|
@ -192,6 +193,29 @@ def get_body_from_message_stanza(message):
|
||||||
return xhtml_to_poezio_colors(xhtml_body)
|
return xhtml_to_poezio_colors(xhtml_body)
|
||||||
return message['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 xhtml_to_poezio_colors(text):
|
||||||
def parse_css(css):
|
def parse_css(css):
|
||||||
def get_color(value):
|
def get_color(value):
|
||||||
|
@ -324,7 +348,6 @@ def xhtml_to_poezio_colors(text):
|
||||||
message += trim(elem.tail)
|
message += trim(elem.tail)
|
||||||
return message
|
return message
|
||||||
|
|
||||||
|
|
||||||
def clean_text(s):
|
def clean_text(s):
|
||||||
"""
|
"""
|
||||||
Remove all xhtml-im attributes (\x19etc) from the string with the
|
Remove all xhtml-im attributes (\x19etc) from the string with the
|
||||||
|
|
Loading…
Reference in a new issue