Author: jvloothuis
Date: Thu May  1 18:00:07 2008
New Revision: 54307

Modified:
   kukit/kss.base/trunk/src/kss/base/commands.py
   kukit/kss.base/trunk/src/kss/base/commands.txt
Log:

Added support for BeautifulSoup, this can be toggled either on or
off. It will automatically run BeautifulSoup on any HTML or XML values
passed to the client if it is turned on (and BeautifulSoup is
available).


Modified: kukit/kss.base/trunk/src/kss/base/commands.py
==============================================================================
--- kukit/kss.base/trunk/src/kss/base/commands.py       (original)
+++ kukit/kss.base/trunk/src/kss/base/commands.py       Thu May  1 18:00:07 2008
@@ -1,7 +1,16 @@
 from xml.sax.saxutils import quoteattr, escape
+
+try:
+    from BeautifulSoup import BeautifulSoup
+    from BeautifulSoup import BeautifulStoneSoup
+except ImportError: # no soup support
+    BeautifulSoup = BeautifulStoneSoup = lambda v: v
+
 from kss.base.registry import command_set_registry
 from kss.base.selectors import Selector
 
+soup_enabled = False
+
 kss_response_header = '''<?xml version="1.0" ?>
 <kukit xmlns="http://www.kukit.org/commands/1.1";><commands>
 '''
@@ -28,10 +37,16 @@
         return "%s('%s')" % (self.__class__.__name__, self.value)
 
 class htmldata(cdatadata):
-    pass
+    def __init__(self, value):
+        if soup_enabled:
+            value = str(BeautifulSoup(value))
+        super(htmldata, self).__init__(value)
 
 class xmldata(cdatadata):
-    pass
+    def __init__(self, value):
+        if soup_enabled:
+            value = str(BeautifulStoneSoup(value))
+        super(xmldata, self).__init__(value)
 
 class KSSCommands(object):
     '''Command renderer for creating KSS responses'''

Modified: kukit/kss.base/trunk/src/kss/base/commands.txt
==============================================================================
--- kukit/kss.base/trunk/src/kss/base/commands.txt      (original)
+++ kukit/kss.base/trunk/src/kss/base/commands.txt      Thu May  1 18:00:07 2008
@@ -225,3 +225,39 @@
 Let us clean up.
 
   >>> command_set_registry.unregister('core')
+
+
+Soup support
+============
+
+If you are outputting faulty HTML you might want to automatically fix
+it up. This can be done with a library called BeautifulSoup. The
+command system has code for integration with BeautifulSoup
+automatically. This means that all calls to replace HTML or other
+commands which manipulate HTML / XML use BeautifulSoup on the data.
+
+By default the system is turned off. To enable it you can set a flag.
+
+  >>> from kss.base import commands
+  >>> commands.soup_enabled = True
+
+The XML and HTML now use BeautifulSoup. To demonstrate this we shall
+load them and show how this works.
+
+  >>> from kss.base.commands import htmldata, xmldata
+
+  >>> htmldata('<p>some text')
+  htmldata('<p>some text</p>')
+
+  >>> xmldata('<para>sdfsdfdsf')
+  xmldata('<para>sdfsdfdsf</para>')
+
+If we disable soup mode it should no longer change our HTML and XML.
+
+  >>> commands.soup_enabled = False
+  >>> htmldata('<p>some text')
+  htmldata('<p>some text')
+
+  >>> xmldata('<para>sdfsdfdsf')
+  xmldata('<para>sdfsdfdsf')
+
_______________________________________________
Kukit-checkins mailing list
[email protected]
http://codespeak.net/mailman/listinfo/kukit-checkins

Reply via email to