Commit ffcfd26d authored by Nicolas Delaby's avatar Nicolas Delaby

Create function to escape OOo text nodes

  * clean up duplicated code
  * Compile REGEX only once
  * implement render_odt_view for ListWidget


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@32855 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 8cb1f624
...@@ -26,6 +26,23 @@ NSMAP = { ...@@ -26,6 +26,23 @@ NSMAP = {
EForm = ElementMaker(namespace=FORM_URI, nsmap=NSMAP) 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: class Widget:
"""A field widget that knows how to display itself as HTML. """A field widget that knows how to display itself as HTML.
""" """
...@@ -243,18 +260,9 @@ class Widget: ...@@ -243,18 +260,9 @@ class Widget:
draw_node.append(text_p_node) draw_node.append(text_p_node)
draw_frame_node.append(draw_node) draw_frame_node.append(draw_node)
# XXX copy from render_odt_view, need to be unified global parent_node
def replaceCharsByNode(match_object): parent_node = text_span_node
#global text_span_node RE_OOO_ESCAPE.sub(ooo_escape, value)
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)
if as_string: if as_string:
return etree.tostring(draw_frame_node) return etree.tostring(draw_frame_node)
return draw_frame_node return draw_frame_node
...@@ -530,17 +538,10 @@ class TextAreaWidget(Widget): ...@@ -530,17 +538,10 @@ class TextAreaWidget(Widget):
#required by lxml #required by lxml
value = value.decode('utf-8') value = value.decode('utf-8')
text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP) text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP)
def replaceCharsByNode(match_object):
#global text_node global parent_node
if match_object.group(1) is None: parent_node = text_node
text_node.text = match_object.group(2) RE_OOO_ESCAPE.sub(ooo_escape, value)
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)
text_node.attrib.update(attr_dict) text_node.attrib.update(attr_dict)
if as_string: if as_string:
return etree.tostring(text_node) return etree.tostring(text_node)
...@@ -946,6 +947,25 @@ class ListWidget(SingleItemsWidget): ...@@ -946,6 +947,25 @@ class ListWidget(SingleItemsWidget):
return render_element('option', contents=text, value=value, return render_element('option', contents=text, value=value,
selected=None, extra=extra_item) 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() ListWidgetInstance = ListWidget()
class MultiListWidget(MultiItemsWidget): class MultiListWidget(MultiItemsWidget):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment