[Erp5-report] r32855 nicolas - /erp5/trunk/products/Formulator/Widget.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Fri Feb 19 13:59:35 CET 2010
Author: nicolas
Date: Fri Feb 19 13:59:34 2010
New Revision: 32855
URL: http://svn.erp5.org?rev=32855&view=rev
Log:
Create function to escape OOo text nodes
* clean up duplicated code
* Compile REGEX only once
* implement render_odt_view for ListWidget
Modified:
erp5/trunk/products/Formulator/Widget.py
Modified: erp5/trunk/products/Formulator/Widget.py
URL: http://svn.erp5.org/erp5/trunk/products/Formulator/Widget.py?rev=32855&r1=32854&r2=32855&view=diff
==============================================================================
--- erp5/trunk/products/Formulator/Widget.py [utf8] (original)
+++ erp5/trunk/products/Formulator/Widget.py [utf8] Fri Feb 19 13:59:34 2010
@@ -25,6 +25,23 @@
}
EForm = ElementMaker(namespace=FORM_URI, nsmap=NSMAP)
+
+RE_OOO_ESCAPE = re.compile(r'([\n\t])?([^\n\t]*)')
+def ooo_escape(match_object):
+ """Replacement function to use inside re.sub expression.
+ This function replace \t by <text:tab/>
+ \n by <text:line-break/>
+ - parent_node is a global variable used to carry new nodes.
+ """
+ match_value = match_object.group(1)
+ if match_value is None:
+ parent_node.text = match_object.group(2)
+ elif match_value == '\n':
+ line_break = SubElement(parent_node, '{%s}%s' % (TEXT_URI, 'line-break'))
+ line_break.tail = match_object.group(2)
+ elif match_value == '\t':
+ line_break = SubElement(parent_node, '{%s}%s' % (TEXT_URI, 'tab'))
+ line_break.tail = match_object.group(2)
class Widget:
"""A field widget that knows how to display itself as HTML.
@@ -243,18 +260,9 @@
draw_node.append(text_p_node)
draw_frame_node.append(draw_node)
- # XXX copy from render_odt_view, need to be unified
- def replaceCharsByNode(match_object):
- #global text_span_node
- if match_object.group(1) is None:
- text_span_node.text = match_object.group(2)
- if match_object.group(1) == '\n':
- line_break = SubElement(text_span_node, '{%s}%s' % (TEXT_URI, 'line-break'))
- line_break.tail = match_object.group(2)
- if match_object.group(1) == '\t':
- line_break = SubElement(text_span_node, '{%s}%s' % (TEXT_URI, 'tab'))
- line_break.tail = match_object.group(2)
- re.sub('([\n\t])?([^\n\t]*)', replaceCharsByNode, value)
+ global parent_node
+ parent_node = text_span_node
+ RE_OOO_ESCAPE.sub(ooo_escape, value)
if as_string:
return etree.tostring(draw_frame_node)
return draw_frame_node
@@ -530,17 +538,10 @@
#required by lxml
value = value.decode('utf-8')
text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP)
- def replaceCharsByNode(match_object):
- #global text_node
- if match_object.group(1) is None:
- text_node.text = match_object.group(2)
- if match_object.group(1) == '\n':
- line_break = SubElement(text_node, '{%s}%s' % (TEXT_URI, 'line-break'))
- line_break.tail = match_object.group(2)
- if match_object.group(1) == '\t':
- line_break = SubElement(text_node, '{%s}%s' % (TEXT_URI, 'tab'))
- line_break.tail = match_object.group(2)
- re.sub('([\n\t])?([^\n\t]*)', replaceCharsByNode, value)
+
+ global parent_node
+ parent_node = text_node
+ RE_OOO_ESCAPE.sub(ooo_escape, value)
text_node.attrib.update(attr_dict)
if as_string:
return etree.tostring(text_node)
@@ -945,6 +946,25 @@
def render_selected_item(self, text, value, key, css_class, extra_item):
return render_element('option', contents=text, value=value,
selected=None, extra=extra_item)
+
+ def render_odt_view(self, field, value, as_string, ooo_builder, REQUEST,
+ render_prefix, attr_dict, local_name):
+ if attr_dict is None:
+ attr_dict = {}
+ if value is None:
+ value = []
+ if isinstance(value, (list, tuple)):
+ value = '\n'.join(value)
+ if isinstance(value, str):
+ #required by lxml
+ value = value.decode('utf-8')
+ text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP)
+ global parent_node = text_node
+ RE_OOO_ESCAPE.sub(ooo_escape, value)
+ text_node.attrib.update(attr_dict)
+ if as_string:
+ return etree.tostring(text_node)
+ return text_node
ListWidgetInstance = ListWidget()
More information about the Erp5-report
mailing list