Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9.2, 1.9.3, and 2.0

revmessage: move function to mark up ctx.description() to qtlib

- change hgview-derived util.xml_escape() to Qt.escape().
- add qtlib.descriptionhtmlizer() which returns a function to mark up
description. It'll be updated to accept ui argument in order to support
custom issue link like hgtk.
- add basic doctest.

Changeset 6efb078daff9

Parent 9736501e4df3

by Yuya Nishihara

Changes to 2 files · Browse files at 6efb078daff9 Showing diff from parent 9736501e4df3 Diff from another changeset...

 
10
11
12
 
13
14
15
 
145
146
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
149
150
 
10
11
12
13
14
15
16
 
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
@@ -10,6 +10,7 @@
 import shutil  import stat  import tempfile +import re    from PyQt4.QtCore import *  from PyQt4.QtGui import * @@ -145,6 +146,60 @@
  msg = msg.replace('\n', '<br />')   return '<span style="%s">%s</span>' % (style, msg)   +def descriptionhtmlizer(): + """Return a function to mark up ctx.description() as an HTML + + >>> htmlize = descriptionhtmlizer() + >>> htmlize('foo <bar> \\n& <baz>') + u'foo &lt;bar&gt; \\n&amp; &lt;baz&gt;' + + changeset hash link: + >>> htmlize('foo af50a62e9c20 bar') + u'foo <a href="rev_hash_af50a62e9c20">af50a62e9c20</a> bar' + >>> htmlize('af50a62e9c2040dcdaf61ba6a6400bb45ab56410') # doctest: +ELLIPSIS + u'<a href="rev_hash_af...10">af...10</a>' + + http/https links: + >>> s = htmlize('foo http://example.com:8000/foo?bar=baz&bax#blah') + >>> (s[:63], s[63:]) # doctest: +NORMALIZE_WHITESPACE + (u'foo <a href="http://example.com:8000/foo?bar=baz&amp;bax#blah">', + u'http://example.com:8000/foo?bar=baz&amp;bax#blah</a>') + >>> htmlize('https://example/') + u'<a href="https://example/">https://example/</a>' + """ + # TODO: support tortoisehg.issue.regex and issue.link + csmatch = r'(\b[0-9a-f]{12}(?:[0-9a-f]{28})?\b)' + httpmatch = r'(\b(http|https)://([-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]))' + regexp = r'%s|%s' % (csmatch, httpmatch) + bodyre = re.compile(regexp) + + revhashprefix = 'rev_hash_' + + def htmlize(desc): + """Mark up ctx.description() [localstr] as an HTML [unicode]""" + desc = unicode(Qt.escape(hglib.tounicode(desc))) + + buf = '' + pos = 0 + for m in bodyre.finditer(desc): + a, b = m.span() + if a >= pos: + buf += desc[pos:a] + pos = b + groups = m.groups() + if groups[0]: + cslink = groups[0] + buf += '<a href="%s%s">%s</a>' % (revhashprefix, cslink, cslink) + if groups[1]: + urllink = groups[1] + buf += '<a href="%s">%s</a>' % (urllink, urllink) + if pos < len(desc): + buf += desc[pos:] + + return buf + + return htmlize +  _iconcache = {}    def geticon(name):
 
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
53
54
55
 
 
56
57
58
 
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
 
14
15
16
 
 
17
18
19
 
 
 
 
20
21
 
 
 
 
 
 
22
23
24
 
41
42
43
44
45
46
47
48
 
55
56
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
59
60
61
62
@@ -14,23 +14,11 @@
 # this program; if not, write to the Free Software Foundation, Inc.,  # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.   -import re -  from PyQt4.QtCore import *  from PyQt4.QtGui import *   -from tortoisehg.util.util import xml_escape -from tortoisehg.util.hglib import tounicode - -from tortoisehg.hgqt.i18n import _  from tortoisehg.hgqt import qtlib   -# initialize changeset and url link regex -csmatch = r'(\b[0-9a-f]{12}(?:[0-9a-f]{28})?\b)' -httpmatch = r'(\b(http|https)://([-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]))' -regexp = r'%s|%s' % (csmatch, httpmatch) -bodyre = re.compile(regexp) -  revhashprefix = 'rev_hash_'    class RevMessage(QWidget): @@ -53,6 +41,8 @@
    self.setLayout(vb)   + self._htmlize = qtlib.descriptionhtmlizer() +   self._message.anchorClicked.connect(self.anchorClicked)     def anchorClicked(self, qurl): @@ -65,28 +55,8 @@
    def displayRevision(self, ctx):   self.ctx = ctx - - desc = xml_escape(tounicode(ctx.description())) - - buf = '' - pos = 0 - for m in bodyre.finditer(desc): - a, b = m.span() - if a >= pos: - buf += desc[pos:a] - pos = b - groups = m.groups() - if groups[0]: - cslink = groups[0] - buf += '<a href="%s%s">%s</a>' % (revhashprefix, cslink, cslink) - if groups[1]: - urllink = groups[1] - buf += '<a href="%s">%s</a>' % (urllink, urllink) - if pos < len(desc): - buf += desc[pos:] - - buf = '<pre>%s</pre>' % buf - self._message.setHtml(buf) + self._message.setHtml('<pre>%s</pre>' + % self._htmlize(ctx.description()))     def minimumSizeHint(self):   return QSize(0, 25)