author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 20 Aug 2010 08:21:15 +0200 | |
branch | stable |
changeset 6126 | aca6a2c357fd |
parent 6106 | 1e6d93f70d14 |
child 6491 | ee9a10b6620e |
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 |
||
5730
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
21 |
contains some functions designed to help implementation of cubicweb user |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
22 |
interface. |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
23 |
""" |
0 | 24 |
|
25 |
__docformat__ = "restructuredtext en" |
|
26 |
||
27 |
import csv |
|
28 |
import re |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1623
diff
changeset
|
29 |
from StringIO import StringIO |
0 | 30 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
31 |
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
|
32 |
from logilab.common.date import ustrftime |
0 | 33 |
|
5949
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
34 |
from cubicweb.utils import json_dumps |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
35 |
|
0 | 36 |
|
37 |
def rql_for_eid(eid): |
|
38 |
"""return the rql query necessary to fetch entity with the given eid. This |
|
39 |
function should only be used to generate link with rql inside, not to give |
|
40 |
to cursor.execute (in which case you won't benefit from rql cache). |
|
41 |
||
42 |
:Parameters: |
|
43 |
- `eid`: the eid of the entity we should search |
|
44 |
:rtype: str |
|
45 |
:return: the rql query |
|
46 |
""" |
|
47 |
return 'Any X WHERE X eid %s' % eid |
|
48 |
||
6106
1e6d93f70d14
[selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5951
diff
changeset
|
49 |
def eid_param(name, eid): |
1e6d93f70d14
[selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5951
diff
changeset
|
50 |
assert eid is not None |
1e6d93f70d14
[selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5951
diff
changeset
|
51 |
if eid is None: |
1e6d93f70d14
[selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5951
diff
changeset
|
52 |
eid = '' |
1e6d93f70d14
[selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5951
diff
changeset
|
53 |
return '%s:%s' % (name, eid) |
0 | 54 |
|
3212
07d11bacfefe
displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3165
diff
changeset
|
55 |
def printable_value(req, attrtype, value, props=None, displaytime=True): |
0 | 56 |
"""return a displayable value (i.e. unicode string)""" |
57 |
if value is None or attrtype == 'Bytes': |
|
58 |
return u'' |
|
59 |
if attrtype == 'String': |
|
60 |
# don't translate empty value if you don't want strange results |
|
61 |
if props is not None and value and props.get('internationalizable'): |
|
62 |
return req._(value) |
|
63 |
return value |
|
64 |
if attrtype == 'Date': |
|
65 |
return ustrftime(value, req.property_value('ui.date-format')) |
|
66 |
if attrtype == 'Time': |
|
67 |
return ustrftime(value, req.property_value('ui.time-format')) |
|
68 |
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
|
69 |
if displaytime: |
07d11bacfefe
displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3165
diff
changeset
|
70 |
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
|
71 |
return ustrftime(value, req.property_value('ui.date-format')) |
0 | 72 |
if attrtype == 'Boolean': |
73 |
if value: |
|
74 |
return req._('yes') |
|
75 |
return req._('no') |
|
76 |
if attrtype == 'Float': |
|
77 |
value = req.property_value('ui.float-format') % value |
|
78 |
return unicode(value) |
|
79 |
||
80 |
||
81 |
# text publishing ############################################################# |
|
82 |
||
83 |
try: |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
84 |
from cubicweb.ext.rest import rest_publish # pylint: disable-msg=W0611 |
0 | 85 |
except ImportError: |
86 |
def rest_publish(entity, data): |
|
87 |
"""default behaviour if docutils was not found""" |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
88 |
return xml_escape(data) |
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
89 |
|
0 | 90 |
TAG_PROG = re.compile(r'</?.*?>', re.U) |
91 |
def remove_html_tags(text): |
|
92 |
"""Removes HTML tags from text |
|
93 |
||
94 |
>>> remove_html_tags('<td>hi <a href="http://www.google.fr">world</a></td>') |
|
95 |
'hi world' |
|
96 |
>>> |
|
97 |
""" |
|
98 |
return TAG_PROG.sub('', text) |
|
99 |
||
100 |
||
101 |
REF_PROG = re.compile(r"<ref\s+rql=([\'\"])([^\1]*?)\1\s*>([^<]*)</ref>", re.U) |
|
102 |
def _subst_rql(view, obj): |
|
103 |
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
|
104 |
return u'<a href="%s">%s</a>' % (view._cw.build_url(rql=rql), descr) |
0 | 105 |
|
106 |
def html_publish(view, text): |
|
107 |
"""replace <ref rql=''> links by <a href="...">""" |
|
108 |
if not text: |
|
109 |
return u'' |
|
110 |
return REF_PROG.sub(lambda obj, view=view:_subst_rql(view, obj), text) |
|
111 |
||
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
|
112 |
# 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
|
113 |
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
|
114 |
# 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
|
115 |
# 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
|
116 |
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
|
117 |
|
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 |
# 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
|
119 |
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
|
120 |
"""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
|
121 |
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
|
122 |
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
|
123 |
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
|
124 |
noenttext = html_unescape(text) |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
125 |
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
|
126 |
# 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
|
127 |
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
|
128 |
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
|
129 |
# 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
|
130 |
return xml_escape(text_nohtml[:length] + u'...') |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
131 |
|
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
132 |
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
|
133 |
|
5730
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
134 |
REM_ROOT_HTML_TAGS = re.compile('</(body|html)>', re.U) |
0 | 135 |
try: |
136 |
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
|
137 |
except (ImportError, AttributeError): |
3352
83aabc5e3de3
a case for tidy : for your consideration
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3212
diff
changeset
|
138 |
# 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
|
139 |
pass |
0 | 140 |
else: |
141 |
||
142 |
def soup2xhtml(data, encoding): |
|
143 |
"""tidy (at least try) html soup and return the result |
|
5730
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
144 |
|
0 | 145 |
Note: the function considers a string with no surrounding tag as valid |
146 |
if <div>`data`</div> can be parsed by an XML parser |
|
147 |
""" |
|
5730
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
148 |
# remove spurious </body> and </html> tags, then normalize line break |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
149 |
# (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1) |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
150 |
data = REM_ROOT_HTML_TAGS.sub('', u'\n'.join(data.splitlines())) |
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
|
151 |
# XXX lxml 1.1 support still needed ? |
0 | 152 |
xmltree = etree.HTML('<div>%s</div>' % data) |
153 |
# NOTE: lxml 1.1 (etch platforms) doesn't recognize |
|
154 |
# the encoding=unicode parameter (lxml 2.0 does), this is |
|
155 |
# why we specify an encoding and re-decode to unicode later |
|
156 |
body = etree.tostring(xmltree[0], encoding=encoding) |
|
157 |
# remove <body> and </body> and decode to unicode |
|
5730
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
158 |
snippet = body[6:-7].decode(encoding) |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
159 |
# take care to bad xhtml (for instance starting with </div>) which |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
160 |
# may mess with the <div> we added below. Only remove it if it's |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
161 |
# still there... |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
162 |
if snippet.startswith('<div>') and snippet.endswith('</div>'): |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
163 |
snippet = snippet[5:-6] |
784025c15a3c
[xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
164 |
return snippet |
0 | 165 |
|
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
|
166 |
if hasattr(etree.HTML('<div>test</div>'), 'iter'): |
165 | 167 |
|
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 |
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
|
169 |
"""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
|
170 |
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
|
171 |
""" |
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 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
|
173 |
return u'' |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
174 |
dom = etree.HTML(text) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
175 |
curlength = 0 |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
176 |
add_ellipsis = False |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
177 |
for element in dom.iter(): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
178 |
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
|
179 |
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
|
180 |
parent.remove(element) |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
181 |
if curlength == length and (element.text or element.tail): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
182 |
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
|
183 |
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
|
184 |
if element.text is not None: |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
185 |
element.text = cut(element.text, length - curlength) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
186 |
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
|
187 |
if element.tail is not None: |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
188 |
if curlength < length: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
189 |
element.tail = cut(element.tail, length - curlength) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
190 |
curlength += len(element.tail) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
191 |
elif curlength == length: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
192 |
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
|
193 |
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
|
194 |
element.tail = '' |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
195 |
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
|
196 |
if add_ellipsis: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
197 |
return text + u'...' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
198 |
return text |
1157
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
199 |
|
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
200 |
def text_cut(text, nbwords=30, gotoperiod=True): |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
201 |
"""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
|
202 |
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
|
203 |
|
1157
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
204 |
: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
|
205 |
: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
|
206 |
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
|
207 |
the sentence if possible) |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
208 |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
209 |
Note that spaces are normalized. |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
210 |
""" |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
211 |
if text is None: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
212 |
return u'' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
213 |
words = text.split() |
927
bfcc610c3d5e
text_cut must return unicode not string
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
525
diff
changeset
|
214 |
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
|
215 |
textlength = minlength = len(' '.join(words[:nbwords])) |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
216 |
if gotoperiod: |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
217 |
textlength = text.find('.', minlength) + 1 |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
218 |
if textlength == 0: # no period found |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
219 |
textlength = minlength |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
220 |
return text[:textlength] |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
221 |
|
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
222 |
def cut(text, length): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
223 |
"""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
|
224 |
(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
|
225 |
resulting in a string of len <length> + 3) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
226 |
""" |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
227 |
if text is None: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
228 |
return u'' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
229 |
if len(text) <= length: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
230 |
return text |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
231 |
# 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
|
232 |
return text[:length] + u'...' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
233 |
|
165 | 234 |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
235 |
|
0 | 236 |
# HTML generation helper functions ############################################ |
237 |
||
5949
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
238 |
class _JSId(object): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
239 |
def __init__(self, id, parent=None): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
240 |
self.id = id |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
241 |
self.parent = parent |
5951
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
242 |
def __unicode__(self): |
5949
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
243 |
if self.parent: |
5951
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
244 |
return u'%s.%s' % (self.parent, self.id) |
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
245 |
return unicode(self.id) |
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
246 |
def __str__(self): |
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
247 |
return unicode(self).encode('utf8') |
5949
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
248 |
def __getattr__(self, attr): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
249 |
return _JSId(attr, self) |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
250 |
def __call__(self, *args): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
251 |
return _JSCallArgs(args, self) |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
252 |
|
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
253 |
class _JSCallArgs(_JSId): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
254 |
def __init__(self, args, parent=None): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
255 |
assert isinstance(args, tuple) |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
256 |
self.args = args |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
257 |
self.parent = parent |
5951
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
258 |
def __unicode__(self): |
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
259 |
args = u','.join(json_dumps(arg) for arg in self.args) |
5949
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
260 |
if self.parent: |
5951
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
261 |
return u'%s(%s)' % (self.parent, args) |
5949
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
262 |
return args |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
263 |
|
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
264 |
class _JS(object): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
265 |
def __getattr__(self, attr): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
266 |
return _JSId(attr) |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
267 |
|
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
268 |
"""magic object to return strings suitable to call some javascript function with |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
269 |
the given arguments (which should be correctly typed). |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
270 |
|
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
271 |
>>> str(js.pouet(1, "2")) |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
272 |
'pouet(1,"2")' |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
273 |
>>> str(js.cw.pouet(1, "2")) |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
274 |
'cw.pouet(1,"2")' |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
275 |
>>> str(js.cw.pouet(1, "2").pouet(None)) |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
276 |
'cw.pouet(1,"2").pouet(null)') |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
277 |
""" |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
278 |
js = _JS() |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
279 |
|
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
280 |
def domid(string): |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
281 |
"""return a valid DOM id from a string (should also be usable in jQuery |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
282 |
search expression...) |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
283 |
""" |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
284 |
return string.replace('.', '_').replace('-', '_') |
2a273c896a38
[box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5730
diff
changeset
|
285 |
|
2398
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
286 |
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
|
287 |
'img', 'area', 'input', 'col')) |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
288 |
|
2516
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
289 |
def sgml_attributes(attrs): |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
290 |
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
|
291 |
for attr, value in sorted(attrs.items()) |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
292 |
if value is not None) |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
293 |
|
1623 | 294 |
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
|
295 |
"""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
|
296 |
|
2399
68799e25f893
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
297 |
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
|
298 |
""" |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
299 |
value = u'<%s' % tag |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
300 |
if attrs: |
980
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
301 |
try: |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
302 |
attrs['class'] = attrs.pop('klass') |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
303 |
except KeyError: |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
304 |
pass |
2516
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
305 |
value += u' ' + sgml_attributes(attrs) |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
306 |
if content: |
1623 | 307 |
if escapecontent: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
308 |
content = xml_escape(unicode(content)) |
1623 | 309 |
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
|
310 |
else: |
2398
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
311 |
if tag in HTML4_EMPTY_TAGS: |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
312 |
value += u' />' |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
313 |
else: |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
314 |
value += u'></%s>' % tag |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
315 |
return value |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
316 |
|
0 | 317 |
def tooltipize(text, tooltip, url=None): |
318 |
"""make an HTML tooltip""" |
|
319 |
url = url or '#' |
|
320 |
return u'<a href="%s" title="%s">%s</a>' % (url, tooltip, text) |
|
321 |
||
322 |
def toggle_action(nodeid): |
|
323 |
"""builds a HTML link that uses the js toggleVisibility function""" |
|
324 |
return u"javascript: toggleVisibility('%s')" % nodeid |
|
325 |
||
326 |
def toggle_link(nodeid, label): |
|
327 |
"""builds a HTML link that uses the js toggleVisibility function""" |
|
328 |
return u'<a href="%s">%s</a>' % (toggle_action(nodeid), label) |
|
329 |
||
330 |
||
331 |
def ureport_as_html(layout): |
|
332 |
from logilab.common.ureports import HTMLWriter |
|
333 |
formater = HTMLWriter(True) |
|
334 |
stream = StringIO() #UStringIO() don't want unicode assertion |
|
335 |
formater.format(layout, stream) |
|
336 |
res = stream.getvalue() |
|
337 |
if isinstance(res, str): |
|
338 |
res = unicode(res, 'UTF8') |
|
339 |
return res |
|
340 |
||
341 |
# traceback formatting ######################################################## |
|
342 |
||
343 |
import traceback |
|
344 |
||
345 |
def rest_traceback(info, exception): |
|
346 |
"""return a ReST formated traceback""" |
|
347 |
res = [u'Traceback\n---------\n::\n'] |
|
348 |
for stackentry in traceback.extract_tb(info[2]): |
|
349 |
res.append(u'\tFile %s, line %s, function %s' % tuple(stackentry[:3])) |
|
350 |
if stackentry[3]: |
|
351 |
res.append(u'\t %s' % stackentry[3].decode('utf-8', 'replace')) |
|
352 |
res.append(u'\n') |
|
353 |
try: |
|
354 |
res.append(u'\t Error: %s\n' % exception) |
|
355 |
except: |
|
356 |
pass |
|
357 |
return u'\n'.join(res) |
|
358 |
||
359 |
||
360 |
def html_traceback(info, exception, title='', |
|
361 |
encoding='ISO-8859-1', body=''): |
|
362 |
""" return an html formatted traceback from python exception infos. |
|
363 |
""" |
|
364 |
tcbk = info[2] |
|
365 |
stacktb = traceback.extract_tb(tcbk) |
|
366 |
strings = [] |
|
367 |
if body: |
|
368 |
strings.append(u'<div class="error_body">') |
|
369 |
# FIXME |
|
370 |
strings.append(body) |
|
371 |
strings.append(u'</div>') |
|
372 |
if title: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
373 |
strings.append(u'<h1 class="error">%s</h1>'% xml_escape(title)) |
0 | 374 |
try: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
375 |
strings.append(u'<p class="error">%s</p>' % xml_escape(str(exception)).replace("\n","<br />")) |
0 | 376 |
except UnicodeError: |
377 |
pass |
|
378 |
strings.append(u'<div class="error_traceback">') |
|
379 |
for index, stackentry in enumerate(stacktb): |
|
380 |
strings.append(u'<b>File</b> <b class="file">%s</b>, <b>line</b> ' |
|
381 |
u'<b class="line">%s</b>, <b>function</b> ' |
|
382 |
u'<b class="function">%s</b>:<br/>'%( |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
383 |
xml_escape(stackentry[0]), stackentry[1], xml_escape(stackentry[2]))) |
0 | 384 |
if stackentry[3]: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
385 |
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
|
386 |
strings.append(u'  %s<br/>\n' % (string)) |
0 | 387 |
# add locals info for each entry |
388 |
try: |
|
389 |
local_context = tcbk.tb_frame.f_locals |
|
390 |
html_info = [] |
|
391 |
chars = 0 |
|
392 |
for name, value in local_context.iteritems(): |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
393 |
value = xml_escape(repr(value)) |
0 | 394 |
info = u'<span class="name">%s</span>=%s, ' % (name, value) |
395 |
line_length = len(name) + len(value) |
|
396 |
chars += line_length |
|
397 |
# 150 is the result of *years* of research ;-) (CSS might be helpful here) |
|
398 |
if chars > 150: |
|
399 |
info = u'<br/>' + info |
|
400 |
chars = line_length |
|
401 |
html_info.append(info) |
|
402 |
boxid = 'ctxlevel%d' % index |
|
403 |
strings.append(u'[%s]' % toggle_link(boxid, '+')) |
|
404 |
strings.append(u'<div id="%s" class="pycontext hidden">%s</div>' % |
|
405 |
(boxid, ''.join(html_info))) |
|
406 |
tcbk = tcbk.tb_next |
|
407 |
except Exception: |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
408 |
pass # doesn't really matter if we have no context info |
0 | 409 |
strings.append(u'</div>') |
410 |
return '\n'.join(strings) |
|
411 |
||
412 |
# csv files / unicode support ################################################# |
|
413 |
||
414 |
class UnicodeCSVWriter: |
|
415 |
"""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
|
416 |
|
0 | 417 |
def __init__(self, wfunc, encoding, **kwargs): |
418 |
self.writer = csv.writer(self, **kwargs) |
|
419 |
self.wfunc = wfunc |
|
420 |
self.encoding = encoding |
|
421 |
||
422 |
def write(self, data): |
|
423 |
self.wfunc(data) |
|
424 |
||
425 |
def writerow(self, row): |
|
426 |
csvrow = [] |
|
427 |
for elt in row: |
|
428 |
if isinstance(elt, unicode): |
|
429 |
csvrow.append(elt.encode(self.encoding)) |
|
430 |
else: |
|
431 |
csvrow.append(str(elt)) |
|
432 |
self.writer.writerow(csvrow) |
|
433 |
||
434 |
def writerows(self, rows): |
|
435 |
for row in rows: |
|
436 |
self.writerow(row) |
|
437 |
||
438 |
||
439 |
# some decorators ############################################################# |
|
440 |
||
441 |
class limitsize(object): |
|
442 |
def __init__(self, maxsize): |
|
443 |
self.maxsize = maxsize |
|
444 |
||
445 |
def __call__(self, function): |
|
446 |
def newfunc(*args, **kwargs): |
|
447 |
ret = function(*args, **kwargs) |
|
448 |
if isinstance(ret, basestring): |
|
449 |
return ret[:self.maxsize] |
|
450 |
return ret |
|
451 |
return newfunc |
|
452 |
||
453 |
||
454 |
def htmlescape(function): |
|
455 |
def newfunc(*args, **kwargs): |
|
456 |
ret = function(*args, **kwargs) |
|
457 |
assert isinstance(ret, basestring) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
458 |
return xml_escape(ret) |
0 | 459 |
return newfunc |