1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from lxml import etree
24 rml_parents = ['tr','story','section']
25 html_parents = ['tr','body','div']
26 sxw_parents = ['{http://openoffice.org/2000/table}table-row','{http://openoffice.org/2000/office}body','{http://openoffice.org/2000/text}section']
27 odt_parents = ['{urn:oasis:names:tc:opendocument:xmlns:office:1.0}body','{urn:oasis:names:tc:opendocument:xmlns:table:1.0}table-row','{urn:oasis:names:tc:opendocument:xmlns:text:1.0}section']
30 def preprocess_rml(self, root_node,type='pdf'):
31 _regex1 = re.compile("\[\[(.*?)(repeatIn\(.*?\s*,\s*[\'\"].*?[\'\"]\s*(?:,\s*(.*?)\s*)?\s*\))(.*?)\]\]")
32 _regex11= re.compile("\[\[(.*?)(repeatIn\(.*?\s*\(.*?\s*[\'\"].*?[\'\"]\s*\),[\'\"].*?[\'\"](?:,\s*(.*?)\s*)?\s*\))(.*?)\]\]")
33 _regex2 = re.compile("\[\[(.*?)(removeParentNode\(\s*(?:['\"](.*?)['\"])\s*\))(.*?)\]\]")
34 _regex3 = re.compile("\[\[\s*(.*?setTag\(\s*['\"](.*?)['\"]\s*,\s*['\"].*?['\"]\s*(?:,.*?)?\).*?)\s*\]\]")
35 for node in root_node:
36 if node.tag == etree.Comment:
38 if node.text or node.tail:
41 while n.tag != txt.group(2):
43 n.set('rml_tag', txt.group(1))
49 while n.tag != txt.group(3):
55 n.set('rml_except', txt.group(0)[2:-2])
58 if len(txt.group(4)) > 1:
65 if type =='html2html':
68 group_3 = txt.group(3)
69 if group_3.startswith("'") or group_3.startswith('"'):
70 group_3 = group_3[1:-1]
73 while n.tag not in match:
75 n.set('rml_loop', txt.group(2))
76 return '[['+txt.group(1)+"''"+txt.group(4)+']]'
77 t = _regex1.sub(_sub1, node.text or node.tail)
79 t = _regex11.sub(_sub1, node.text or node.tail)
80 t = _regex3.sub(_sub3, t)
81 node.text = _regex2.sub(_sub2, t)
82 self.preprocess_rml(node,type)
85 if __name__=='__main__':
86 node = etree.XML('''<story>
87 <para>This is a test[[ setTag('para','xpre') ]]</para>
90 <td><para>Row 1 [[ setTag('tr','tr',{'style':'TrLevel'+str(a['level']), 'paraStyle':('Level'+str(a['level']))}) ]] </para></td>
91 <td>Row 2 [[ True and removeParentNode('td') ]] </td>
93 <td>Row 1 [[repeatIn(o.order_line,'o')]] </td>
100 result = a.preprocess_rml(node)
101 print etree.tostring(result)
104 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: