1 # -*- coding: utf-8 -*-
15 from subprocess import Popen, PIPE
16 from urlparse import urlparse
21 from lxml import etree, html
26 from openerp.tools.func import lazy_property
27 import openerp.tools.lru
28 from openerp.http import request
29 from openerp.tools.safe_eval import safe_eval as eval
30 from openerp.osv import osv, orm, fields
31 from openerp.tools import html_escape as escape
32 from openerp.tools.translate import _
34 _logger = logging.getLogger(__name__)
36 #--------------------------------------------------------------------
37 # QWeb template engine
38 #--------------------------------------------------------------------
39 class QWebException(Exception):
40 def __init__(self, message, **kw):
41 Exception.__init__(self, message)
44 if 'node' not in self.qweb:
46 return etree.tostring(self.qweb['node'], pretty_print=True)
48 class QWebTemplateNotFound(QWebException):
51 def raise_qweb_exception(etype=None, **kw):
54 orig_type, original, tb = sys.exc_info()
56 raise etype, original, tb
58 for k, v in kw.items():
60 # Will use `raise foo from bar` in python 3 and rename cause to __cause__
61 e.qweb['cause'] = original
64 class QWebContext(dict):
65 def __init__(self, cr, uid, data, loader=None, templates=None, context=None):
69 self.templates = templates or {}
70 self.context = context
72 super(QWebContext, self).__init__(dic)
73 self['defined'] = lambda key: key in self
75 def safe_eval(self, expr):
76 locals_dict = collections.defaultdict(lambda: None)
77 locals_dict.update(self)
78 locals_dict.pop('cr', None)
79 locals_dict.pop('loader', None)
80 return eval(expr, None, locals_dict, nocopy=True, locals_builtins=True)
83 """ Clones the current context, conserving all data and metadata
84 (loader, template cache, ...)
86 return QWebContext(self.cr, self.uid, dict.copy(self),
88 templates=self.templates,
94 class QWeb(orm.AbstractModel):
95 """ Base QWeb rendering engine
97 * to customize ``t-field`` rendering, subclass ``ir.qweb.field`` and
98 create new models called :samp:`ir.qweb.field.{widget}`
99 * alternatively, override :meth:`~.get_converter_for` and return an
100 arbitrary model to use as field converter
102 Beware that if you need extensions or alterations which could be
103 incompatible with other subsystems, you should create a local object
104 inheriting from ``ir.qweb`` and customize that.
109 _void_elements = frozenset([
110 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',
111 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'])
112 _format_regex = re.compile(
117 # jinja-style pattern
121 def __init__(self, pool, cr):
122 super(QWeb, self).__init__(pool, cr)
124 self._render_tag = self.prefixed_methods('render_tag_')
125 self._render_att = self.prefixed_methods('render_att_')
127 def prefixed_methods(self, prefix):
128 """ Extracts all methods prefixed by ``prefix``, and returns a mapping
129 of (t-name, method) where the t-name is the method name with prefix
130 removed and underscore converted to dashes
135 n_prefix = len(prefix)
137 (name[n_prefix:].replace('_', '-'), getattr(type(self), name))
138 for name in dir(self)
139 if name.startswith(prefix)
142 def register_tag(self, tag, func):
143 self._render_tag[tag] = func
145 def add_template(self, qwebcontext, name, node):
146 """Add a parsed template in the context. Used to preprocess templates."""
147 qwebcontext.templates[name] = node
149 def load_document(self, document, res_id, qwebcontext):
151 Loads an XML document and installs any contained template in the engine
153 :type document: a parsed lxml.etree element, an unparsed XML document
154 (as a string) or the path of an XML file to load
156 if not isinstance(document, basestring):
157 # assume lxml.etree.Element
159 elif document.startswith("<?xml"):
160 dom = etree.fromstring(document)
162 dom = etree.parse(document).getroot()
165 if node.get('t-name'):
166 name = str(node.get("t-name"))
167 self.add_template(qwebcontext, name, node)
168 if res_id and node.tag == "t":
169 self.add_template(qwebcontext, res_id, node)
172 def get_template(self, name, qwebcontext):
173 """ Tries to fetch the template ``name``, either gets it from the
174 context's template cache or loads one with the context's loader (if
177 :raises QWebTemplateNotFound: if the template can not be found or loaded
179 origin_template = qwebcontext.get('__caller__') or qwebcontext['__stack__'][0]
180 if qwebcontext.loader and name not in qwebcontext.templates:
182 xml_doc = qwebcontext.loader(name)
184 raise_qweb_exception(QWebTemplateNotFound, message="Loader could not find template %r" % name, template=origin_template)
185 self.load_document(xml_doc, isinstance(name, (int, long)) and name or None, qwebcontext=qwebcontext)
187 if name in qwebcontext.templates:
188 return qwebcontext.templates[name]
190 raise QWebTemplateNotFound("Template %r not found" % name, template=origin_template)
192 def eval(self, expr, qwebcontext):
194 return qwebcontext.safe_eval(expr)
196 template = qwebcontext.get('__template__')
197 raise_qweb_exception(message="Could not evaluate expression %r" % expr, expression=expr, template=template)
199 def eval_object(self, expr, qwebcontext):
200 return self.eval(expr, qwebcontext)
202 def eval_str(self, expr, qwebcontext):
204 return qwebcontext.get(0, '')
205 val = self.eval(expr, qwebcontext)
206 if isinstance(val, unicode):
207 return val.encode("utf8")
208 if val is False or val is None:
212 def eval_format(self, expr, qwebcontext):
213 expr, replacements = self._format_regex.subn(
214 lambda m: self.eval_str(m.group(1) or m.group(2), qwebcontext),
222 return str(expr % qwebcontext)
224 template = qwebcontext.get('__template__')
225 raise_qweb_exception(message="Format error for expression %r" % expr, expression=expr, template=template)
227 def eval_bool(self, expr, qwebcontext):
228 return int(bool(self.eval(expr, qwebcontext)))
230 def render(self, cr, uid, id_or_xml_id, qwebcontext=None, loader=None, context=None):
231 """ render(cr, uid, id_or_xml_id, qwebcontext=None, loader=None, context=None)
233 Renders the template specified by the provided template name
235 :param qwebcontext: context for rendering the template
236 :type qwebcontext: dict or :class:`QWebContext` instance
237 :param loader: if ``qwebcontext`` is a dict, loader set into the
238 context instantiated for rendering
240 if qwebcontext is None:
243 if not isinstance(qwebcontext, QWebContext):
244 qwebcontext = QWebContext(cr, uid, qwebcontext, loader=loader, context=context)
246 qwebcontext['__template__'] = id_or_xml_id
247 stack = qwebcontext.get('__stack__', [])
249 qwebcontext['__caller__'] = stack[-1]
250 stack.append(id_or_xml_id)
251 qwebcontext['__stack__'] = stack
252 qwebcontext['xmlid'] = str(stack[0]) # Temporary fix
253 return self.render_node(self.get_template(id_or_xml_id, qwebcontext), qwebcontext)
255 def render_node(self, element, qwebcontext):
256 generated_attributes = ""
258 template_attributes = {}
259 for (attribute_name, attribute_value) in element.attrib.iteritems():
260 attribute_name = str(attribute_name)
261 if attribute_name == "groups":
262 cr = qwebcontext.get('request') and qwebcontext['request'].cr or None
263 uid = qwebcontext.get('request') and qwebcontext['request'].uid or None
264 can_see = self.user_has_groups(cr, uid, groups=attribute_value) if cr and uid else False
268 attribute_value = attribute_value.encode("utf8")
270 if attribute_name.startswith("t-"):
271 for attribute in self._render_att:
272 if attribute_name[2:].startswith(attribute):
273 attrs = self._render_att[attribute](
274 self, element, attribute_name, attribute_value, qwebcontext)
275 for att, val in attrs:
277 if not isinstance(val, str):
278 val = unicode(val).encode('utf-8')
279 generated_attributes += self.render_attribute(element, att, val, qwebcontext)
282 if attribute_name[2:] in self._render_tag:
283 t_render = attribute_name[2:]
284 template_attributes[attribute_name[2:]] = attribute_value
286 generated_attributes += self.render_attribute(element, attribute_name, attribute_value, qwebcontext)
288 if 'debug' in template_attributes:
289 debugger = template_attributes.get('debug', 'pdb')
290 __import__(debugger).set_trace() # pdb, ipdb, pudb, ...
292 result = self._render_tag[t_render](self, element, template_attributes, generated_attributes, qwebcontext)
294 result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
297 result += element.tail.encode('utf-8')
299 if isinstance(result, unicode):
300 return result.encode('utf-8')
303 def render_element(self, element, template_attributes, generated_attributes, qwebcontext, inner=None):
305 # template_attributes: t-* attributes
306 # generated_attributes: generated attributes
307 # qwebcontext: values
308 # inner: optional innerXml
310 g_inner = inner.encode('utf-8') if isinstance(inner, unicode) else inner
312 g_inner = [] if element.text is None else [element.text.encode('utf-8')]
313 for current_node in element.iterchildren(tag=etree.Element):
315 g_inner.append(self.render_node(current_node, qwebcontext))
316 except QWebException:
319 template = qwebcontext.get('__template__')
320 raise_qweb_exception(message="Could not render element %r" % element.tag, node=element, template=template)
321 name = str(element.tag)
322 inner = "".join(g_inner)
323 trim = template_attributes.get("trim", 0)
327 inner = inner.lstrip()
328 elif trim == 'right':
329 inner = inner.rstrip()
331 inner = inner.strip()
334 elif len(inner) or name not in self._void_elements:
335 return "<%s%s>%s</%s>" % tuple(
336 qwebcontext if isinstance(qwebcontext, str) else qwebcontext.encode('utf-8')
337 for qwebcontext in (name, generated_attributes, inner, name)
340 return "<%s%s/>" % (name, generated_attributes)
342 def render_attribute(self, element, name, value, qwebcontext):
343 return ' %s="%s"' % (name, escape(value))
346 def render_att_att(self, element, attribute_name, attribute_value, qwebcontext):
347 if attribute_name.startswith("t-attf-"):
348 return [(attribute_name[7:], self.eval_format(attribute_value, qwebcontext))]
350 if attribute_name.startswith("t-att-"):
351 return [(attribute_name[6:], self.eval(attribute_value, qwebcontext))]
353 result = self.eval_object(attribute_value, qwebcontext)
354 if isinstance(result, collections.Mapping):
355 return result.iteritems()
360 def render_tag_raw(self, element, template_attributes, generated_attributes, qwebcontext):
361 inner = self.eval_str(template_attributes["raw"], qwebcontext)
362 return self.render_element(element, template_attributes, generated_attributes, qwebcontext, inner)
364 def render_tag_esc(self, element, template_attributes, generated_attributes, qwebcontext):
365 options = json.loads(template_attributes.get('esc-options') or '{}')
366 widget = self.get_widget_for(options.get('widget'))
367 inner = widget.format(template_attributes['esc'], options, qwebcontext)
368 return self.render_element(element, template_attributes, generated_attributes, qwebcontext, inner)
370 def _iterate(self, iterable):
371 if isinstance (iterable, collections.Mapping):
372 return iterable.iteritems()
374 return itertools.izip(*itertools.tee(iterable))
376 def render_tag_foreach(self, element, template_attributes, generated_attributes, qwebcontext):
377 expr = template_attributes["foreach"]
378 enum = self.eval_object(expr, qwebcontext)
380 template = qwebcontext.get('__template__')
381 raise QWebException("foreach enumerator %r is not defined while rendering template %r" % (expr, template), template=template)
382 if isinstance(enum, int):
385 varname = template_attributes['as'].replace('.', '_')
386 copy_qwebcontext = qwebcontext.copy()
389 if isinstance(enum, collections.Sized):
391 copy_qwebcontext["%s_size" % varname] = size
393 copy_qwebcontext["%s_all" % varname] = enum
395 for index, (item, value) in enumerate(self._iterate(enum)):
396 copy_qwebcontext.update({
398 '%s_value' % varname: value,
399 '%s_index' % varname: index,
400 '%s_first' % varname: index == 0,
403 copy_qwebcontext['%s_last' % varname] = index + 1 == size
405 copy_qwebcontext.update({
406 '%s_parity' % varname: 'odd',
407 '%s_even' % varname: False,
408 '%s_odd' % varname: True,
411 copy_qwebcontext.update({
412 '%s_parity' % varname: 'even',
413 '%s_even' % varname: True,
414 '%s_odd' % varname: False,
416 ru.append(self.render_element(element, template_attributes, generated_attributes, copy_qwebcontext))
419 def render_tag_if(self, element, template_attributes, generated_attributes, qwebcontext):
420 if self.eval_bool(template_attributes["if"], qwebcontext):
421 return self.render_element(element, template_attributes, generated_attributes, qwebcontext)
424 def render_tag_call(self, element, template_attributes, generated_attributes, qwebcontext):
425 d = qwebcontext.copy()
426 d[0] = self.render_element(element, template_attributes, generated_attributes, d)
427 cr = d.get('request') and d['request'].cr or None
428 uid = d.get('request') and d['request'].uid or None
430 template = self.eval_format(template_attributes["call"], d)
432 template = int(template)
435 return self.render(cr, uid, template, d)
437 def render_tag_call_assets(self, element, template_attributes, generated_attributes, qwebcontext):
438 """ This special 't-call' tag can be used in order to aggregate/minify javascript and css assets"""
440 # An asset bundle is rendered in two differents contexts (when genereting html and
441 # when generating the bundle itself) so they must be qwebcontext free
442 # even '0' variable is forbidden
443 template = qwebcontext.get('__template__')
444 raise QWebException("t-call-assets cannot contain children nodes", template=template)
445 xmlid = template_attributes['call-assets']
446 cr, uid, context = [getattr(qwebcontext, attr) for attr in ('cr', 'uid', 'context')]
447 bundle = AssetsBundle(xmlid, cr=cr, uid=uid, context=context, registry=self.pool)
448 css = self.get_attr_bool(template_attributes.get('css'), default=True)
449 js = self.get_attr_bool(template_attributes.get('js'), default=True)
450 return bundle.to_html(css=css, js=js, debug=bool(qwebcontext.get('debug')))
452 def render_tag_set(self, element, template_attributes, generated_attributes, qwebcontext):
453 if "value" in template_attributes:
454 qwebcontext[template_attributes["set"]] = self.eval_object(template_attributes["value"], qwebcontext)
455 elif "valuef" in template_attributes:
456 qwebcontext[template_attributes["set"]] = self.eval_format(template_attributes["valuef"], qwebcontext)
458 qwebcontext[template_attributes["set"]] = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
461 def render_tag_field(self, element, template_attributes, generated_attributes, qwebcontext):
462 """ eg: <span t-record="browse_record(res.partner, 1)" t-field="phone">+1 555 555 8069</span>"""
463 node_name = element.tag
464 assert node_name not in ("table", "tbody", "thead", "tfoot", "tr", "td",
465 "li", "ul", "ol", "dl", "dt", "dd"),\
466 "RTE widgets do not work correctly on %r elements" % node_name
467 assert node_name != 't',\
468 "t-field can not be used on a t element, provide an actual HTML node"
470 record, field_name = template_attributes["field"].rsplit('.', 1)
471 record = self.eval_object(record, qwebcontext)
473 field = record._fields[field_name]
474 options = json.loads(template_attributes.get('field-options') or '{}')
475 field_type = get_field_type(field, options)
477 converter = self.get_converter_for(field_type)
479 return converter.to_html(qwebcontext.cr, qwebcontext.uid, field_name, record, options,
480 element, template_attributes, generated_attributes, qwebcontext, context=qwebcontext.context)
482 def get_converter_for(self, field_type):
483 """ returns a :class:`~openerp.models.Model` used to render a
486 By default, tries to get the model named
487 :samp:`ir.qweb.field.{field_type}`, falling back on ``ir.qweb.field``.
489 :param str field_type: type or widget of field to render
491 return self.pool.get('ir.qweb.field.' + field_type, self.pool['ir.qweb.field'])
493 def get_widget_for(self, widget):
494 """ returns a :class:`~openerp.models.Model` used to render a
497 :param str widget: name of the widget to use, or ``None``
499 widget_model = ('ir.qweb.widget.' + widget) if widget else 'ir.qweb.widget'
500 return self.pool.get(widget_model) or self.pool['ir.qweb.widget']
502 def get_attr_bool(self, attr, default=False):
505 if attr in ('false', '0'):
507 elif attr in ('true', '1'):
511 #--------------------------------------------------------------------
512 # QWeb Fields converters
513 #--------------------------------------------------------------------
515 class FieldConverter(osv.AbstractModel):
516 """ Used to convert a t-field specification into an output HTML field.
518 :meth:`~.to_html` is the entry point of this conversion from QWeb, it:
520 * converts the record value to html using :meth:`~.record_to_html`
521 * generates the metadata attributes (``data-oe-``) to set on the root
523 * generates the root result node itself through :meth:`~.render_element`
525 _name = 'ir.qweb.field'
527 def attributes(self, cr, uid, field_name, record, options,
528 source_element, g_att, t_att, qweb_context,
530 """ attributes(cr, uid, field_name, record, options, source_element, g_att, t_att, qweb_context, context=None)
532 Generates the metadata attributes (prefixed by ``data-oe-`` for the
533 root node of the field conversion. Attribute values are escaped by the
536 The default attributes are:
538 * ``model``, the name of the record's model
539 * ``id`` the id of the record to which the field belongs
540 * ``field`` the name of the converted field
541 * ``type`` the logical field type (widget, may not match the field's
542 ``type``, may not be any Field subclass name)
543 * ``translate``, a boolean flag (``0`` or ``1``) denoting whether the
544 field is translatable
545 * ``expression``, the original expression
547 :returns: iterable of (attribute name, attribute value) pairs.
549 field = record._fields[field_name]
550 field_type = get_field_type(field, options)
552 ('data-oe-model', record._name),
553 ('data-oe-id', record.id),
554 ('data-oe-field', field_name),
555 ('data-oe-type', field_type),
556 ('data-oe-expression', t_att['field']),
559 def value_to_html(self, cr, uid, value, field, options=None, context=None):
560 """ value_to_html(cr, uid, value, field, options=None, context=None)
562 Converts a single value to its HTML version/output
564 if not value: return ''
567 def record_to_html(self, cr, uid, field_name, record, options=None, context=None):
568 """ record_to_html(cr, uid, field_name, record, options=None, context=None)
570 Converts the specified field of the browse_record ``record`` to HTML
572 field = record._fields[field_name]
573 return self.value_to_html(
574 cr, uid, record[field_name], field, options=options, context=context)
576 def to_html(self, cr, uid, field_name, record, options,
577 source_element, t_att, g_att, qweb_context, context=None):
578 """ to_html(cr, uid, field_name, record, options, source_element, t_att, g_att, qweb_context, context=None)
580 Converts a ``t-field`` to its HTML output. A ``t-field`` may be
581 extended by a ``t-field-options``, which is a JSON-serialized mapping
582 of configuration values.
584 A default configuration key is ``widget`` which can override the
585 field's own ``_type``.
588 content = self.record_to_html(cr, uid, field_name, record, options, context=context)
589 if options.get('html-escape', True):
590 content = escape(content)
591 elif hasattr(content, '__html__'):
592 content = content.__html__()
594 _logger.warning("Could not get field %s for model %s",
595 field_name, record._name, exc_info=True)
598 inherit_branding = context and context.get('inherit_branding')
599 if not inherit_branding and context and context.get('inherit_branding_auto'):
600 inherit_branding = self.pool['ir.model.access'].check(cr, uid, record._name, 'write', False, context=context)
603 # add branding attributes
605 ' %s="%s"' % (name, escape(value))
606 for name, value in self.attributes(
607 cr, uid, field_name, record, options,
608 source_element, g_att, t_att, qweb_context)
611 return self.render_element(cr, uid, source_element, t_att, g_att,
612 qweb_context, content)
614 def qweb_object(self):
615 return self.pool['ir.qweb']
617 def render_element(self, cr, uid, source_element, t_att, g_att,
618 qweb_context, content):
619 """ render_element(cr, uid, source_element, t_att, g_att, qweb_context, content)
621 Final rendering hook, by default just calls ir.qweb's ``render_element``
623 return self.qweb_object().render_element(
624 source_element, t_att, g_att, qweb_context, content or '')
626 def user_lang(self, cr, uid, context):
627 """ user_lang(cr, uid, context)
629 Fetches the res.lang object corresponding to the language code stored
630 in the user's context. Fallbacks to en_US if no lang is present in the
631 context *or the language code is not valid*.
633 :returns: res.lang browse_record
635 if context is None: context = {}
637 lang_code = context.get('lang') or 'en_US'
638 Lang = self.pool['res.lang']
640 lang_ids = Lang.search(cr, uid, [('code', '=', lang_code)], context=context) \
641 or Lang.search(cr, uid, [('code', '=', 'en_US')], context=context)
643 return Lang.browse(cr, uid, lang_ids[0], context=context)
645 class FloatConverter(osv.AbstractModel):
646 _name = 'ir.qweb.field.float'
647 _inherit = 'ir.qweb.field'
649 def precision(self, cr, uid, field, options=None, context=None):
650 _, precision = field.digits or (None, None)
653 def value_to_html(self, cr, uid, value, field, options=None, context=None):
656 precision = self.precision(cr, uid, field, options=options, context=context)
657 fmt = '%f' if precision is None else '%.{precision}f'
659 lang_code = context.get('lang') or 'en_US'
660 lang = self.pool['res.lang']
661 formatted = lang.format(cr, uid, [lang_code], fmt.format(precision=precision), value, grouping=True)
663 # %f does not strip trailing zeroes. %g does but its precision causes
664 # it to switch to scientific notation starting at a million *and* to
665 # strip decimals. So use %f and if no precision was specified manually
667 if precision is None:
668 formatted = re.sub(r'(?:(0|\d+?)0+)$', r'\1', formatted)
671 class DateConverter(osv.AbstractModel):
672 _name = 'ir.qweb.field.date'
673 _inherit = 'ir.qweb.field'
675 def value_to_html(self, cr, uid, value, field, options=None, context=None):
676 if not value or len(value)<10: return ''
677 lang = self.user_lang(cr, uid, context=context)
678 locale = babel.Locale.parse(lang.code)
680 if isinstance(value, basestring):
681 value = datetime.datetime.strptime(
682 value[:10], openerp.tools.DEFAULT_SERVER_DATE_FORMAT)
684 if options and 'format' in options:
685 pattern = options['format']
687 strftime_pattern = lang.date_format
688 pattern = openerp.tools.posix_to_ldml(strftime_pattern, locale=locale)
690 return babel.dates.format_date(
691 value, format=pattern,
694 class DateTimeConverter(osv.AbstractModel):
695 _name = 'ir.qweb.field.datetime'
696 _inherit = 'ir.qweb.field'
698 def value_to_html(self, cr, uid, value, field, options=None, context=None):
699 if not value: return ''
700 lang = self.user_lang(cr, uid, context=context)
701 locale = babel.Locale.parse(lang.code)
703 if isinstance(value, basestring):
704 value = datetime.datetime.strptime(
705 value, openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT)
706 value = fields.datetime.context_timestamp(
707 cr, uid, timestamp=value, context=context)
709 if options and 'format' in options:
710 pattern = options['format']
712 strftime_pattern = (u"%s %s" % (lang.date_format, lang.time_format))
713 pattern = openerp.tools.posix_to_ldml(strftime_pattern, locale=locale)
715 if options and options.get('hide_seconds'):
716 pattern = pattern.replace(":ss", "").replace(":s", "")
718 return babel.dates.format_datetime(value, format=pattern, locale=locale)
720 class TextConverter(osv.AbstractModel):
721 _name = 'ir.qweb.field.text'
722 _inherit = 'ir.qweb.field'
724 def value_to_html(self, cr, uid, value, field, options=None, context=None):
726 Escapes the value and converts newlines to br. This is bullshit.
728 if not value: return ''
730 return nl2br(value, options=options)
732 class SelectionConverter(osv.AbstractModel):
733 _name = 'ir.qweb.field.selection'
734 _inherit = 'ir.qweb.field'
736 def record_to_html(self, cr, uid, field_name, record, options=None, context=None):
737 value = record[field_name]
738 if not value: return ''
739 field = record._fields[field_name]
740 selection = dict(field.get_description(record.env)['selection'])
741 return self.value_to_html(
742 cr, uid, selection[value], field, options=options)
744 class ManyToOneConverter(osv.AbstractModel):
745 _name = 'ir.qweb.field.many2one'
746 _inherit = 'ir.qweb.field'
748 def record_to_html(self, cr, uid, field_name, record, options=None, context=None):
749 [read] = record.read([field_name])
750 if not read[field_name]: return ''
751 _, value = read[field_name]
752 return nl2br(value, options=options)
754 class HTMLConverter(osv.AbstractModel):
755 _name = 'ir.qweb.field.html'
756 _inherit = 'ir.qweb.field'
758 def value_to_html(self, cr, uid, value, field, options=None, context=None):
759 return HTMLSafe(value or '')
761 class ImageConverter(osv.AbstractModel):
762 """ ``image`` widget rendering, inserts a data:uri-using image tag in the
763 document. May be overridden by e.g. the website module to generate links
766 .. todo:: what happens if different output need different converters? e.g.
767 reports may need embedded images or FS links whereas website
770 _name = 'ir.qweb.field.image'
771 _inherit = 'ir.qweb.field'
773 def value_to_html(self, cr, uid, value, field, options=None, context=None):
775 image = Image.open(cStringIO.StringIO(value.decode('base64')))
778 raise ValueError("Non-image binary fields can not be converted to HTML")
779 except: # image.verify() throws "suitable exceptions", I have no idea what they are
780 raise ValueError("Invalid image content")
782 return HTMLSafe('<img src="data:%s;base64,%s">' % (Image.MIME[image.format], value))
784 class MonetaryConverter(osv.AbstractModel):
785 """ ``monetary`` converter, has a mandatory option
786 ``display_currency``.
788 The currency is used for formatting *and rounding* of the float value. It
789 is assumed that the linked res_currency has a non-empty rounding value and
790 res.currency's ``round`` method is used to perform rounding.
792 .. note:: the monetary converter internally adds the qweb context to its
793 options mapping, so that the context is available to callees.
794 It's set under the ``_qweb_context`` key.
796 _name = 'ir.qweb.field.monetary'
797 _inherit = 'ir.qweb.field'
799 def to_html(self, cr, uid, field_name, record, options,
800 source_element, t_att, g_att, qweb_context, context=None):
801 options['_qweb_context'] = qweb_context
802 return super(MonetaryConverter, self).to_html(
803 cr, uid, field_name, record, options,
804 source_element, t_att, g_att, qweb_context, context=context)
806 def record_to_html(self, cr, uid, field_name, record, options, context=None):
809 Currency = self.pool['res.currency']
810 display_currency = self.display_currency(cr, uid, options['display_currency'], options)
812 # lang.format mandates a sprintf-style format. These formats are non-
813 # minimal (they have a default fixed precision instead), and
814 # lang.format will not set one by default. currency.round will not
815 # provide one either. So we need to generate a precision value
816 # (integer > 0) from the currency's rounding (a float generally < 1.0).
818 # The log10 of the rounding should be the number of digits involved if
819 # negative, if positive clamp to 0 digits and call it a day.
820 # nb: int() ~ floor(), we want nearest rounding instead
821 precision = int(round(math.log10(display_currency.rounding)))
822 fmt = "%.{0}f".format(-precision if precision < 0 else 0)
824 from_amount = record[field_name]
826 if options.get('from_currency'):
827 from_currency = self.display_currency(cr, uid, options['from_currency'], options)
828 from_amount = Currency.compute(cr, uid, from_currency.id, display_currency.id, from_amount)
830 lang_code = context.get('lang') or 'en_US'
831 lang = self.pool['res.lang']
832 formatted_amount = lang.format(cr, uid, [lang_code],
833 fmt, Currency.round(cr, uid, display_currency, from_amount),
834 grouping=True, monetary=True)
837 if display_currency.position == 'before':
842 return HTMLSafe(u'{pre}<span class="oe_currency_value">{0}</span>{post}'.format(
846 symbol=display_currency.symbol,
849 def display_currency(self, cr, uid, currency, options):
850 return self.qweb_object().eval_object(
851 currency, options['_qweb_context'])
854 ('year', 3600 * 24 * 365),
855 ('month', 3600 * 24 * 30),
856 ('week', 3600 * 24 * 7),
862 class DurationConverter(osv.AbstractModel):
863 """ ``duration`` converter, to display integral or fractional values as
864 human-readable time spans (e.g. 1.5 as "1 hour 30 minutes").
866 Can be used on any numerical field.
868 Has a mandatory option ``unit`` which can be one of ``second``, ``minute``,
869 ``hour``, ``day``, ``week`` or ``year``, used to interpret the numerical
870 field value before converting it.
872 Sub-second values will be ignored.
874 _name = 'ir.qweb.field.duration'
875 _inherit = 'ir.qweb.field'
877 def value_to_html(self, cr, uid, value, field, options=None, context=None):
878 units = dict(TIMEDELTA_UNITS)
880 raise ValueError(_("Durations can't be negative"))
881 if not options or options.get('unit') not in units:
882 raise ValueError(_("A unit must be provided to duration widgets"))
884 locale = babel.Locale.parse(
885 self.user_lang(cr, uid, context=context).code)
886 factor = units[options['unit']]
890 for unit, secs_per_unit in TIMEDELTA_UNITS:
891 v, r = divmod(r, secs_per_unit)
893 section = babel.dates.format_timedelta(
894 v*secs_per_unit, threshold=1, locale=locale)
896 sections.append(section)
897 return ' '.join(sections)
900 class RelativeDatetimeConverter(osv.AbstractModel):
901 _name = 'ir.qweb.field.relative'
902 _inherit = 'ir.qweb.field'
904 def value_to_html(self, cr, uid, value, field, options=None, context=None):
905 parse_format = openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT
906 locale = babel.Locale.parse(
907 self.user_lang(cr, uid, context=context).code)
909 if isinstance(value, basestring):
910 value = datetime.datetime.strptime(value, parse_format)
912 # value should be a naive datetime in UTC. So is fields.Datetime.now()
913 reference = datetime.datetime.strptime(field.now(), parse_format)
915 return babel.dates.format_timedelta(
916 value - reference, add_direction=True, locale=locale)
918 class Contact(orm.AbstractModel):
919 _name = 'ir.qweb.field.contact'
920 _inherit = 'ir.qweb.field.many2one'
922 def record_to_html(self, cr, uid, field_name, record, options=None, context=None):
928 opf = options.get('fields') or ["name", "address", "phone", "mobile", "fax", "email"]
930 value_rec = record[field_name]
933 value_rec = value_rec.sudo().with_context(show_address=True)
934 value = value_rec.name_get()[0][1]
937 'name': value.split("\n")[0],
938 'address': escape("\n".join(value.split("\n")[1:])),
939 'phone': value_rec.phone,
940 'mobile': value_rec.mobile,
941 'fax': value_rec.fax,
942 'city': value_rec.city,
943 'country_id': value_rec.country_id.display_name,
944 'website': value_rec.website,
945 'email': value_rec.email,
951 html = self.pool["ir.ui.view"].render(cr, uid, "base.contact", val, engine='ir.qweb', context=context).decode('utf8')
953 return HTMLSafe(html)
955 class QwebView(orm.AbstractModel):
956 _name = 'ir.qweb.field.qweb'
957 _inherit = 'ir.qweb.field.many2one'
959 def record_to_html(self, cr, uid, field_name, record, options=None, context=None):
960 if not getattr(record, field_name):
963 view = getattr(record, field_name)
965 if view._model._name != "ir.ui.view":
966 _logger.warning("%s.%s must be a 'ir.ui.view' model." % (record, field_name))
969 ctx = (context or {}).copy()
970 ctx['object'] = record
971 html = view.render(ctx, engine='ir.qweb', context=ctx).decode('utf8')
973 return HTMLSafe(html)
975 class QwebWidget(osv.AbstractModel):
976 _name = 'ir.qweb.widget'
978 def _format(self, inner, options, qwebcontext):
979 return self.pool['ir.qweb'].eval_str(inner, qwebcontext)
981 def format(self, inner, options, qwebcontext):
982 return escape(self._format(inner, options, qwebcontext))
984 class QwebWidgetMonetary(osv.AbstractModel):
985 _name = 'ir.qweb.widget.monetary'
986 _inherit = 'ir.qweb.widget'
988 def _format(self, inner, options, qwebcontext):
989 inner = self.pool['ir.qweb'].eval(inner, qwebcontext)
990 display = self.pool['ir.qweb'].eval_object(options['display_currency'], qwebcontext)
991 precision = int(round(math.log10(display.rounding)))
992 fmt = "%.{0}f".format(-precision if precision < 0 else 0)
993 lang_code = qwebcontext.context.get('lang') or 'en_US'
994 formatted_amount = self.pool['res.lang'].format(
995 qwebcontext.cr, qwebcontext.uid, [lang_code], fmt, inner, grouping=True, monetary=True
998 if display.position == 'before':
1003 return u'{pre}{0}{post}'.format(
1004 formatted_amount, pre=pre, post=post
1005 ).format(symbol=display.symbol,)
1007 class HTMLSafe(object):
1008 """ HTMLSafe string wrapper, Werkzeug's escape() has special handling for
1009 objects with a ``__html__`` methods but AFAIK does not provide any such
1012 Wrapping a string in HTML will prevent its escaping
1014 __slots__ = ['string']
1015 def __init__(self, string):
1016 self.string = string
1021 if isinstance(s, unicode):
1022 return s.encode('utf-8')
1024 def __unicode__(self):
1026 if isinstance(s, str):
1027 return s.decode('utf-8')
1030 def nl2br(string, options=None):
1031 """ Converts newlines to HTML linebreaks in ``string``. Automatically
1032 escapes content unless options['html-escape'] is set to False, and returns
1033 the result wrapped in an HTMLSafe object.
1036 :param dict options:
1039 if options is None: options = {}
1041 if options.get('html-escape', True):
1042 string = escape(string)
1043 return HTMLSafe(string.replace('\n', '<br>\n'))
1045 def get_field_type(field, options):
1046 """ Gets a t-field's effective type from the field definition and its options """
1047 return options.get('widget', field.type)
1049 class AssetError(Exception):
1051 class AssetNotFound(AssetError):
1054 class AssetsBundle(object):
1055 # Sass installation:
1057 # sudo gem install sass compass bootstrap-sass
1059 # If the following error is encountered:
1060 # 'ERROR: Cannot load compass.'
1062 # sudo gem install compass --pre
1063 cmd_sass = ['sass', '--stdin', '-t', 'compressed', '--unix-newlines', '--compass', '-r', 'bootstrap-sass']
1064 rx_css_import = re.compile("(@import[^;{]+;?)", re.M)
1065 rx_sass_import = re.compile("""(@import\s?['"]([^'"]+)['"])""")
1066 rx_css_split = re.compile("\/\*\! ([a-f0-9-]+) \*\/")
1068 def __init__(self, xmlid, debug=False, cr=None, uid=None, context=None, registry=None):
1070 self.cr = request.cr if cr is None else cr
1071 self.uid = request.uid if uid is None else uid
1072 self.context = request.context if context is None else context
1073 self.registry = request.registry if registry is None else registry
1074 self.javascripts = []
1075 self.stylesheets = []
1076 self.css_errors = []
1078 self._checksum = None
1080 context = self.context.copy()
1081 context['inherit_branding'] = False
1082 context['rendering_bundle'] = True
1083 self.html = self.registry['ir.ui.view'].render(self.cr, self.uid, xmlid, context=context)
1087 fragments = html.fragments_fromstring(self.html)
1088 for el in fragments:
1089 if isinstance(el, basestring):
1090 self.remains.append(el)
1091 elif isinstance(el, html.HtmlElement):
1092 src = el.get('src', '')
1093 href = el.get('href', '')
1094 atype = el.get('type')
1095 media = el.get('media')
1096 if el.tag == 'style':
1097 if atype == 'text/sass' or src.endswith('.sass'):
1098 self.stylesheets.append(SassAsset(self, inline=el.text, media=media))
1100 self.stylesheets.append(StylesheetAsset(self, inline=el.text, media=media))
1101 elif el.tag == 'link' and el.get('rel') == 'stylesheet' and self.can_aggregate(href):
1102 if href.endswith('.sass') or atype == 'text/sass':
1103 self.stylesheets.append(SassAsset(self, url=href, media=media))
1105 self.stylesheets.append(StylesheetAsset(self, url=href, media=media))
1106 elif el.tag == 'script' and not src:
1107 self.javascripts.append(JavascriptAsset(self, inline=el.text))
1108 elif el.tag == 'script' and self.can_aggregate(src):
1109 self.javascripts.append(JavascriptAsset(self, url=src))
1111 self.remains.append(html.tostring(el))
1114 self.remains.append(html.tostring(el))
1116 # notYETimplementederror
1117 raise NotImplementedError
1119 def can_aggregate(self, url):
1120 return not urlparse(url).netloc and not url.startswith(('/web/css', '/web/js'))
1122 def to_html(self, sep=None, css=True, js=True, debug=False):
1127 if css and self.stylesheets:
1129 for style in self.stylesheets:
1130 response.append(style.to_html())
1132 for jscript in self.javascripts:
1133 response.append(jscript.to_html())
1135 url_for = self.context.get('url_for', lambda url: url)
1136 if css and self.stylesheets:
1137 href = '/web/css/%s/%s' % (self.xmlid, self.version)
1138 response.append('<link href="%s" rel="stylesheet"/>' % url_for(href))
1140 src = '/web/js/%s/%s' % (self.xmlid, self.version)
1141 response.append('<script type="text/javascript" src="%s"></script>' % url_for(src))
1142 response.extend(self.remains)
1143 return sep + sep.join(response)
1146 def last_modified(self):
1147 """Returns last modified date of linked files"""
1148 return max(itertools.chain(
1149 (asset.last_modified for asset in self.javascripts),
1150 (asset.last_modified for asset in self.stylesheets),
1155 return self.checksum[0:7]
1160 Not really a full checksum.
1161 We compute a SHA1 on the rendered bundle + max linked files last_modified date
1163 check = self.html + str(self.last_modified)
1164 return hashlib.sha1(check).hexdigest()
1167 content = self.get_cache('js')
1169 content = ';\n'.join(asset.minify() for asset in self.javascripts)
1170 self.set_cache('js', content)
1174 content = self.get_cache('css')
1177 content = '\n'.join(asset.minify() for asset in self.stylesheets)
1180 msg = '\n'.join(self.css_errors)
1181 content += self.css_message(msg.replace('\n', '\\A '))
1183 # move up all @import rules to the top
1186 matches.append(matchobj.group(0))
1189 content = re.sub(self.rx_css_import, push, content)
1191 matches.append(content)
1192 content = u'\n'.join(matches)
1195 self.set_cache('css', content)
1199 def get_cache(self, type):
1201 domain = [('url', '=', '/web/%s/%s/%s' % (type, self.xmlid, self.version))]
1202 bundle = self.registry['ir.attachment'].search_read(self.cr, openerp.SUPERUSER_ID, domain, ['datas'], context=self.context)
1203 if bundle and bundle[0]['datas']:
1204 content = bundle[0]['datas'].decode('base64')
1207 def set_cache(self, type, content):
1208 ira = self.registry['ir.attachment']
1209 url_prefix = '/web/%s/%s/' % (type, self.xmlid)
1210 # Invalidate previous caches
1211 oids = ira.search(self.cr, openerp.SUPERUSER_ID, [('url', '=like', url_prefix + '%')], context=self.context)
1213 ira.unlink(self.cr, openerp.SUPERUSER_ID, oids, context=self.context)
1214 url = url_prefix + self.version
1215 ira.create(self.cr, openerp.SUPERUSER_ID, dict(
1216 datas=content.encode('utf8').encode('base64'),
1220 ), context=self.context)
1222 def css_message(self, message):
1228 font-family: monospace;
1232 """ % message.replace('"', '\\"')
1234 def compile_sass(self):
1236 Checks if the bundle contains any sass content, then compiles it to css.
1237 Css compilation is done at the bundle level and not in the assets
1238 because they are potentially interdependant.
1240 sass = [asset for asset in self.stylesheets if isinstance(asset, SassAsset)]
1243 source = '\n'.join([asset.get_source() for asset in sass])
1245 # move up all @import rules to the top and exclude file imports
1248 ref = matchobj.group(2)
1249 line = '@import "%s"' % ref
1250 if '.' not in ref and line not in imports and not ref.startswith(('.', '/', '~')):
1251 imports.append(line)
1253 source = re.sub(self.rx_sass_import, push, source)
1254 imports.append(source)
1255 source = u'\n'.join(imports)
1258 compiler = Popen(self.cmd_sass, stdin=PIPE, stdout=PIPE, stderr=PIPE)
1260 msg = "Could not find 'sass' program needed to compile sass/scss files"
1262 self.css_errors.append(msg)
1264 result = compiler.communicate(input=source.encode('utf-8'))
1265 if compiler.returncode:
1266 error = self.get_sass_error(''.join(result), source=source)
1267 _logger.warning(error)
1268 self.css_errors.append(error)
1270 compiled = result[0].strip().decode('utf8')
1271 fragments = self.rx_css_split.split(compiled)[1:]
1273 asset_id = fragments.pop(0)
1274 asset = next(asset for asset in sass if asset.id == asset_id)
1275 asset._content = fragments.pop(0)
1277 def get_sass_error(self, stderr, source=None):
1278 # TODO: try to find out which asset the error belongs to
1279 error = stderr.split('Load paths')[0].replace(' Use --trace for backtrace.', '')
1280 error += "This error occured while compiling the bundle '%s' containing:" % self.xmlid
1281 for asset in self.stylesheets:
1282 if isinstance(asset, SassAsset):
1283 error += '\n - %s' % (asset.url if asset.url else '<inline sass>')
1286 class WebAsset(object):
1289 def __init__(self, bundle, inline=None, url=None):
1290 self.id = str(uuid.uuid4())
1291 self.bundle = bundle
1292 self.inline = inline
1295 self.uid = bundle.uid
1296 self.registry = bundle.registry
1297 self.context = bundle.context
1298 self._content = None
1299 self._filename = None
1300 self._ir_attach = None
1301 name = '<inline asset>' if inline else url
1302 self.name = "%s defined in bundle '%s'" % (name, bundle.xmlid)
1303 if not inline and not url:
1304 raise Exception("An asset should either be inlined or url linked")
1307 if not (self.inline or self._filename or self._ir_attach):
1308 addon = filter(None, self.url.split('/'))[0]
1310 # Test url against modules static assets
1311 mpath = openerp.http.addons_manifest[addon]['addons_path']
1312 self._filename = mpath + self.url.replace('/', os.path.sep)
1315 # Test url against ir.attachments
1316 fields = ['__last_update', 'datas', 'mimetype']
1317 domain = [('type', '=', 'binary'), ('url', '=', self.url)]
1318 ira = self.registry['ir.attachment']
1319 attach = ira.search_read(self.cr, openerp.SUPERUSER_ID, domain, fields, context=self.context)
1320 self._ir_attach = attach[0]
1322 raise AssetNotFound("Could not find %s" % self.name)
1325 raise NotImplementedError()
1328 def last_modified(self):
1332 return datetime.datetime.fromtimestamp(os.path.getmtime(self._filename))
1333 elif self._ir_attach:
1334 server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
1335 last_update = self._ir_attach['__last_update']
1337 return datetime.datetime.strptime(last_update, server_format + '.%f')
1339 return datetime.datetime.strptime(last_update, server_format)
1342 return datetime.datetime(1970, 1, 1)
1346 if not self._content:
1347 self._content = self.inline or self._fetch_content()
1348 return self._content
1350 def _fetch_content(self):
1351 """ Fetch content from file or database"""
1355 with open(self._filename, 'rb') as fp:
1356 return fp.read().decode('utf-8')
1358 return self._ir_attach['datas'].decode('base64')
1359 except UnicodeDecodeError:
1360 raise AssetError('%s is not utf-8 encoded.' % self.name)
1362 raise AssetNotFound('File %s does not exist.' % self.name)
1364 raise AssetError('Could not get content for %s.' % self.name)
1369 def with_header(self, content=None):
1371 content = self.content
1372 return '\n/* %s */\n%s' % (self.name, content)
1374 class JavascriptAsset(WebAsset):
1376 return self.with_header(rjsmin(self.content))
1378 def _fetch_content(self):
1380 return super(JavascriptAsset, self)._fetch_content()
1381 except AssetError, e:
1382 return "console.error(%s);" % json.dumps(e.message)
1386 return '<script type="text/javascript" src="%s"></script>' % (self.html_url % self.url)
1388 return '<script type="text/javascript" charset="utf-8">%s</script>' % self.with_header()
1390 class StylesheetAsset(WebAsset):
1391 rx_import = re.compile(r"""@import\s+('|")(?!'|"|/|https?://)""", re.U)
1392 rx_url = re.compile(r"""url\s*\(\s*('|"|)(?!'|"|/|https?://|data:)""", re.U)
1393 rx_sourceMap = re.compile(r'(/\*# sourceMappingURL=.*)', re.U)
1394 rx_charset = re.compile(r'(@charset "[^"]+";)', re.U)
1396 def __init__(self, *args, **kw):
1397 self.media = kw.pop('media', None)
1398 super(StylesheetAsset, self).__init__(*args, **kw)
1402 content = super(StylesheetAsset, self).content
1404 content = '@media %s { %s }' % (self.media, content)
1407 def _fetch_content(self):
1409 content = super(StylesheetAsset, self)._fetch_content()
1410 web_dir = os.path.dirname(self.url)
1412 content = self.rx_import.sub(
1413 r"""@import \1%s/""" % (web_dir,),
1417 content = self.rx_url.sub(
1418 r"url(\1%s/" % (web_dir,),
1422 # remove charset declarations, we only support utf-8
1423 content = self.rx_charset.sub('', content)
1424 except AssetError, e:
1425 self.bundle.css_errors.append(e.message)
1430 # remove existing sourcemaps, make no sense after re-mini
1431 content = self.rx_sourceMap.sub('', self.content)
1433 content = re.sub(r'/\*.*?\*/', '', content, flags=re.S)
1435 content = re.sub(r'\s+', ' ', content)
1436 content = re.sub(r' *([{}]) *', r'\1', content)
1437 return self.with_header(content)
1440 media = (' media="%s"' % werkzeug.utils.escape(self.media)) if self.media else ''
1442 href = self.html_url % self.url
1443 return '<link rel="stylesheet" href="%s" type="text/css"%s/>' % (href, media)
1445 return '<style type="text/css"%s>%s</style>' % (media, self.with_header())
1447 class SassAsset(StylesheetAsset):
1449 rx_indent = re.compile(r'^( +|\t+)', re.M)
1454 return self.with_header()
1458 ira = self.registry['ir.attachment']
1459 url = self.html_url % self.url
1460 domain = [('type', '=', 'binary'), ('url', '=', self.url)]
1461 ira_id = ira.search(self.cr, openerp.SUPERUSER_ID, domain, context=self.context)
1463 # TODO: update only if needed
1464 ira.write(self.cr, openerp.SUPERUSER_ID, [ira_id], {'datas': self.content}, context=self.context)
1466 ira.create(self.cr, openerp.SUPERUSER_ID, dict(
1467 datas=self.content.encode('utf8').encode('base64'),
1468 mimetype='text/css',
1472 ), context=self.context)
1473 return super(SassAsset, self).to_html()
1475 def get_source(self):
1476 content = textwrap.dedent(self.inline or self._fetch_content())
1480 if self.indent is None:
1482 if self.indent == self.reindent:
1483 # Don't reindent the file if identation is the final one (reindent)
1484 raise StopIteration()
1485 return ind.replace(self.indent, self.reindent)
1488 content = self.rx_indent.sub(fix_indent, content)
1489 except StopIteration:
1491 return "/*! %s */\n%s" % (self.id, content)
1494 """ Minify js with a clever regex.
1495 Taken from http://opensource.perlig.de/rjsmin
1496 Apache License, Version 2.0 """
1498 """ Substitution callback """
1499 groups = match.groups()
1505 (groups[4] and '\n') or
1506 (groups[5] and ' ') or
1507 (groups[6] and ' ') or
1508 (groups[7] and ' ') or
1513 r'([^\047"/\000-\040]+)|((?:(?:\047[^\047\\\r\n]*(?:\\(?:[^\r\n]|\r?'
1514 r'\n|\r)[^\047\\\r\n]*)*\047)|(?:"[^"\\\r\n]*(?:\\(?:[^\r\n]|\r?\n|'
1515 r'\r)[^"\\\r\n]*)*"))[^\047"/\000-\040]*)|(?:(?<=[(,=:\[!&|?{};\r\n]'
1516 r')(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/'
1517 r'))*((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*'
1518 r'(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/)[^\047"/\000-\040]*'
1519 r'))|(?:(?<=[\000-#%-,./:-@\[-^`{-~-]return)(?:[\000-\011\013\014\01'
1520 r'6-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*((?:/(?![\r\n/*])[^/'
1521 r'\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]'
1522 r'*)*\]))[^/\\\[\r\n]*)*/)[^\047"/\000-\040]*))|(?<=[^\000-!#%&(*,./'
1523 r':-@\[\\^`{|~])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/'
1524 r'*][^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))(?:[\000-\011\013\01'
1525 r'4\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040"#'
1526 r'%-\047)*,./:-@\\-^`|-~])|(?<=[^\000-#%-,./:-@\[-^`{-~-])((?:[\000-'
1527 r'\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=[^'
1528 r'\000-#%-,./:-@\[-^`{-~-])|(?<=\+)((?:[\000-\011\013\014\016-\040]|'
1529 r'(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=\+)|(?<=-)((?:[\000-\011\0'
1530 r'13\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=-)|(?:[\0'
1531 r'00-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+|(?:'
1532 r'(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*'
1533 r']*\*+(?:[^/*][^*]*\*+)*/))*)+', subber, '\n%s\n' % script