[pypy-svn] rev 1465 - pypy/trunk/doc/funding/tools

stephan at codespeak.net stephan at codespeak.net
Tue Sep 30 15:24:17 CEST 2003


Author: stephan
Date: Tue Sep 30 15:24:16 2003
New Revision: 1465

Added:
   pypy/trunk/doc/funding/tools/
   pypy/trunk/doc/funding/tools/format.py
Log:
added basic tool to replace table style

Added: pypy/trunk/doc/funding/tools/format.py
==============================================================================
--- (empty file)
+++ pypy/trunk/doc/funding/tools/format.py	Tue Sep 30 15:24:16 2003
@@ -0,0 +1,167 @@
+import re
+import sys
+
+START = re.compile(r'\s*<[^/][^<]*?>',re.DOTALL)
+STOP = re.compile(r'\s*</[^<]*?>',re.DOTALL)
+EMPTY = re.compile(r'\s*<[^/][^<]*?/>',re.DOTALL)
+OTHER = re.compile(r'\s*<[!\?][^<]*>')
+ANY = re.compile(r'[^<]*',re.DOTALL)
+tab = '  '
+
+tabprop = """<style:properties fo:padding-left="0.191cm" fo:padding-right="0.191cm" fo:padding-top="0cm" fo:padding-bottom="0cm" fo:border-left="0.035cm solid #000000" fo:border-right="none" fo:border-top="0.035cm solid #000000" fo:border-bottom="none"/>"""
+
+class parse(object):
+    def __init__(self,text):
+        self.text = text
+
+    def start(self,txt):pass
+    def stop(self,txt):pass
+    def empty(self,txt):pass
+    def other(self,txt):pass
+    def any(self,txt):pass
+    def parse(self):
+        content = self.text[:]
+        while content:
+            m = OTHER.match(content)
+            if m is not None:
+                #print 'OTHER:',m.group()
+                content = content[m.span()[1]:]
+                self.other(m.group())
+                continue
+            m = EMPTY.match(content)
+            if m is not None:
+                #print 'EMPTY:',m.group()
+                content = content[m.span()[1]:]
+                self.empty(m.group())
+                continue
+            m = START.match(content)
+            if m is not None:
+                #print 'START:',m.group()
+                content = content[m.span()[1]:]
+                self.start(m.group())
+                continue
+            m = STOP.match(content)
+            if m is not None:
+                #print 'STOP:',m.group()
+                content = content[m.span()[1]:]
+                self.stop(m.group())
+                continue
+            m = ANY.match(content)
+            if m is not None:
+                #print 'ANY:',m.group()
+                txt = m.group()
+                txt.strip()
+                self.any(txt)
+                content = content[m.span()[1]:]
+                continue
+
+class format(parse):
+    def __init__(self,*argl,**argd):
+        super(format,self).__init__(*argl,**argd)
+        self.level = 0
+        self.outbuf = []
+
+    def __str__(self):
+        self.parse()
+        return '\n'.join(self.outbuf)
+
+    def start(self,txt):
+        self.outbuf.append(tab*self.level+txt)
+        self.level += 1
+
+    def stop(self,txt):
+        self.level -= 1
+        self.outbuf.append(tab*self.level+txt)
+
+    def any(self,txt):
+        self.outbuf.append(tab*self.level+txt)
+
+    def empty(self,txt):
+        self.outbuf.append(tab*self.level+txt)
+
+    def other(self,txt):
+        self.outbuf.append(tab*self.level+txt)
+
+class findTableName(parse):
+    def __init__(self,*argl,**argd):
+        super(findTableName,self).__init__(*argl,**argd)
+        self.tabname = {}
+        self.currTable = ''
+
+    def any(self,txt):
+        if self.currTable and txt.find('Workpackage number') != -1:
+            self.tabname[self.currTable] = True
+
+    def stop(self,txt):
+        if txt.startswith('</table:table'):
+            self.currTable = ''
+
+    def start(self,txt):
+        if txt.startswith('<table:table'):
+            i = txt.find('table:name')
+            if i != -1:
+                start = txt.find('"',i) +1
+                stop = txt.find('"',start)
+                self.currTable = txt[start:stop]
+
+    def __str__(self):
+        self.parse()
+        return str(self.tabname)
+
+    def __call__(self):
+        self.parse()
+        return self.tabname.keys()
+
+class replaceMargin(format):
+    def __init__(self,*argl,**argd):
+        super(replaceMargin,self).__init__(*argl,**argd)
+        self.tabnames = findTableName(self.text)()
+        print >> sys.stderr,self.tabnames
+        self.inStyle = True
+
+    def __str__(self):
+        self.parse()
+        return ''.join(self.outbuf)
+
+    def start(self,txt):
+        txt = txt.strip()
+        if txt.startswith('<style:style'):
+            for tab in self.tabnames:
+                if txt.find(tab) != -1 and txt.find('table-cell') != -1:
+                    print >> sys.stderr,'found table-cell'
+                    self.inStyle = True
+        self.outbuf.append(txt)
+
+    def stop(self,txt):
+        txt = txt.strip()
+        if self.inStyle:
+            self.inStyle = False
+        self.outbuf.append(txt)
+
+    def other(self,txt):
+        txt = txt.strip()
+        self.outbuf.append(txt)
+
+    def empty(self,txt):
+        txt = txt.strip()
+        if self.inStyle and txt.find('style:properties') != -1:
+            self.outbuf.append(tabprop)
+            self.inStyle = False
+        else:
+            self.outbuf.append(txt)
+
+    def any(self,txt):
+        txt = txt.strip()
+        if txt:
+            self.outbuf.append(txt)
+
+def changeStyle(txt):
+    f = format(str(replaceMargin(txt)))
+    return str(f)
+
+if __name__ == '__main__':
+    import sys
+    fn = sys.argv[1]
+    content = open(fn).read()
+    f = format(str(replaceMargin(content)))
+    print f


More information about the Pypy-commit mailing list