166 params.append(simplejson.dumps(extraparams)) |
166 params.append(simplejson.dumps(extraparams)) |
167 if swap: |
167 if swap: |
168 params.append('true') |
168 params.append('true') |
169 return "javascript: replacePageChunk(%s);" % ', '.join(params) |
169 return "javascript: replacePageChunk(%s);" % ', '.join(params) |
170 |
170 |
|
171 from lxml import etree |
|
172 from copy import deepcopy |
|
173 |
171 def safe_cut(text, length): |
174 def safe_cut(text, length): |
172 """returns a string of length <length> based on <text>, removing any html |
175 """returns a string of length <length> based on <text>, removing any html |
173 tags from given text if cut is necessary. |
176 tags from given text if cut is necessary. |
174 """ |
177 """ |
175 if text is None: |
178 if text is None: |
176 return u'' |
179 return u'' |
177 text_nohtml = remove_html_tags(text) |
180 textParse = etree.HTML(text) |
178 # try to keep html tags if text is short enough |
181 compteur = 0 |
179 if len(text_nohtml) <= length: |
182 |
180 return text |
183 for element in textParse.iter(): |
181 # else if un-tagged text is too long, cut it |
184 if compteur > length: |
182 return text_nohtml[:length-3] + u'...' |
185 parent = element.getparent() |
|
186 parent.remove(element) |
|
187 else: |
|
188 if element.text is not None: |
|
189 text_resum = text_cut_letters(element.text,length) |
|
190 len_text_resum = len(''.join(text_resum.split())) |
|
191 compteur = compteur + len_text_resum |
|
192 element.text = text_resum |
|
193 |
|
194 if element.tail is not None: |
|
195 if compteur < length: |
|
196 text_resum = text_cut_letters(element.tail,length) |
|
197 len_text_resum = len(''.join(text_resum.split())) |
|
198 compteur = compteur + len_text_resum |
|
199 element.tail = text_resum |
|
200 else: |
|
201 element.tail = '' |
|
202 |
|
203 div = etree.HTML('<div></div>')[0][0] |
|
204 listNode = textParse[0].getchildren() |
|
205 for node in listNode: |
|
206 div.append(deepcopy(node)) |
|
207 return etree.tounicode(div) |
183 |
208 |
184 def text_cut(text, nbwords=30): |
209 def text_cut(text, nbwords=30): |
185 if text is None: |
210 if text is None: |
186 return u'' |
211 return u'' |
187 minlength = len(' '.join(text.split()[:nbwords])) |
212 minlength = len(' '.join(text.split()[:nbwords])) |
188 textlength = text.find('.', minlength) + 1 |
213 textlength = text.find('.', minlength) + 1 |
189 if textlength == 0: # no point found |
214 if textlength == 0: # no point found |
190 textlength = minlength |
215 textlength = minlength |
191 return text[:textlength] |
216 return text[:textlength] |
192 |
217 |
|
218 def text_cut_letters(text, nbletters): |
|
219 if text is None: |
|
220 return u'' |
|
221 if len(''.join(text.split())) <= nbletters: |
|
222 return text |
|
223 else: |
|
224 text_nospace = ''.join(text.split()) |
|
225 textlength=text.find('.') + 1 |
|
226 |
|
227 if textlength==0: |
|
228 textlength=text.find(' ', nbletters+5) |
|
229 |
|
230 return text[:textlength] |
193 |
231 |
194 def cut(text, length): |
232 def cut(text, length): |
195 """returns a string of length <length> based on <text> |
233 """returns a string of length <length> based on <text> |
196 post: |
234 post: |
197 len(__return__) <= length |
235 len(__return__) <= length |