Mercurial > libervia-desktop-kivy
annotate cagou/core/simple_xhtml.py @ 167:ffef21fd97a2
chat: allow to scroll using side bar + increased bar size
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 28 Apr 2018 15:40:29 +0200 |
parents | cd99f70ea592 |
children | ab3f5173ef5c |
rev | line source |
---|---|
22 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
126 | 5 # Copyright (C) 2016-2018 Jérôme Poisson (goffi@goffi.org) |
22 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 from sat.core import log as logging | |
22 log = logging.getLogger(__name__) | |
57 | 23 from kivy.uix.stacklayout import StackLayout |
24 from kivy.uix.label import Label | |
25 from kivy.utils import escape_markup | |
22 | 26 from kivy import properties |
57 | 27 from xml.etree import ElementTree as ET |
106
9909ed7a7a20
moved SimpleXHTMLWidget to a dedicated module
Goffi <goffi@goffi.org>
parents:
105
diff
changeset
|
28 from sat_frontends.tools import css_color, strings as sat_strings |
9909ed7a7a20
moved SimpleXHTMLWidget to a dedicated module
Goffi <goffi@goffi.org>
parents:
105
diff
changeset
|
29 from cagou.core.image import AsyncImage |
100
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
30 import webbrowser |
22 | 31 |
32 | |
57 | 33 class Escape(unicode): |
34 """Class used to mark that a message need to be escaped""" | |
35 | |
36 def __init__(self, text): | |
37 super(Escape, self).__init__(text) | |
38 | |
39 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
40 class SimpleXHTMLWidgetEscapedText(Label): |
100
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
41 |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
42 def _addUrlMarkup(self, text): |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
43 text_elts = [] |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
44 idx = 0 |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
45 links = 0 |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
46 while True: |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
47 m = sat_strings.RE_URL.search(text[idx:]) |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
48 if m is not None: |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
49 text_elts.append(escape_markup(m.string[0:m.start()])) |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
50 link_key = u'link_' + unicode(links) |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
51 url = m.group() |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
52 text_elts.append(u'[color=5500ff][ref={link}]{url}[/ref][/color]'.format( |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
53 link = link_key, |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
54 url = url |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
55 )) |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
56 if not links: |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
57 self.ref_urls = {link_key: url} |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
58 else: |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
59 self.ref_urls[link_key] = url |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
60 links += 1 |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
61 idx += m.end() |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
62 else: |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
63 if links: |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
64 text_elts.append(escape_markup(text[idx:])) |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
65 self.markup = True |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
66 self.text = u''.join(text_elts) |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
67 break |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
68 |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
69 def on_text(self, instance, text): |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
70 # do NOT call the method if self.markup is set |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
71 # this would result in infinite loop (because self.text |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
72 # is changed if an URL is found, and in this case markup too) |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
73 if text and not self.markup: |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
74 self._addUrlMarkup(text) |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
75 |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
76 def on_ref_press(self, ref): |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
77 url = self.ref_urls[ref] |
d7447c585603
chat: added url detection on text messages
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
78 webbrowser.open(url) |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
79 |
59 | 80 |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
81 class SimpleXHTMLWidgetText(Label): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
82 pass |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
83 |
59 | 84 |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
85 class SimpleXHTMLWidgetImage(AsyncImage): |
59 | 86 # following properties are desired height/width |
87 # i.e. the ones specified in height/width attributes of <img> | |
88 # (or wanted for whatever reason) | |
89 # set to 0 to ignore them | |
90 target_height = properties.NumericProperty() | |
91 target_width = properties.NumericProperty() | |
92 | |
93 def _get_parent_container(self): | |
94 """get parent SimpleXHTMLWidget instance | |
95 | |
96 @param warning(bool): if True display a log.error if nothing found | |
97 @return (SimpleXHTMLWidget, None): found SimpleXHTMLWidget instance | |
98 """ | |
99 parent = self.parent | |
100 while parent and not isinstance(parent, SimpleXHTMLWidget): | |
101 parent = parent.parent | |
102 if parent is None: | |
103 log.error(u"no SimpleXHTMLWidget parent found") | |
104 return parent | |
105 | |
106 def _on_source_load(self, value): | |
107 # this method is called when image is loaded | |
108 super(SimpleXHTMLWidgetImage, self)._on_source_load(value) | |
109 if self.parent is not None: | |
110 container = self._get_parent_container() | |
111 # image is loaded, we need to recalculate size | |
112 self.on_container_width(container, container.width) | |
113 | |
114 def on_container_width(self, container, container_width): | |
115 """adapt size according to container width | |
116 | |
117 called when parent container (SimpleXHTMLWidget) width change | |
118 """ | |
119 target_size = (self.target_width or self.texture.width, self.target_height or self.texture.height) | |
120 padding = container.padding | |
121 padding_h = (padding[0] + padding[2]) if len(padding) == 4 else padding[0] | |
122 width = container_width - padding_h | |
123 if target_size[0] < width: | |
124 self.size = target_size | |
125 else: | |
126 height = width / self.image_ratio | |
127 self.size = (width, height) | |
128 | |
129 def on_parent(self, instance, parent): | |
130 if parent is not None: | |
131 container = self._get_parent_container() | |
132 container.bind(width=self.on_container_width) | |
133 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
134 |
57 | 135 class SimpleXHTMLWidget(StackLayout): |
136 """widget handling simple XHTML parsing""" | |
137 xhtml = properties.StringProperty() | |
138 color = properties.ListProperty([1, 1, 1, 1]) | |
139 # XXX: bold is only used for escaped text | |
140 bold = properties.BooleanProperty(False) | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
141 content_width = properties.NumericProperty(0) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
142 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
143 # text/XHTML input |
57 | 144 |
145 def on_xhtml(self, instance, xhtml): | |
146 """parse xhtml and set content accordingly | |
147 | |
148 if xhtml is an instance of Escape, a Label with not markup | |
149 will be used | |
150 """ | |
151 self.clear_widgets() | |
152 if isinstance(xhtml, Escape): | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
153 label = SimpleXHTMLWidgetEscapedText(text=xhtml, color=self.color) |
57 | 154 self.bind(color=label.setter('color')) |
155 self.bind(bold=label.setter('bold')) | |
156 self.add_widget(label) | |
157 else: | |
158 xhtml = ET.fromstring(xhtml.encode('utf-8')) | |
159 self.current_wid = None | |
160 self.styles = [] | |
161 self._callParseMethod(xhtml) | |
162 | |
163 def escape(self, text): | |
164 """mark that a text need to be escaped (i.e. no markup)""" | |
165 return Escape(text) | |
166 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
167 # sizing |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
168 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
169 def on_width(self, instance, width): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
170 if len(self.children) == 1: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
171 wid = self.children[0] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
172 if isinstance(wid, Label): |
68 | 173 # we have simple text |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
174 try: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
175 full_width = wid._full_width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
176 except AttributeError: |
68 | 177 # on first time, we need the required size |
178 # for the full text, without width limit | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
179 wid.size_hint = (None, None) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
180 wid.texture_update() |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
181 full_width = wid._full_width = wid.texture_size[0] |
68 | 182 |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
183 if full_width > width: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
184 wid.text_size = width, None |
68 | 185 wid.width = width |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
186 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
187 wid.text_size = None, None |
68 | 188 wid.texture_update() |
189 wid.width = wid.texture_size[0] | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
190 self.content_width = wid.width + self.padding[0] + self.padding[2] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
191 else: |
102
20251d926cc2
chat: fixed bad size_hint setting on XHTML
Goffi <goffi@goffi.org>
parents:
100
diff
changeset
|
192 wid.size_hint = (1, None) |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
193 wid.height = 100 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
194 self.content_width = self.width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
195 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
196 self._do_complexe_sizing(width) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
197 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
198 def _do_complexe_sizing(self, width): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
199 try: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
200 self.splitted |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
201 except AttributeError: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
202 # XXX: to make things easier, we split labels in words |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
203 log.debug(u"split start") |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
204 children = self.children[::-1] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
205 self.clear_widgets() |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
206 for child in children: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
207 if isinstance(child, Label): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
208 log.debug(u"label before split: {}".format(child.text)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
209 styles = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
210 tag = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
211 new_text = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
212 current_tag = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
213 current_value = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
214 current_wid = self._createText() |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
215 value = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
216 close = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
217 # we will parse the text and create a new widget |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
218 # on each new word (actually each space) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
219 # FIXME: handle '\n' and other white chars |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
220 for c in child.text: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
221 if tag: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
222 # we are parsing a markup tag |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
223 if c == u']': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
224 current_tag_s = u''.join(current_tag) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
225 current_style = (current_tag_s, u''.join(current_value)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
226 if close: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
227 for idx, s in enumerate(reversed(styles)): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
228 if s[0] == current_tag_s: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
229 del styles[len(styles) - idx - 1] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
230 break |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
231 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
232 styles.append(current_style) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
233 current_tag = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
234 current_value = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
235 tag = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
236 value = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
237 close = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
238 elif c == u'/': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
239 close = True |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
240 elif c == u'=': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
241 value = True |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
242 elif value: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
243 current_value.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
244 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
245 current_tag.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
246 new_text.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
247 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
248 # we are parsing regular text |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
249 if c == u'[': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
250 new_text.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
251 tag = True |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
252 elif c == u' ': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
253 # new word, we do a new widget |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
254 new_text.append(u' ') |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
255 for t, v in reversed(styles): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
256 new_text.append(u'[/{}]'.format(t)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
257 current_wid.text = u''.join(new_text) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
258 new_text = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
259 self.add_widget(current_wid) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
260 log.debug(u"new widget: {}".format(current_wid.text)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
261 current_wid = self._createText() |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
262 for t, v in styles: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
263 new_text.append(u'[{tag}{value}]'.format( |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
264 tag = t, |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
265 value = u'={}'.format(v) if v else u'')) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
266 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
267 new_text.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
268 if current_wid.text: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
269 # we may have a remaining widget after the parsing |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
270 close_styles = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
271 for t, v in reversed(styles): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
272 close_styles.append(u'[/{}]'.format(t)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
273 current_wid.text = u''.join(close_styles) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
274 self.add_widget(current_wid) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
275 log.debug(u"new widget: {}".format(current_wid.text)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
276 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
277 # non Label widgets, we just add them |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
278 self.add_widget(child) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
279 self.splitted = True |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
280 log.debug(u"split OK") |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
281 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
282 # we now set the content width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
283 # FIXME: for now we just use the full width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
284 self.content_width = width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
285 |
57 | 286 # XHTML parsing methods |
287 | |
288 def _callParseMethod(self, e): | |
289 """call the suitable method to parse the element | |
290 | |
291 self.xhtml_[tag] will be called if it exists, else | |
292 self.xhtml_generic will be used | |
293 @param e(ET.Element): element to parse | |
294 """ | |
295 try: | |
296 method = getattr(self, "xhtml_{}".format(e.tag)) | |
297 except AttributeError: | |
298 log.warning(u"Unhandled XHTML tag: {}".format(e.tag)) | |
299 method = self.xhtml_generic | |
300 method(e) | |
301 | |
302 def _addStyle(self, tag, value=None, append_to_list=True): | |
303 """add a markup style to label | |
304 | |
305 @param tag(unicode): markup tag | |
306 @param value(unicode): markup value if suitable | |
307 @param append_to_list(bool): if True style we be added to self.styles | |
308 self.styles is needed to keep track of styles to remove | |
309 should most probably be set to True | |
310 """ | |
311 label = self._getLabel() | |
312 label.text += u'[{tag}{value}]'.format( | |
313 tag = tag, | |
314 value = u'={}'.format(value) if value else '' | |
315 ) | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
316 if append_to_list: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
317 self.styles.append((tag, value)) |
57 | 318 |
319 def _removeStyle(self, tag, remove_from_list=True): | |
320 """remove a markup style from the label | |
321 | |
322 @param tag(unicode): markup tag to remove | |
323 @param remove_from_list(bool): if True, remove from self.styles too | |
324 should most probably be set to True | |
325 """ | |
326 label = self._getLabel() | |
327 label.text += u'[/{tag}]'.format( | |
328 tag = tag | |
329 ) | |
330 if remove_from_list: | |
331 for rev_idx, style in enumerate(reversed(self.styles)): | |
332 if style[0] == tag: | |
333 tag_idx = len(self.styles) - 1 - rev_idx | |
334 del self.styles[tag_idx] | |
335 break | |
336 | |
337 def _getLabel(self): | |
338 """get current Label if it exists, or create a new one""" | |
339 if not isinstance(self.current_wid, Label): | |
340 self._addLabel() | |
341 return self.current_wid | |
342 | |
343 def _addLabel(self): | |
344 """add a new Label | |
345 | |
346 current styles will be closed and reopened if needed | |
347 """ | |
348 self._closeLabel() | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
349 self.current_wid = self._createText() |
57 | 350 for tag, value in self.styles: |
351 self._addStyle(tag, value, append_to_list=False) | |
352 self.add_widget(self.current_wid) | |
353 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
354 def _createText(self): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
355 label = SimpleXHTMLWidgetText(color=self.color, markup=True) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
356 self.bind(color=label.setter('color')) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
357 label.bind(texture_size=label.setter('size')) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
358 return label |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
359 |
57 | 360 def _closeLabel(self): |
361 """close current style tags in current label | |
362 | |
363 needed when you change label to keep style between | |
364 different widgets | |
365 """ | |
366 if isinstance(self.current_wid, Label): | |
367 for tag, value in reversed(self.styles): | |
368 self._removeStyle(tag, remove_from_list=False) | |
369 | |
370 def _parseCSS(self, e): | |
371 """parse CSS found in "style" attribute of element | |
372 | |
373 self._css_styles will be created and contained markup styles added by this method | |
374 @param e(ET.Element): element which may have a "style" attribute | |
375 """ | |
376 styles_limit = len(self.styles) | |
377 styles = e.attrib['style'].split(u';') | |
378 for style in styles: | |
379 try: | |
380 prop, value = style.split(u':') | |
381 except ValueError: | |
382 log.warning(u"can't parse style: {}".format(style)) | |
383 continue | |
384 prop = prop.strip().replace(u'-', '_') | |
385 value = value.strip() | |
386 try: | |
387 method = getattr(self, "css_{}".format(prop)) | |
388 except AttributeError: | |
389 log.warning(u"Unhandled CSS: {}".format(prop)) | |
390 else: | |
391 method(e, value) | |
392 self._css_styles = self.styles[styles_limit:] | |
393 | |
394 def _closeCSS(self): | |
395 """removed CSS styles | |
396 | |
397 styles in self._css_styles will be removed | |
398 and the attribute will be deleted | |
399 """ | |
400 for tag, dummy in reversed(self._css_styles): | |
401 self._removeStyle(tag) | |
402 del self._css_styles | |
403 | |
404 def xhtml_generic(self, elem, style=True, markup=None): | |
405 """generic method for adding HTML elements | |
406 | |
407 this method handle content, style and children parsing | |
408 @param elem(ET.Element): element to add | |
409 @param style(bool): if True handle style attribute (CSS) | |
410 @param markup(tuple[unicode, (unicode, None)], None): kivy markup to use | |
411 """ | |
412 # we first add markup and CSS style | |
413 if markup is not None: | |
414 if isinstance(markup, basestring): | |
415 tag, value = markup, None | |
416 else: | |
417 tag, value = markup | |
418 self._addStyle(tag, value) | |
419 style_ = 'style' in elem.attrib and style | |
420 if style_: | |
421 self._parseCSS(elem) | |
422 | |
423 # then content | |
424 if elem.text: | |
425 self._getLabel().text += escape_markup(elem.text) | |
426 | |
427 # we parse the children | |
428 for child in elem: | |
429 self._callParseMethod(child) | |
430 | |
431 # closing CSS style and markup | |
432 if style_: | |
433 self._closeCSS() | |
434 if markup is not None: | |
435 self._removeStyle(tag) | |
436 | |
437 # and the tail, which is regular text | |
438 if elem.tail: | |
439 self._getLabel().text += escape_markup(elem.tail) | |
440 | |
441 # method handling XHTML elements | |
442 | |
443 def xhtml_br(self, elem): | |
444 label = self._getLabel() | |
445 label.text+='\n' | |
446 self.xhtml_generic(style=False) | |
447 | |
448 def xhtml_em(self, elem): | |
449 self.xhtml_generic(elem, markup='i') | |
450 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
451 def xhtml_img(self, elem): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
452 try: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
453 src = elem.attrib['src'] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
454 except KeyError: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
455 log.warning(u"<img> element without src: {}".format(ET.tostring(elem))) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
456 return |
59 | 457 try: |
458 target_height = int(elem.get(u'height', 0)) | |
459 except ValueError: | |
460 log.warning(u"Can't parse image height: {}".format(elem.get(u'height'))) | |
461 target_height = 0 | |
462 try: | |
463 target_width = int(elem.get(u'width', 0)) | |
464 except ValueError: | |
465 log.warning(u"Can't parse image width: {}".format(elem.get(u'width'))) | |
466 target_width = 0 | |
467 | |
468 img = SimpleXHTMLWidgetImage(source=src, target_height=target_height, target_width=target_width) | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
469 self.current_wid = img |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
470 self.add_widget(img) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
471 |
57 | 472 def xhtml_p(self, elem): |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
473 if isinstance(self.current_wid, Label): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
474 self.current_wid.text+="\n\n" |
57 | 475 self.xhtml_generic(elem) |
476 | |
477 def xhtml_span(self, elem): | |
478 self.xhtml_generic(elem) | |
479 | |
480 def xhtml_strong(self, elem): | |
481 self.xhtml_generic(elem, markup='b') | |
482 | |
483 # methods handling CSS properties | |
484 | |
485 def css_color(self, elem, value): | |
486 self._addStyle(u"color", css_color.parse(value)) | |
487 | |
488 def css_text_decoration(self, elem, value): | |
489 if value == u'underline': | |
490 log.warning(u"{} not handled yet, it needs Kivy 1.9.2 to be released".format(value)) | |
491 # FIXME: activate when 1.9.2 is out | |
492 # self._addStyle('u') | |
493 elif value == u'line-through': | |
494 log.warning(u"{} not handled yet, it needs Kivy 1.9.2 to be released".format(value)) | |
495 # FIXME: activate when 1.9.2 is out | |
496 # self._addStyle('s') | |
497 else: | |
498 log.warning(u"unhandled text decoration: {}".format(value)) |