comparison src/cagou/plugins/plugin_wid_chat.py @ 100:d7447c585603

chat: added url detection on text messages
author Goffi <goffi@goffi.org>
date Sat, 31 Dec 2016 12:12:17 +0100
parents 4d8c122b86a6
children 20251d926cc2
comparison
equal deleted inserted replaced
99:f67b9baa81f0 100:d7447c585603
31 from kivy.metrics import dp 31 from kivy.metrics import dp
32 from kivy.utils import escape_markup 32 from kivy.utils import escape_markup
33 from kivy import properties 33 from kivy import properties
34 from sat_frontends.quick_frontend import quick_widgets 34 from sat_frontends.quick_frontend import quick_widgets
35 from sat_frontends.quick_frontend import quick_chat 35 from sat_frontends.quick_frontend import quick_chat
36 from sat_frontends.tools import jid, css_color 36 from sat_frontends.tools import jid, css_color, strings as sat_strings
37 from cagou.core import cagou_widget 37 from cagou.core import cagou_widget
38 from cagou.core.image import Image 38 from cagou.core.image import Image
39 from cagou import G 39 from cagou import G
40 from xml.etree import ElementTree as ET 40 from xml.etree import ElementTree as ET
41 import webbrowser
41 42
42 43
43 PLUGIN_INFO = { 44 PLUGIN_INFO = {
44 "name": _(u"chat"), 45 "name": _(u"chat"),
45 "main": "Chat", 46 "main": "Chat",
59 def __init__(self, text): 60 def __init__(self, text):
60 super(Escape, self).__init__(text) 61 super(Escape, self).__init__(text)
61 62
62 63
63 class SimpleXHTMLWidgetEscapedText(Label): 64 class SimpleXHTMLWidgetEscapedText(Label):
64 pass 65
66 def _addUrlMarkup(self, text):
67 text_elts = []
68 idx = 0
69 links = 0
70 while True:
71 m = sat_strings.RE_URL.search(text[idx:])
72 if m is not None:
73 text_elts.append(escape_markup(m.string[0:m.start()]))
74 link_key = u'link_' + unicode(links)
75 url = m.group()
76 text_elts.append(u'[color=5500ff][ref={link}]{url}[/ref][/color]'.format(
77 link = link_key,
78 url = url
79 ))
80 if not links:
81 self.ref_urls = {link_key: url}
82 else:
83 self.ref_urls[link_key] = url
84 links += 1
85 idx += m.end()
86 else:
87 if links:
88 text_elts.append(escape_markup(text[idx:]))
89 self.markup = True
90 self.text = u''.join(text_elts)
91 break
92
93 def on_text(self, instance, text):
94 # do NOT call the method if self.markup is set
95 # this would result in infinite loop (because self.text
96 # is changed if an URL is found, and in this case markup too)
97 if text and not self.markup:
98 self._addUrlMarkup(text)
99
100 def on_ref_press(self, ref):
101 url = self.ref_urls[ref]
102 webbrowser.open(url)
65 103
66 104
67 class SimpleXHTMLWidgetText(Label): 105 class SimpleXHTMLWidgetText(Label):
68 pass 106 pass
69 107