ir_qweb_fields.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """
  4. Web_editor-context rendering needs to add some metadata to rendered and allow to edit fields,
  5. as well as render a few fields differently.
  6. Also, adds methods to convert values back to Odoo models.
  7. """
  8. import babel
  9. import base64
  10. import io
  11. import json
  12. import logging
  13. import os
  14. import re
  15. import pytz
  16. import requests
  17. from datetime import datetime
  18. from lxml import etree, html
  19. from PIL import Image as I
  20. from werkzeug import urls
  21. import odoo.modules
  22. from odoo import _, api, models, fields
  23. from odoo.exceptions import UserError, ValidationError
  24. from odoo.tools import ustr, posix_to_ldml, pycompat
  25. from odoo.tools import html_escape as escape
  26. from odoo.tools.misc import get_lang, babel_locale_parse
  27. REMOTE_CONNECTION_TIMEOUT = 2.5
  28. logger = logging.getLogger(__name__)
  29. class IrQWeb(models.AbstractModel):
  30. """ IrQWeb object for rendering editor stuff
  31. """
  32. _inherit = 'ir.qweb'
  33. def _compile_node(self, el, compile_context, indent):
  34. snippet_key = compile_context.get('snippet-key')
  35. if snippet_key == compile_context['template'] \
  36. or compile_context.get('snippet-sub-call-key') == compile_context['template']:
  37. # Get the path of element to only consider the first node of the
  38. # snippet template content (ignoring all ancestors t elements which
  39. # are not t-call ones)
  40. nb_real_elements_in_hierarchy = 0
  41. node = el
  42. while node is not None and nb_real_elements_in_hierarchy < 2:
  43. if node.tag != 't' or 't-call' in node.attrib:
  44. nb_real_elements_in_hierarchy += 1
  45. node = node.getparent()
  46. if nb_real_elements_in_hierarchy == 1:
  47. # The first node might be a call to a sub template
  48. sub_call = el.get('t-call')
  49. if sub_call:
  50. el.set('t-options', f"{{'snippet-key': '{snippet_key}', 'snippet-sub-call-key': '{sub_call}'}}")
  51. # If it already has a data-snippet it is a saved or an inherited snippet.
  52. # Do not override it.
  53. elif 'data-snippet' not in el.attrib:
  54. el.attrib['data-snippet'] = snippet_key.split('.', 1)[-1]
  55. return super()._compile_node(el, compile_context, indent)
  56. # compile directives
  57. def _compile_directive_snippet(self, el, compile_context, indent):
  58. key = el.attrib.pop('t-snippet')
  59. el.set('t-call', key)
  60. snippet_lang = self._context.get('snippet_lang')
  61. if snippet_lang:
  62. el.set('t-lang', f"'{snippet_lang}'")
  63. el.set('t-options', f"{{'snippet-key': {key!r}}}")
  64. view = self.env['ir.ui.view']._get(key).sudo()
  65. name = el.attrib.pop('string', view.name)
  66. thumbnail = el.attrib.pop('t-thumbnail', "oe-thumbnail")
  67. # Forbid sanitize contains the specific reason:
  68. # - "true": always forbid
  69. # - "form": forbid if forms are sanitized
  70. forbid_sanitize = el.attrib.pop('t-forbid-sanitize', None)
  71. div = '<div name="%s" data-oe-type="snippet" data-oe-thumbnail="%s" data-oe-snippet-id="%s" data-oe-keywords="%s" %s>' % (
  72. escape(pycompat.to_text(name)),
  73. escape(pycompat.to_text(thumbnail)),
  74. escape(pycompat.to_text(view.id)),
  75. escape(pycompat.to_text(el.findtext('keywords'))),
  76. f'data-oe-forbid-sanitize="{forbid_sanitize}"' if forbid_sanitize else '',
  77. )
  78. self._append_text(div, compile_context)
  79. code = self._compile_node(el, compile_context, indent)
  80. self._append_text('</div>', compile_context)
  81. return code
  82. def _compile_directive_snippet_call(self, el, compile_context, indent):
  83. key = el.attrib.pop('t-snippet-call')
  84. el.set('t-call', key)
  85. el.set('t-options', f"{{'snippet-key': {key!r}}}")
  86. return self._compile_node(el, compile_context, indent)
  87. def _compile_directive_install(self, el, compile_context, indent):
  88. key = el.attrib.pop('t-install')
  89. thumbnail = el.attrib.pop('t-thumbnail', 'oe-thumbnail')
  90. if self.user_has_groups('base.group_system'):
  91. module = self.env['ir.module.module'].search([('name', '=', key)])
  92. if not module or module.state == 'installed':
  93. return []
  94. name = el.attrib.get('string') or 'Snippet'
  95. div = '<div name="%s" data-oe-type="snippet" data-module-id="%s" data-oe-thumbnail="%s"><section/></div>' % (
  96. escape(pycompat.to_text(name)),
  97. module.id,
  98. escape(pycompat.to_text(thumbnail))
  99. )
  100. self._append_text(div, compile_context)
  101. return []
  102. def _compile_directive_placeholder(self, el, compile_context, indent):
  103. el.set('t-att-placeholder', el.attrib.pop('t-placeholder'))
  104. return []
  105. # order and ignore
  106. def _directives_eval_order(self):
  107. directives = super()._directives_eval_order()
  108. # Insert before "att" as those may rely on static attributes like
  109. # "string" and "att" clears all of those
  110. index = directives.index('att') - 1
  111. directives.insert(index, 'placeholder')
  112. directives.insert(index, 'snippet')
  113. directives.insert(index, 'snippet-call')
  114. directives.insert(index, 'install')
  115. return directives
  116. def _get_template_cache_keys(self):
  117. return super()._get_template_cache_keys() + ['snippet_lang']
  118. #------------------------------------------------------
  119. # QWeb fields
  120. #------------------------------------------------------
  121. class Field(models.AbstractModel):
  122. _name = 'ir.qweb.field'
  123. _description = 'Qweb Field'
  124. _inherit = 'ir.qweb.field'
  125. @api.model
  126. def attributes(self, record, field_name, options, values):
  127. attrs = super(Field, self).attributes(record, field_name, options, values)
  128. field = record._fields[field_name]
  129. placeholder = options.get('placeholder') or getattr(field, 'placeholder', None)
  130. if placeholder:
  131. attrs['placeholder'] = placeholder
  132. if options['translate'] and field.type in ('char', 'text'):
  133. lang = record.env.lang or 'en_US'
  134. base_lang = record._get_base_lang()
  135. if lang == base_lang:
  136. attrs['data-oe-translation-state'] = 'translated'
  137. else:
  138. base_value = record.with_context(lang=base_lang)[field_name]
  139. value = record[field_name]
  140. attrs['data-oe-translation-state'] = 'translated' if base_value != value else 'to_translate'
  141. return attrs
  142. def value_from_string(self, value):
  143. return value
  144. @api.model
  145. def from_html(self, model, field, element):
  146. return self.value_from_string(element.text_content().strip())
  147. class Integer(models.AbstractModel):
  148. _name = 'ir.qweb.field.integer'
  149. _description = 'Qweb Field Integer'
  150. _inherit = 'ir.qweb.field.integer'
  151. @api.model
  152. def from_html(self, model, field, element):
  153. lang = self.user_lang()
  154. value = element.text_content().strip()
  155. return int(value.replace(lang.thousands_sep or '', ''))
  156. class Float(models.AbstractModel):
  157. _name = 'ir.qweb.field.float'
  158. _description = 'Qweb Field Float'
  159. _inherit = 'ir.qweb.field.float'
  160. @api.model
  161. def from_html(self, model, field, element):
  162. lang = self.user_lang()
  163. value = element.text_content().strip()
  164. return float(value.replace(lang.thousands_sep or '', '')
  165. .replace(lang.decimal_point, '.'))
  166. class ManyToOne(models.AbstractModel):
  167. _name = 'ir.qweb.field.many2one'
  168. _description = 'Qweb Field Many to One'
  169. _inherit = 'ir.qweb.field.many2one'
  170. @api.model
  171. def attributes(self, record, field_name, options, values):
  172. attrs = super(ManyToOne, self).attributes(record, field_name, options, values)
  173. if options.get('inherit_branding'):
  174. many2one = getattr(record, field_name)
  175. if many2one:
  176. attrs['data-oe-many2one-id'] = many2one.id
  177. attrs['data-oe-many2one-model'] = many2one._name
  178. return attrs
  179. @api.model
  180. def from_html(self, model, field, element):
  181. Model = self.env[element.get('data-oe-model')]
  182. id = int(element.get('data-oe-id'))
  183. M2O = self.env[field.comodel_name]
  184. field_name = element.get('data-oe-field')
  185. many2one_id = int(element.get('data-oe-many2one-id'))
  186. record = many2one_id and M2O.browse(many2one_id)
  187. if record and record.exists():
  188. # save the new id of the many2one
  189. Model.browse(id).write({field_name: many2one_id})
  190. # not necessary, but might as well be explicit about it
  191. return None
  192. class Contact(models.AbstractModel):
  193. _name = 'ir.qweb.field.contact'
  194. _description = 'Qweb Field Contact'
  195. _inherit = 'ir.qweb.field.contact'
  196. @api.model
  197. def attributes(self, record, field_name, options, values):
  198. attrs = super(Contact, self).attributes(record, field_name, options, values)
  199. if options.get('inherit_branding'):
  200. attrs['data-oe-contact-options'] = json.dumps(options)
  201. return attrs
  202. # helper to call the rendering of contact field
  203. @api.model
  204. def get_record_to_html(self, ids, options=None):
  205. return self.value_to_html(self.env['res.partner'].search([('id', '=', ids[0])]), options=options)
  206. class Date(models.AbstractModel):
  207. _name = 'ir.qweb.field.date'
  208. _description = 'Qweb Field Date'
  209. _inherit = 'ir.qweb.field.date'
  210. @api.model
  211. def attributes(self, record, field_name, options, values):
  212. attrs = super(Date, self).attributes(record, field_name, options, values)
  213. if options.get('inherit_branding'):
  214. attrs['data-oe-original'] = record[field_name]
  215. if record._fields[field_name].type == 'datetime':
  216. attrs = self.env['ir.qweb.field.datetime'].attributes(record, field_name, options, values)
  217. attrs['data-oe-type'] = 'datetime'
  218. return attrs
  219. lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env)
  220. locale = babel_locale_parse(lg.code)
  221. babel_format = value_format = posix_to_ldml(lg.date_format, locale=locale)
  222. if record[field_name]:
  223. date = fields.Date.from_string(record[field_name])
  224. value_format = pycompat.to_text(babel.dates.format_date(date, format=babel_format, locale=locale))
  225. attrs['data-oe-original-with-format'] = value_format
  226. return attrs
  227. @api.model
  228. def from_html(self, model, field, element):
  229. value = element.text_content().strip()
  230. if not value:
  231. return False
  232. lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env)
  233. date = datetime.strptime(value, lg.date_format)
  234. return fields.Date.to_string(date)
  235. class DateTime(models.AbstractModel):
  236. _name = 'ir.qweb.field.datetime'
  237. _description = 'Qweb Field Datetime'
  238. _inherit = 'ir.qweb.field.datetime'
  239. @api.model
  240. def attributes(self, record, field_name, options, values):
  241. attrs = super(DateTime, self).attributes(record, field_name, options, values)
  242. if options.get('inherit_branding'):
  243. value = record[field_name]
  244. lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env)
  245. locale = babel_locale_parse(lg.code)
  246. babel_format = value_format = posix_to_ldml('%s %s' % (lg.date_format, lg.time_format), locale=locale)
  247. tz = record.env.context.get('tz') or self.env.user.tz
  248. if isinstance(value, str):
  249. value = fields.Datetime.from_string(value)
  250. if value:
  251. # convert from UTC (server timezone) to user timezone
  252. value = fields.Datetime.context_timestamp(self.with_context(tz=tz), timestamp=value)
  253. value_format = pycompat.to_text(babel.dates.format_datetime(value, format=babel_format, locale=locale))
  254. value = fields.Datetime.to_string(value)
  255. attrs['data-oe-original'] = value
  256. attrs['data-oe-original-with-format'] = value_format
  257. attrs['data-oe-original-tz'] = tz
  258. return attrs
  259. @api.model
  260. def from_html(self, model, field, element):
  261. value = element.text_content().strip()
  262. if not value:
  263. return False
  264. # parse from string to datetime
  265. lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env)
  266. try:
  267. datetime_format = f'{lg.date_format} {lg.time_format}'
  268. dt = datetime.strptime(value, datetime_format)
  269. except ValueError:
  270. raise ValidationError(_("The datetime %s does not match the format %s", value, datetime_format))
  271. # convert back from user's timezone to UTC
  272. tz_name = element.attrib.get('data-oe-original-tz') or self.env.context.get('tz') or self.env.user.tz
  273. if tz_name:
  274. try:
  275. user_tz = pytz.timezone(tz_name)
  276. utc = pytz.utc
  277. dt = user_tz.localize(dt).astimezone(utc)
  278. except Exception:
  279. logger.warning(
  280. "Failed to convert the value for a field of the model"
  281. " %s back from the user's timezone (%s) to UTC",
  282. model, tz_name,
  283. exc_info=True)
  284. # format back to string
  285. return fields.Datetime.to_string(dt)
  286. class Text(models.AbstractModel):
  287. _name = 'ir.qweb.field.text'
  288. _description = 'Qweb Field Text'
  289. _inherit = 'ir.qweb.field.text'
  290. @api.model
  291. def from_html(self, model, field, element):
  292. return html_to_text(element)
  293. class Selection(models.AbstractModel):
  294. _name = 'ir.qweb.field.selection'
  295. _description = 'Qweb Field Selection'
  296. _inherit = 'ir.qweb.field.selection'
  297. @api.model
  298. def from_html(self, model, field, element):
  299. value = element.text_content().strip()
  300. selection = field.get_description(self.env)['selection']
  301. for k, v in selection:
  302. if isinstance(v, str):
  303. v = ustr(v)
  304. if value == v:
  305. return k
  306. raise ValueError(u"No value found for label %s in selection %s" % (
  307. value, selection))
  308. class HTML(models.AbstractModel):
  309. _name = 'ir.qweb.field.html'
  310. _description = 'Qweb Field HTML'
  311. _inherit = 'ir.qweb.field.html'
  312. @api.model
  313. def attributes(self, record, field_name, options, values=None):
  314. attrs = super().attributes(record, field_name, options, values)
  315. if options.get('inherit_branding'):
  316. field = record._fields[field_name]
  317. if field.sanitize:
  318. if field.sanitize_overridable and not record.user_has_groups('base.group_sanitize_override'):
  319. try:
  320. field.convert_to_column(record[field_name], record)
  321. except UserError:
  322. # The field contains element(s) that would be removed if
  323. # sanitized. It means that someone who was part of a
  324. # group allowing to bypass the sanitation saved that
  325. # field previously. Mark the field as not editable.
  326. attrs['data-oe-sanitize-prevent-edition'] = 1
  327. if not (field.sanitize_overridable and record.user_has_groups('base.group_sanitize_override')):
  328. # Don't mark the field as 'sanitize' if the sanitize is
  329. # defined as overridable and the user has the right to do so
  330. attrs['data-oe-sanitize'] = 1 if field.sanitize_form else 'allow_form'
  331. return attrs
  332. @api.model
  333. def from_html(self, model, field, element):
  334. content = []
  335. if element.text:
  336. content.append(element.text)
  337. content.extend(html.tostring(child, encoding='unicode')
  338. for child in element.iterchildren(tag=etree.Element))
  339. return '\n'.join(content)
  340. class Image(models.AbstractModel):
  341. """
  342. Widget options:
  343. ``class``
  344. set as attribute on the generated <img> tag
  345. """
  346. _name = 'ir.qweb.field.image'
  347. _description = 'Qweb Field Image'
  348. _inherit = 'ir.qweb.field.image'
  349. local_url_re = re.compile(r'^/(?P<module>[^]]+)/static/(?P<rest>.+)$')
  350. @api.model
  351. def from_html(self, model, field, element):
  352. if element.find('img') is None:
  353. return False
  354. url = element.find('img').get('src')
  355. url_object = urls.url_parse(url)
  356. if url_object.path.startswith('/web/image'):
  357. fragments = url_object.path.split('/')
  358. query = url_object.decode_query()
  359. url_id = fragments[3].split('-')[0]
  360. # ir.attachment image urls: /web/image/<id>[-<checksum>][/...]
  361. if url_id.isdigit():
  362. model = 'ir.attachment'
  363. oid = url_id
  364. field = 'datas'
  365. # url of binary field on model: /web/image/<model>/<id>/<field>[/...]
  366. else:
  367. model = query.get('model', fragments[3])
  368. oid = query.get('id', fragments[4])
  369. field = query.get('field', fragments[5])
  370. item = self.env[model].browse(int(oid))
  371. return item[field]
  372. if self.local_url_re.match(url_object.path):
  373. return self.load_local_url(url)
  374. return self.load_remote_url(url)
  375. def load_local_url(self, url):
  376. match = self.local_url_re.match(urls.url_parse(url).path)
  377. rest = match.group('rest')
  378. for sep in os.sep, os.altsep:
  379. if sep and sep != '/':
  380. rest.replace(sep, '/')
  381. path = odoo.modules.get_module_resource(
  382. match.group('module'), 'static', *(rest.split('/')))
  383. if not path:
  384. return None
  385. try:
  386. with open(path, 'rb') as f:
  387. # force complete image load to ensure it's valid image data
  388. image = I.open(f)
  389. image.load()
  390. f.seek(0)
  391. return base64.b64encode(f.read())
  392. except Exception:
  393. logger.exception("Failed to load local image %r", url)
  394. return None
  395. def load_remote_url(self, url):
  396. try:
  397. # should probably remove remote URLs entirely:
  398. # * in fields, downloading them without blowing up the server is a
  399. # challenge
  400. # * in views, may trigger mixed content warnings if HTTPS CMS
  401. # linking to HTTP images
  402. # implement drag & drop image upload to mitigate?
  403. req = requests.get(url, timeout=REMOTE_CONNECTION_TIMEOUT)
  404. # PIL needs a seekable file-like image so wrap result in IO buffer
  405. image = I.open(io.BytesIO(req.content))
  406. # force a complete load of the image data to validate it
  407. image.load()
  408. except Exception:
  409. logger.warning("Failed to load remote image %r", url, exc_info=True)
  410. return None
  411. # don't use original data in case weird stuff was smuggled in, with
  412. # luck PIL will remove some of it?
  413. out = io.BytesIO()
  414. image.save(out, image.format)
  415. return base64.b64encode(out.getvalue())
  416. class Monetary(models.AbstractModel):
  417. _name = 'ir.qweb.field.monetary'
  418. _inherit = 'ir.qweb.field.monetary'
  419. @api.model
  420. def from_html(self, model, field, element):
  421. lang = self.user_lang()
  422. value = element.find('span').text_content().strip()
  423. return float(value.replace(lang.thousands_sep or '', '')
  424. .replace(lang.decimal_point, '.'))
  425. class Duration(models.AbstractModel):
  426. _name = 'ir.qweb.field.duration'
  427. _description = 'Qweb Field Duration'
  428. _inherit = 'ir.qweb.field.duration'
  429. @api.model
  430. def attributes(self, record, field_name, options, values):
  431. attrs = super(Duration, self).attributes(record, field_name, options, values)
  432. if options.get('inherit_branding'):
  433. attrs['data-oe-original'] = record[field_name]
  434. return attrs
  435. @api.model
  436. def from_html(self, model, field, element):
  437. value = element.text_content().strip()
  438. # non-localized value
  439. return float(value)
  440. class RelativeDatetime(models.AbstractModel):
  441. _name = 'ir.qweb.field.relative'
  442. _description = 'Qweb Field Relative'
  443. _inherit = 'ir.qweb.field.relative'
  444. # get formatting from ir.qweb.field.relative but edition/save from datetime
  445. class QwebView(models.AbstractModel):
  446. _name = 'ir.qweb.field.qweb'
  447. _description = 'Qweb Field qweb'
  448. _inherit = 'ir.qweb.field.qweb'
  449. def html_to_text(element):
  450. """ Converts HTML content with HTML-specified line breaks (br, p, div, ...)
  451. in roughly equivalent textual content.
  452. Used to replace and fixup the roundtripping of text and m2o: when using
  453. libxml 2.8.0 (but not 2.9.1) and parsing HTML with lxml.html.fromstring
  454. whitespace text nodes (text nodes composed *solely* of whitespace) are
  455. stripped out with no recourse, and fundamentally relying on newlines
  456. being in the text (e.g. inserted during user edition) is probably poor form
  457. anyway.
  458. -> this utility function collapses whitespace sequences and replaces
  459. nodes by roughly corresponding linebreaks
  460. * p are pre-and post-fixed by 2 newlines
  461. * br are replaced by a single newline
  462. * block-level elements not already mentioned are pre- and post-fixed by
  463. a single newline
  464. ought be somewhat similar (but much less high-tech) to aaronsw's html2text.
  465. the latter produces full-blown markdown, our text -> html converter only
  466. replaces newlines by <br> elements at this point so we're reverting that,
  467. and a few more newline-ish elements in case the user tried to add
  468. newlines/paragraphs into the text field
  469. :param element: lxml.html content
  470. :returns: corresponding pure-text output
  471. """
  472. # output is a list of str | int. Integers are padding requests (in minimum
  473. # number of newlines). When multiple padding requests, fold them into the
  474. # biggest one
  475. output = []
  476. _wrap(element, output)
  477. # remove any leading or tailing whitespace, replace sequences of
  478. # (whitespace)\n(whitespace) by a single newline, where (whitespace) is a
  479. # non-newline whitespace in this case
  480. return re.sub(
  481. r'[ \t\r\f]*\n[ \t\r\f]*',
  482. '\n',
  483. ''.join(_realize_padding(output)).strip())
  484. _PADDED_BLOCK = set('p h1 h2 h3 h4 h5 h6'.split())
  485. # https://developer.mozilla.org/en-US/docs/HTML/Block-level_elements minus p
  486. _MISC_BLOCK = set((
  487. 'address article aside audio blockquote canvas dd dl div figcaption figure'
  488. ' footer form header hgroup hr ol output pre section tfoot ul video'
  489. ).split())
  490. def _collapse_whitespace(text):
  491. """ Collapses sequences of whitespace characters in ``text`` to a single
  492. space
  493. """
  494. return re.sub('\s+', ' ', text)
  495. def _realize_padding(it):
  496. """ Fold and convert padding requests: integers in the output sequence are
  497. requests for at least n newlines of padding. Runs thereof can be collapsed
  498. into the largest requests and converted to newlines.
  499. """
  500. padding = 0
  501. for item in it:
  502. if isinstance(item, int):
  503. padding = max(padding, item)
  504. continue
  505. if padding:
  506. yield '\n' * padding
  507. padding = 0
  508. yield item
  509. # leftover padding irrelevant as the output will be stripped
  510. def _wrap(element, output, wrapper=''):
  511. """ Recursively extracts text from ``element`` (via _element_to_text), and
  512. wraps it all in ``wrapper``. Extracted text is added to ``output``
  513. :type wrapper: basestring | int
  514. """
  515. output.append(wrapper)
  516. if element.text:
  517. output.append(_collapse_whitespace(element.text))
  518. for child in element:
  519. _element_to_text(child, output)
  520. output.append(wrapper)
  521. def _element_to_text(e, output):
  522. if e.tag == 'br':
  523. output.append('\n')
  524. elif e.tag in _PADDED_BLOCK:
  525. _wrap(e, output, 2)
  526. elif e.tag in _MISC_BLOCK:
  527. _wrap(e, output, 1)
  528. else:
  529. # inline
  530. _wrap(e, output)
  531. if e.tail:
  532. output.append(_collapse_whitespace(e.tail))