[Checkins] SVN: zope3org/trunk/src/zorg/restsupport/ Added support for html fragments and entities to rest2html

Uwe Oestermeier uwe_oestermeier at iwm-kmrc.de
Sat Mar 18 14:57:54 EST 2006


Log message for revision 66066:
  Added support for html fragments and entities to rest2html

Changed:
  U   zope3org/trunk/src/zorg/restsupport/__init__.py
  U   zope3org/trunk/src/zorg/restsupport/html2rest.py

-=-
Modified: zope3org/trunk/src/zorg/restsupport/__init__.py
===================================================================
--- zope3org/trunk/src/zorg/restsupport/__init__.py	2006-03-18 16:56:40 UTC (rev 66065)
+++ zope3org/trunk/src/zorg/restsupport/__init__.py	2006-03-18 19:57:54 UTC (rev 66066)
@@ -40,8 +40,8 @@
     renderer = ReStructuredTextToHTMLRenderer(main, None)
     return renderer.render()
                 
-              
-def html2rest(html, catch_errors=True) :
+
+def html2rest(html, fragment=False, catch_errors=True) :
     """ Converts html to rest. 
     
     >>> html = '<html><body><h1>Header</h1><p>Test</p></body></html>'
@@ -53,15 +53,27 @@
     Test
     <BLANKLINE>
 
-    Fragments can not be processed. In this case the input HTML is returned
+    If an error occurs the input HTML is returned
     unmodified with an error prefix :
     
     >>> html = '<h1>Header</h1><p>Test</p>'
     >>> print html2rest(html)
     Sorry, cannot convert to ReST:
     <h1>Header</h1><p>Test</p>
-
+    
+    >>> print html2rest(html, fragment=True)
+    ======
+    Header
+    ======
+    <BLANKLINE>
+    Test
+    <BLANKLINE>
+    
     """
+    
+    if fragment :
+        html = "<html><body>%s</body></html>" % html
+        
     if catch_errors :
         try :
             parser = Html2ReStructuredTextParser()
@@ -84,28 +96,3 @@
         if not lines :
             raise RuntimeError, "cannot convert fragments"
         return "\n".join(lines)
- 
-# def html2rest2(html) :
-#     """ Converts html to rest. 
-#     
-#     >>> html = '<html><body><h1>Header</h1><p>Test</p></body></html>'
-#     >>> print html2rest2(html)
-#     ======
-#     Header
-#     ======
-#     <BLANKLINE>
-#     Test
-#     <BLANKLINE>
-# 
-#     Fragments can not be processed. In this case the input HTML is returned
-#     unmodified with an error prefix :
-#     
-#     >>> html = '<h1>Header</h1><p>Test</p>'
-#     >>> print html2rest2(html)
-#     Sorry, cannot convert to ReST:
-#     <h1>Header</h1><p>Test</p>
-# 
-#     """
-#     from zorg.restsupport.html2rest2 import html2rest as _html2rest
-#     return _html2rest(html)
-#  

Modified: zope3org/trunk/src/zorg/restsupport/html2rest.py
===================================================================
--- zope3org/trunk/src/zorg/restsupport/html2rest.py	2006-03-18 16:56:40 UTC (rev 66065)
+++ zope3org/trunk/src/zorg/restsupport/html2rest.py	2006-03-18 19:57:54 UTC (rev 66066)
@@ -37,6 +37,7 @@
 import re
 import HTMLParser
 import StringIO
+from htmlentitydefs import  name2codepoint
 
 ### Global
 
@@ -724,8 +725,11 @@
         'table': True, 'tr': True, 'th': True, # uo: allow text in 'td'
     }
 
-    entities = { 'amp': '&', 'lt': '<', 'gt': '>', 'nbsp': ' ' }
-
+    entities = { 'amp': '&', 'lt': '<', 'gt': '>', 'nbsp': ' '}
+    
+    for name, code in name2codepoint.items() :      # uo
+        entities[name] = unichr(code).encode('utf-8')
+        
     charref = {
         8211: '-',
         8217: "'",



More information about the Checkins mailing list