/usr/lib/python2.7/dist-packages/tryton/common/htmltextbuffer.py is in tryton-client 3.8.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | # This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from io import BytesIO
import xml.etree.ElementTree as ET
from xml.sax.saxutils import escape, unescape
from HTMLParser import HTMLParser
import pango
import gtk
import chardet
MIME = 'text/html'
def _reverse_dict(dct):
return {j: i for i, j in dct.iteritems()}
SIZE2SCALE = {
'1': pango.SCALE_XX_SMALL,
'2': pango.SCALE_X_SMALL,
'3': pango.SCALE_SMALL,
'4': pango.SCALE_MEDIUM,
'5': pango.SCALE_LARGE,
'6': pango.SCALE_X_LARGE,
'7': pango.SCALE_XX_LARGE,
}
SCALE2SIZE = _reverse_dict(SIZE2SCALE)
ALIGN2JUSTIFICATION = {
'left': gtk.JUSTIFY_LEFT,
'center': gtk.JUSTIFY_CENTER,
'right': gtk.JUSTIFY_RIGHT,
'justify': gtk.JUSTIFY_FILL,
}
JUSTIFICATION2ALIGN = _reverse_dict(ALIGN2JUSTIFICATION)
FAMILIES = ['normal', 'sans', 'serif', 'monospace']
def _markup(text):
return '<markup>%s</markup>' % text
def _strip_markup(text):
return text[len(u'<markup>'):-len(u'</markup>')]
def _strip_newline(text):
return ''.join(text.splitlines())
class MarkupHTMLParse(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.root = ET.Element('markup')
self._tags = [self.root]
self.head = False
self.body = True
def handle_starttag(self, tag, attrs):
if tag in ['b', 'i', 'u', 'div', 'font']:
el = ET.SubElement(self._tags[-1], tag)
for key, value in attrs:
el.set(key, value)
self._tags.append(el)
elif tag == 'br':
ET.SubElement(self._tags[-1], tag)
elif tag == 'head':
self.head = True
def handle_endtag(self, tag):
if tag in ['b', 'i', 'u', 'div', 'font']:
el = self._tags.pop()
assert el.tag == tag
elif tag == 'head':
self.head = False
elif tag == 'body':
self.body = False
def handle_data(self, data):
if self.head or not self.body:
return
el = self._tags[-1]
if len(el) == 0:
if el.text is None:
el.text = ''
el.text += data
else:
child = el[-1]
if child.tail is None:
child.tail = ''
child.tail += data
def parse_markup(markup_text):
'Return plain text and a list of start, end TextTag'
markup_text = BytesIO(_markup(markup_text))
plain_text = ''
tag_stack = []
tags = []
for event, element in ET.iterparse(markup_text, events=['start', 'end']):
if element.tag == 'markup':
if event == 'start' and element.text:
plain_text += unescape(element.text)
if event == 'end' and element.tail:
plain_text += unescape(element.tail)
continue
if event == 'start':
tag_stack.append((element, len(plain_text)))
if element.text:
plain_text += unescape(element.text)
elif event == 'end':
if element.tag == 'div':
plain_text += '\n'
assert tag_stack[-1][0] == element
_, start = tag_stack.pop()
end = len(plain_text)
tags.append((start, end, element))
if element.tail:
plain_text += unescape(element.tail)
return plain_text, tags
def normalize_markup(markup_text, method='html'):
if isinstance(markup_text, unicode):
markup_text = markup_text.encode('utf-8')
parser = MarkupHTMLParse()
parser.feed(_strip_newline(markup_text))
root = parser.root
parent_map = {c: p for p in root.iter() for c in p}
def order(em):
"Re-order alphabetically tags"
if (len(em) == 1
and not em.text
and em.tag not in ['div', 'markup']):
child, = em
if ((em.tag > child.tag or child.tag == 'div')
and not child.tail):
em.tag, child.tag = child.tag, em.tag
em.attrib, child.attrib = child.attrib, em.attrib
if em in parent_map:
order(parent_map[em])
# Add missing div for the first line
if len(root) > 0 and root[0].tag != 'div':
div = ET.Element('div')
for em in list(root):
if em.tag != 'div':
root.remove(em)
div.append(em)
else:
break
root.insert(0, div)
for em in root.iter():
while em.text is None and len(em) == 1:
dup, = em
if dup.tag == em.tag and dup.tail is None:
em.attrib.update(dup.attrib)
em.remove(dup)
em.extend(dup)
em.text = dup.text
else:
break
order(em)
# Add missing br to empty lines
for em in root.findall('div'):
if em.text is None and len(em) == 0:
em.append(ET.Element('br'))
# TODO order attributes
return _strip_markup(ET.tostring(root, encoding='utf-8', method=method))
def find_list_delta(old, new):
added = [e for e in new if e not in old]
removed = [e for e in reversed(old) if e not in new]
return added, removed
def serialize(register, content, start, end, data):
text = ''
if start.compare(end) == 0:
return text
iter_ = start
while True:
# Loop over line per line to get a div per line
tags = []
active_tags = []
end_line = iter_.copy()
if not end_line.ends_line():
end_line.forward_to_line_end()
if end_line.compare(end) > 0:
end_line = end
# Open div of the line
align = None
for tag in iter_.get_tags():
if tag.props.justification_set:
align = JUSTIFICATION2ALIGN[tag.props.justification]
if align:
text += '<div align="%s">' % align
else:
text += '<div>'
while True:
new_tags = iter_.get_tags()
added, removed = find_list_delta(tags, new_tags)
for tag in removed:
if tag not in active_tags:
continue
# close all open tags after this one
while active_tags:
atag = active_tags.pop()
text += _get_html(atag, close=True)
if atag == tag:
break
added.insert(0, atag)
for tag in added:
text += _get_html(tag)
active_tags.append(tag)
tags = new_tags
old_iter = iter_.copy()
# Move to next tag toggle
while True:
iter_.forward_char()
if iter_.get_char() == '\0':
break
if iter_.toggles_tag():
break
# Might have moved too far
if iter_.compare(end_line) > 0:
iter_ = end_line
text += escape(old_iter.get_text(iter_))
if iter_.compare(end_line) == 0:
break
# close any open tags
for tag in reversed(active_tags):
text += _get_html(tag, close=True)
# Close the div of the line
text += '</div>'
iter_.forward_char()
if iter_.compare(end) >= 0:
break
return normalize_markup(text)
def deserialize(register, content, iter_, text, create_tags, data):
if not isinstance(text, unicode):
encoding = chardet.detect(text)['encoding']
text = text.decode(encoding)
text = text.encode('utf-8')
text, tags = parse_markup(normalize_markup(text, method='xml'))
offset = iter_.get_offset()
content.insert(iter_, text)
def sort_key(tag):
start, end, element = tag
return start, -end, element
for start, end, element in sorted(tags, key=sort_key):
for tag in _get_tags(content, element):
istart = content.get_iter_at_offset(start + offset)
iend = content.get_iter_at_offset(end + offset)
content.apply_tag(tag, istart, iend)
return True
def setup_tags(text_buffer):
for name, props in reversed(_TAGS):
text_buffer.create_tag(name, **props)
_TAGS = [
('bold', {'weight': pango.WEIGHT_BOLD}),
('italic', {'style': pango.STYLE_ITALIC}),
('underline', {'underline': pango.UNDERLINE_SINGLE}),
]
_TAGS.extend([('family %s' % family, {'family': family})
for family in FAMILIES])
_TAGS.extend([('size %s' % size, {'scale': scale})
for size, scale in SIZE2SCALE.iteritems()])
_TAGS.extend([('justification %s' % align, {'justification': justification})
for align, justification in ALIGN2JUSTIFICATION.iteritems()])
def register_foreground(text_buffer, color):
name = 'foreground %s' % color
tag_table = text_buffer.get_tag_table()
tag = tag_table.lookup(name)
if not tag:
tag = text_buffer.create_tag(name, foreground_gdk=color)
return tag
def _get_tags(content, element):
'Return tag for the element'
tag_table = content.get_tag_table()
if element.tag == 'b':
yield tag_table.lookup('bold')
elif element.tag == 'i':
yield tag_table.lookup('italic')
elif element.tag == 'u':
yield tag_table.lookup('underline')
if 'face' in element.attrib:
tag = tag_table.lookup('family %s' % element.attrib['face'])
if tag:
yield tag
size = element.attrib.get('size')
if size in SIZE2SCALE:
yield tag_table.lookup('size %s' % size)
if 'color' in element.attrib:
yield register_foreground(
content, gtk.gdk.Color(element.attrib['color']))
align = element.attrib.get('align')
if align in ALIGN2JUSTIFICATION:
yield tag_table.lookup('justification %s' % align)
# TODO style background-color
def _get_html(texttag, close=False, div_only=False):
'Return the html tag'
# div alignment is managed at line level
tags = []
attrib = {}
font = attrib['font'] = {}
if texttag.props.weight_set and texttag.props.weight == pango.WEIGHT_BOLD:
tags.append('b')
if texttag.props.style_set and texttag.props.style == pango.STYLE_ITALIC:
tags.append('i')
if (texttag.props.underline_set
and texttag.props.underline == pango.UNDERLINE_SINGLE):
tags.append('u')
if texttag.props.family_set and texttag.props.family:
font['face'] = texttag.props.family
if texttag.props.scale_set and texttag.props.scale != pango.SCALE_MEDIUM:
font['size'] = SCALE2SIZE[texttag.props.scale]
if (texttag.props.foreground_set
and texttag.props.foreground_gdk != gtk.gdk.Color()):
font['color'] = str(texttag.props.foreground_gdk)
# TODO style background-color
if font:
tags.append('font')
if close:
return ''.join('</%s>' % t for t in tags)
return ''.join('<%s %s>' % (t, ' '.join('%s="%s"' % (a, v)
for a, v in attrib.get(t, {}).iteritems()))
for t in tags)
def get_tags(content, start, end):
'Get all tags'
iter_ = start.copy()
tags = set()
while True:
tags.update(iter_.get_tags())
iter_.forward_char()
if iter_.compare(end) > 0:
iter_ = end
if iter_.compare(end) == 0:
break
tags.update(iter_.get_tags())
return tags
def remove_tags(content, start, end, *names):
'Remove all tags starting by names'
tags = get_tags(content, start, end)
for tag in sorted(tags, key=lambda t: t.get_priority()):
for name in names:
if tag.props.name and tag.props.name.startswith(name):
content.remove_tag(tag, start, end)
if __name__ == '__main__':
html = u'''<b>Bold</b>
<i>Italic</i>
<u>Underline</u>
<div><br/></div>
<div align="center">Center</div>
<div><font face="sans" size="6">Sans6<font color="#f00">red</font></font></div>
<div align="center"> <b> <i><u>Title</u></i> </b></div>'''
win = gtk.Window()
win.set_title('HTMLTextBuffer')
def cb(window, event):
print text_buffer.serialize(
text_buffer, MIME, text_buffer.get_start_iter(),
text_buffer.get_end_iter())
gtk.main_quit()
win.connect('delete-event', cb)
vbox = gtk.VBox()
win.add(vbox)
text_buffer = gtk.TextBuffer()
text_view = gtk.TextView(text_buffer)
vbox.pack_start(text_view)
setup_tags(text_buffer)
text_buffer.register_serialize_format(MIME, serialize, None)
text_buffer.register_deserialize_format(MIME, deserialize, None)
text_buffer.deserialize(
text_buffer, MIME, text_buffer.get_start_iter(), html)
result = text_buffer.serialize(
text_buffer, MIME, text_buffer.get_start_iter(),
text_buffer.get_end_iter())
assert normalize_markup(html) == result
win.show_all()
gtk.main()
|