[Checkins] SVN: zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/t added EmtpyElementRemover and LinksToPrinceFootnotes transformations

Andreas Jung andreas at andreas-jung.com
Sun Dec 7 06:41:24 EST 2008


Log message for revision 93742:
  added EmtpyElementRemover and LinksToPrinceFootnotes transformations
  

Changed:
  U   zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/tests/testTransformation.py
  U   zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/transformation.py

-=-
Modified: zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/tests/testTransformation.py
===================================================================
--- zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/tests/testTransformation.py	2008-12-07 01:24:19 UTC (rev 93741)
+++ zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/tests/testTransformation.py	2008-12-07 11:41:23 UTC (rev 93742)
@@ -80,6 +80,19 @@
         self.assertEqual('<span>Control Panel</span>' in result, True)
         self.assertEqual('<span>foo bar</span>' in result, True)
 
+    def testEmptyElementRemover(self):
+        T = transformation.EmptyElementRemover('<div>hello world<div></div><p></p></div>') 
+        T.transform()
+        result = str(T)
+        self.assertEqual(result, '<div>hello world</div>')
+
+
+    def testLinksToPrinceFootnotes(self):
+        T = transformation.LinksToPrinceFootnotes('<a href="http://plone.org">plone.org</a>')
+        T.transform()
+        result = str(T)
+        self.assertEqual(result, '<span class="generated-footnote-text">plone.org<span class="generated-footnote">http://plone.org</span></span>')
+
 def test_suite():
     from unittest import TestSuite, makeSuite
     suite = TestSuite()

Modified: zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/transformation.py
===================================================================
--- zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/transformation.py	2008-12-07 01:24:19 UTC (rev 93741)
+++ zopyx.smartprintng.core/trunk/zopyx/smartprintng/core/transformation.py	2008-12-07 11:41:23 UTC (rev 93742)
@@ -164,7 +164,7 @@
         self.enumerateLinks()
         soup = self.soup
 
-        if links:                      
+        if links:
             pat = '<li>[%d] %s</li>'
             div = Tag(soup, 'div')
             div['id'] = 'enumerated-links'
@@ -213,6 +213,50 @@
         self.soup = BeautifulSoup(html2)
 
 
+class EmptyElementRemover(BaseTransform):
+    implements(IHTMLTransformation)
+
+    name = 'zopyx.smartprintng.emptyelementremover'
+    description = 'Removes empty elements from document'
+
+    def transform(self, remove_elements=('p', 'div')):
+        soup = self.soup
+        for e in soup.findAll(remove_elements):
+            if not e.contents:
+                e.extract()
+
+
+class LinksToPrinceFootnotes(BaseTransform):
+    implements(IHTMLTransformation)
+
+    name = 'zopyx.smartprintng.linkstoprincefootnotes'
+    description = 'Convert links to external URLs to PrinceXML footnotes'
+
+    def transform(self):
+        soup = self.soup
+        for a in soup.findAll('a'):
+
+            try:
+                href = a['href']
+            except KeyError:
+                continue
+
+            if not re.match(r'^(http|http|ftp)://', href):
+                continue
+
+            contents = a.contents
+            span1 = Tag(soup, 'span')
+            span1['class'] = 'generated-footnote-text'
+            span1.insert(0, NavigableString(contents[0]))
+            span2 = Tag(soup, 'span')
+            span2['class'] = 'generated-footnote'
+            span2.insert(0, NavigableString(href))
+            span1.insert(1, span2)
+            a.replaceWith(span1)
+
+
+
+
 # register factories for all transformations
 import inspect
 for x in locals().values():



More information about the Checkins mailing list