author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 17 May 2010 17:31:29 +0200 | |
branch | stable |
changeset 5531 | 0ce62ac0d1d7 |
parent 5424 | 8ecbcbff9777 |
child 5730 | 784025c15a3c |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
2 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
3 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
4 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
5 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
6 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
7 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
8 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
9 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
10 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
11 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
12 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
14 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
15 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
16 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
17 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
18 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 19 |
"""user interface libraries |
20 |
||
21 |
contains some functions designed to help implementation of cubicweb user interface |
|
22 |
||
23 |
""" |
|
24 |
__docformat__ = "restructuredtext en" |
|
25 |
||
26 |
import csv |
|
27 |
import re |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1623
diff
changeset
|
28 |
from StringIO import StringIO |
0 | 29 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
30 |
from logilab.mtconverter import xml_escape, html_unescape |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
31 |
from logilab.common.date import ustrftime |
0 | 32 |
|
33 |
||
34 |
def rql_for_eid(eid): |
|
35 |
"""return the rql query necessary to fetch entity with the given eid. This |
|
36 |
function should only be used to generate link with rql inside, not to give |
|
37 |
to cursor.execute (in which case you won't benefit from rql cache). |
|
38 |
||
39 |
:Parameters: |
|
40 |
- `eid`: the eid of the entity we should search |
|
41 |
:rtype: str |
|
42 |
:return: the rql query |
|
43 |
""" |
|
44 |
return 'Any X WHERE X eid %s' % eid |
|
45 |
||
46 |
||
3212
07d11bacfefe
displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3165
diff
changeset
|
47 |
def printable_value(req, attrtype, value, props=None, displaytime=True): |
0 | 48 |
"""return a displayable value (i.e. unicode string)""" |
49 |
if value is None or attrtype == 'Bytes': |
|
50 |
return u'' |
|
51 |
if attrtype == 'String': |
|
52 |
# don't translate empty value if you don't want strange results |
|
53 |
if props is not None and value and props.get('internationalizable'): |
|
54 |
return req._(value) |
|
55 |
return value |
|
56 |
if attrtype == 'Date': |
|
57 |
return ustrftime(value, req.property_value('ui.date-format')) |
|
58 |
if attrtype == 'Time': |
|
59 |
return ustrftime(value, req.property_value('ui.time-format')) |
|
60 |
if attrtype == 'Datetime': |
|
3212
07d11bacfefe
displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3165
diff
changeset
|
61 |
if displaytime: |
07d11bacfefe
displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3165
diff
changeset
|
62 |
return ustrftime(value, req.property_value('ui.datetime-format')) |
07d11bacfefe
displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3165
diff
changeset
|
63 |
return ustrftime(value, req.property_value('ui.date-format')) |
0 | 64 |
if attrtype == 'Boolean': |
65 |
if value: |
|
66 |
return req._('yes') |
|
67 |
return req._('no') |
|
68 |
if attrtype == 'Float': |
|
69 |
value = req.property_value('ui.float-format') % value |
|
70 |
return unicode(value) |
|
71 |
||
72 |
||
73 |
# text publishing ############################################################# |
|
74 |
||
75 |
try: |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
76 |
from cubicweb.ext.rest import rest_publish # pylint: disable-msg=W0611 |
0 | 77 |
except ImportError: |
78 |
def rest_publish(entity, data): |
|
79 |
"""default behaviour if docutils was not found""" |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
80 |
return xml_escape(data) |
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
81 |
|
0 | 82 |
TAG_PROG = re.compile(r'</?.*?>', re.U) |
83 |
def remove_html_tags(text): |
|
84 |
"""Removes HTML tags from text |
|
85 |
||
86 |
>>> remove_html_tags('<td>hi <a href="http://www.google.fr">world</a></td>') |
|
87 |
'hi world' |
|
88 |
>>> |
|
89 |
""" |
|
90 |
return TAG_PROG.sub('', text) |
|
91 |
||
92 |
||
93 |
REF_PROG = re.compile(r"<ref\s+rql=([\'\"])([^\1]*?)\1\s*>([^<]*)</ref>", re.U) |
|
94 |
def _subst_rql(view, obj): |
|
95 |
delim, rql, descr = obj.groups() |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
96 |
return u'<a href="%s">%s</a>' % (view._cw.build_url(rql=rql), descr) |
0 | 97 |
|
98 |
def html_publish(view, text): |
|
99 |
"""replace <ref rql=''> links by <a href="...">""" |
|
100 |
if not text: |
|
101 |
return u'' |
|
102 |
return REF_PROG.sub(lambda obj, view=view:_subst_rql(view, obj), text) |
|
103 |
||
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
104 |
# fallback implementation, nicer one defined below if lxml is available |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
105 |
def soup2xhtml(data, encoding): |
2208
1c73148ed912
normalize line breaks (actually fix https://www.logilab.net/cwo/ticket/343754)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
106 |
# normalize line break |
1c73148ed912
normalize line breaks (actually fix https://www.logilab.net/cwo/ticket/343754)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
107 |
# see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1 |
1c73148ed912
normalize line breaks (actually fix https://www.logilab.net/cwo/ticket/343754)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
108 |
return u'\n'.join(data.splitlines()) |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
109 |
|
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
110 |
# fallback implementation, nicer one defined below if lxml> 2.0 is available |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
111 |
def safe_cut(text, length): |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
112 |
"""returns a string of length <length> based on <text>, removing any html |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
113 |
tags from given text if cut is necessary.""" |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
114 |
if text is None: |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
115 |
return u'' |
362
a6a319f000c3
use mtconverter's html_unescape rather than saxutils' escape to deal with any html entity
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
350
diff
changeset
|
116 |
noenttext = html_unescape(text) |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
117 |
text_nohtml = remove_html_tags(noenttext) |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
118 |
# try to keep html tags if text is short enough |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
119 |
if len(text_nohtml) <= length: |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
120 |
return text |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
121 |
# else if un-tagged text is too long, cut it |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
122 |
return xml_escape(text_nohtml[:length] + u'...') |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
123 |
|
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
124 |
fallback_safe_cut = safe_cut |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
125 |
|
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
126 |
|
0 | 127 |
try: |
128 |
from lxml import etree |
|
228
27b958dc72ae
[lxml] lxml version < 2 does not provide an iter method on some elements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
165
diff
changeset
|
129 |
except (ImportError, AttributeError): |
3352
83aabc5e3de3
a case for tidy : for your consideration
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3212
diff
changeset
|
130 |
# gae environment: lxml not available |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
131 |
pass |
0 | 132 |
else: |
133 |
||
134 |
def soup2xhtml(data, encoding): |
|
135 |
"""tidy (at least try) html soup and return the result |
|
136 |
Note: the function considers a string with no surrounding tag as valid |
|
137 |
if <div>`data`</div> can be parsed by an XML parser |
|
138 |
""" |
|
2208
1c73148ed912
normalize line breaks (actually fix https://www.logilab.net/cwo/ticket/343754)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
139 |
# normalize line break |
1c73148ed912
normalize line breaks (actually fix https://www.logilab.net/cwo/ticket/343754)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
140 |
# see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1 |
1c73148ed912
normalize line breaks (actually fix https://www.logilab.net/cwo/ticket/343754)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
141 |
data = u'\n'.join(data.splitlines()) |
1c73148ed912
normalize line breaks (actually fix https://www.logilab.net/cwo/ticket/343754)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
142 |
# XXX lxml 1.1 support still needed ? |
0 | 143 |
xmltree = etree.HTML('<div>%s</div>' % data) |
144 |
# NOTE: lxml 1.1 (etch platforms) doesn't recognize |
|
145 |
# the encoding=unicode parameter (lxml 2.0 does), this is |
|
146 |
# why we specify an encoding and re-decode to unicode later |
|
147 |
body = etree.tostring(xmltree[0], encoding=encoding) |
|
148 |
# remove <body> and </body> and decode to unicode |
|
149 |
return body[11:-13].decode(encoding) |
|
150 |
||
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
151 |
if hasattr(etree.HTML('<div>test</div>'), 'iter'): |
165 | 152 |
|
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
153 |
def safe_cut(text, length): |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
154 |
"""returns an html document of length <length> based on <text>, |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
155 |
and cut is necessary. |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
156 |
""" |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
157 |
if text is None: |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
158 |
return u'' |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
159 |
dom = etree.HTML(text) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
160 |
curlength = 0 |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
161 |
add_ellipsis = False |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
162 |
for element in dom.iter(): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
163 |
if curlength >= length: |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
164 |
parent = element.getparent() |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
165 |
parent.remove(element) |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
166 |
if curlength == length and (element.text or element.tail): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
167 |
add_ellipsis = True |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
168 |
else: |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
169 |
if element.text is not None: |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
170 |
element.text = cut(element.text, length - curlength) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
171 |
curlength += len(element.text) |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
172 |
if element.tail is not None: |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
173 |
if curlength < length: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
174 |
element.tail = cut(element.tail, length - curlength) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
175 |
curlength += len(element.tail) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
176 |
elif curlength == length: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
177 |
element.tail = '...' |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
178 |
else: |
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
179 |
element.tail = '' |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
180 |
text = etree.tounicode(dom[0])[6:-7] # remove wrapping <body></body> |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
181 |
if add_ellipsis: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
182 |
return text + u'...' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
183 |
return text |
1157
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
184 |
|
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
185 |
def text_cut(text, nbwords=30, gotoperiod=True): |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
186 |
"""from the given plain text, return a text with at least <nbwords> words, |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
187 |
trying to go to the end of the current sentence. |
277
a11a3c231050
fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
228
diff
changeset
|
188 |
|
1157
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
189 |
:param nbwords: the minimum number of words required |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
190 |
:param gotoperiod: specifies if the function should try to go to |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
191 |
the first period after the cut (i.e. finish |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
192 |
the sentence if possible) |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
193 |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
194 |
Note that spaces are normalized. |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
195 |
""" |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
196 |
if text is None: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
197 |
return u'' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
198 |
words = text.split() |
927
bfcc610c3d5e
text_cut must return unicode not string
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
525
diff
changeset
|
199 |
text = u' '.join(words) # normalize spaces |
1157
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
200 |
textlength = minlength = len(' '.join(words[:nbwords])) |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
201 |
if gotoperiod: |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
202 |
textlength = text.find('.', minlength) + 1 |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
203 |
if textlength == 0: # no period found |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
204 |
textlength = minlength |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
205 |
return text[:textlength] |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
206 |
|
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
207 |
def cut(text, length): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
208 |
"""returns a string of a maximum length <length> based on <text> |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
209 |
(approximatively, since if text has been cut, '...' is added to the end of the string, |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
210 |
resulting in a string of len <length> + 3) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
211 |
""" |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
212 |
if text is None: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
213 |
return u'' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
214 |
if len(text) <= length: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
215 |
return text |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
216 |
# else if un-tagged text is too long, cut it |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
217 |
return text[:length] + u'...' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
218 |
|
165 | 219 |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
220 |
|
0 | 221 |
# HTML generation helper functions ############################################ |
222 |
||
2398
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
223 |
HTML4_EMPTY_TAGS = frozenset(('base', 'meta', 'link', 'hr', 'br', 'param', |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
224 |
'img', 'area', 'input', 'col')) |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
225 |
|
2516
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
226 |
def sgml_attributes(attrs): |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
227 |
return u' '.join(u'%s="%s"' % (attr, xml_escape(unicode(value))) |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
228 |
for attr, value in sorted(attrs.items()) |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
229 |
if value is not None) |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
230 |
|
1623 | 231 |
def simple_sgml_tag(tag, content=None, escapecontent=True, **attrs): |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
232 |
"""generation of a simple sgml tag (eg without children tags) easier |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
233 |
|
2399
68799e25f893
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
234 |
content and attri butes will be escaped |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
235 |
""" |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
236 |
value = u'<%s' % tag |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
237 |
if attrs: |
980
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
238 |
try: |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
239 |
attrs['class'] = attrs.pop('klass') |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
240 |
except KeyError: |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
241 |
pass |
2516
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
242 |
value += u' ' + sgml_attributes(attrs) |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
243 |
if content: |
1623 | 244 |
if escapecontent: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
245 |
content = xml_escape(unicode(content)) |
1623 | 246 |
value += u'>%s</%s>' % (content, tag) |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
247 |
else: |
2398
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
248 |
if tag in HTML4_EMPTY_TAGS: |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
249 |
value += u' />' |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
250 |
else: |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
251 |
value += u'></%s>' % tag |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
252 |
return value |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
253 |
|
0 | 254 |
def tooltipize(text, tooltip, url=None): |
255 |
"""make an HTML tooltip""" |
|
256 |
url = url or '#' |
|
257 |
return u'<a href="%s" title="%s">%s</a>' % (url, tooltip, text) |
|
258 |
||
259 |
def toggle_action(nodeid): |
|
260 |
"""builds a HTML link that uses the js toggleVisibility function""" |
|
261 |
return u"javascript: toggleVisibility('%s')" % nodeid |
|
262 |
||
263 |
def toggle_link(nodeid, label): |
|
264 |
"""builds a HTML link that uses the js toggleVisibility function""" |
|
265 |
return u'<a href="%s">%s</a>' % (toggle_action(nodeid), label) |
|
266 |
||
267 |
||
268 |
def ureport_as_html(layout): |
|
269 |
from logilab.common.ureports import HTMLWriter |
|
270 |
formater = HTMLWriter(True) |
|
271 |
stream = StringIO() #UStringIO() don't want unicode assertion |
|
272 |
formater.format(layout, stream) |
|
273 |
res = stream.getvalue() |
|
274 |
if isinstance(res, str): |
|
275 |
res = unicode(res, 'UTF8') |
|
276 |
return res |
|
277 |
||
278 |
# traceback formatting ######################################################## |
|
279 |
||
280 |
import traceback |
|
281 |
||
282 |
def rest_traceback(info, exception): |
|
283 |
"""return a ReST formated traceback""" |
|
284 |
res = [u'Traceback\n---------\n::\n'] |
|
285 |
for stackentry in traceback.extract_tb(info[2]): |
|
286 |
res.append(u'\tFile %s, line %s, function %s' % tuple(stackentry[:3])) |
|
287 |
if stackentry[3]: |
|
288 |
res.append(u'\t %s' % stackentry[3].decode('utf-8', 'replace')) |
|
289 |
res.append(u'\n') |
|
290 |
try: |
|
291 |
res.append(u'\t Error: %s\n' % exception) |
|
292 |
except: |
|
293 |
pass |
|
294 |
return u'\n'.join(res) |
|
295 |
||
296 |
||
297 |
def html_traceback(info, exception, title='', |
|
298 |
encoding='ISO-8859-1', body=''): |
|
299 |
""" return an html formatted traceback from python exception infos. |
|
300 |
""" |
|
301 |
tcbk = info[2] |
|
302 |
stacktb = traceback.extract_tb(tcbk) |
|
303 |
strings = [] |
|
304 |
if body: |
|
305 |
strings.append(u'<div class="error_body">') |
|
306 |
# FIXME |
|
307 |
strings.append(body) |
|
308 |
strings.append(u'</div>') |
|
309 |
if title: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
310 |
strings.append(u'<h1 class="error">%s</h1>'% xml_escape(title)) |
0 | 311 |
try: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
312 |
strings.append(u'<p class="error">%s</p>' % xml_escape(str(exception)).replace("\n","<br />")) |
0 | 313 |
except UnicodeError: |
314 |
pass |
|
315 |
strings.append(u'<div class="error_traceback">') |
|
316 |
for index, stackentry in enumerate(stacktb): |
|
317 |
strings.append(u'<b>File</b> <b class="file">%s</b>, <b>line</b> ' |
|
318 |
u'<b class="line">%s</b>, <b>function</b> ' |
|
319 |
u'<b class="function">%s</b>:<br/>'%( |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
320 |
xml_escape(stackentry[0]), stackentry[1], xml_escape(stackentry[2]))) |
0 | 321 |
if stackentry[3]: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
322 |
string = xml_escape(stackentry[3]).decode('utf-8', 'replace') |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2516
diff
changeset
|
323 |
strings.append(u'  %s<br/>\n' % (string)) |
0 | 324 |
# add locals info for each entry |
325 |
try: |
|
326 |
local_context = tcbk.tb_frame.f_locals |
|
327 |
html_info = [] |
|
328 |
chars = 0 |
|
329 |
for name, value in local_context.iteritems(): |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
330 |
value = xml_escape(repr(value)) |
0 | 331 |
info = u'<span class="name">%s</span>=%s, ' % (name, value) |
332 |
line_length = len(name) + len(value) |
|
333 |
chars += line_length |
|
334 |
# 150 is the result of *years* of research ;-) (CSS might be helpful here) |
|
335 |
if chars > 150: |
|
336 |
info = u'<br/>' + info |
|
337 |
chars = line_length |
|
338 |
html_info.append(info) |
|
339 |
boxid = 'ctxlevel%d' % index |
|
340 |
strings.append(u'[%s]' % toggle_link(boxid, '+')) |
|
341 |
strings.append(u'<div id="%s" class="pycontext hidden">%s</div>' % |
|
342 |
(boxid, ''.join(html_info))) |
|
343 |
tcbk = tcbk.tb_next |
|
344 |
except Exception: |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
345 |
pass # doesn't really matter if we have no context info |
0 | 346 |
strings.append(u'</div>') |
347 |
return '\n'.join(strings) |
|
348 |
||
349 |
# csv files / unicode support ################################################# |
|
350 |
||
351 |
class UnicodeCSVWriter: |
|
352 |
"""proxies calls to csv.writer.writerow to be able to deal with unicode""" |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
353 |
|
0 | 354 |
def __init__(self, wfunc, encoding, **kwargs): |
355 |
self.writer = csv.writer(self, **kwargs) |
|
356 |
self.wfunc = wfunc |
|
357 |
self.encoding = encoding |
|
358 |
||
359 |
def write(self, data): |
|
360 |
self.wfunc(data) |
|
361 |
||
362 |
def writerow(self, row): |
|
363 |
csvrow = [] |
|
364 |
for elt in row: |
|
365 |
if isinstance(elt, unicode): |
|
366 |
csvrow.append(elt.encode(self.encoding)) |
|
367 |
else: |
|
368 |
csvrow.append(str(elt)) |
|
369 |
self.writer.writerow(csvrow) |
|
370 |
||
371 |
def writerows(self, rows): |
|
372 |
for row in rows: |
|
373 |
self.writerow(row) |
|
374 |
||
375 |
||
376 |
# some decorators ############################################################# |
|
377 |
||
378 |
class limitsize(object): |
|
379 |
def __init__(self, maxsize): |
|
380 |
self.maxsize = maxsize |
|
381 |
||
382 |
def __call__(self, function): |
|
383 |
def newfunc(*args, **kwargs): |
|
384 |
ret = function(*args, **kwargs) |
|
385 |
if isinstance(ret, basestring): |
|
386 |
return ret[:self.maxsize] |
|
387 |
return ret |
|
388 |
return newfunc |
|
389 |
||
390 |
||
391 |
def htmlescape(function): |
|
392 |
def newfunc(*args, **kwargs): |
|
393 |
ret = function(*args, **kwargs) |
|
394 |
assert isinstance(ret, basestring) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
395 |
return xml_escape(ret) |
0 | 396 |
return newfunc |