Use PyTidyLib rather than mx.Tidy when available.
authorAdam Sampson <ats@offog.org>
Tue, 22 Dec 2009 15:55:17 +0000 (15:55 +0000)
committerAdam Sampson <ats@offog.org>
Tue, 22 Dec 2009 15:55:17 +0000 (15:55 +0000)
NEWS
PLUGINS
config
rawdoglib/rawdog.py

diff --git a/NEWS b/NEWS
index 96625930960bf2e54e5dfbd1544f20573642e2a2..123cee7ec48449fd539dab264f769c891c978392 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,11 @@ Add the "showtracebacks" option, which causes exceptions that occur
 while a feed is being fetched to be reported with a traceback in the
 resulting error message.
 
+Use PyTidyLib in preference to mx.Tidy when available (suggested by
+Joseph Reagle). If neither is available, "tidyhtml true" just does
+nothing, so it's now turned on in the provided config file. The
+mxtidy_args hook is now called tidy_args.
+
 - rawdog 2.12
 
 Make rawdog work with Python 2.6 (reported by Roy Lanek).
diff --git a/PLUGINS b/PLUGINS
index 694f0b3484665bf02dc2d1e9ab912e0ea6a3751e..033923b0c12725919225350b523531a3b2484f9e 100644 (file)
--- a/PLUGINS
+++ b/PLUGINS
@@ -247,17 +247,17 @@ normal expansion process); you can thus use this hook either for
 manipulating template parameters, or for replacing the template system
 entirely.
 
-### mxtidy_args(config, args, baseurl, inline)
+### tidy_args(config, args, baseurl, inline)
 
-* args: a dictionary of keyword arguments for mx.Tidy.tidy
+* args: a dictionary of keyword arguments for Tidy
 * baseurl: the URL at which the HTML was originally found
 * inline: a boolean indicating whether the output should be inline HTML
   or a block element
 
 When HTML is being sanitised by rawdog and the "tidyhtml" option is
-enabled, this hook will be called just before mx.Tidy.tidy is run. It
-can be used to add or modify mx.Tidy options; for example, to make it
-produce XHTML output.
+enabled, this hook will be called just before Tidy is run (either via
+PyTidyLib or via mx.Tidy). It can be used to add or modify Tidy options;
+for example, to make it produce XHTML output.
 
 ### clean_html(config, html, baseurl, inline)
 
diff --git a/config b/config
index f1110573f4f1c9ad210f6f5c5756bfed4f68be22..bd9fb6bf0fef80897a6412ec2bfd146a288efd19 100644 (file)
--- a/config
+++ b/config
@@ -182,9 +182,9 @@ blocklevelhtml true
 # Whether to attempt to turn feed-provided HTML into valid HTML.
 # The most common problem that this solves is a non-closed element in an
 # article causing formatting problems for the rest of the page.
-# If this option is turned on, you must have the mx.Tidy Python module
+# For this option to have any effect, you need to have PyTidyLib or mx.Tixy
 # installed.
-tidyhtml false
+tidyhtml true
 
 # Whether the articles displayed should be sorted first by the date
 # provided in the feed (useful for "planet" pages, where you're
index 221b365e0970cc0c1fc47e37477229b978755591..7c9c2208925bf202535a61dcfcb0171327a3baa0 100644 (file)
@@ -37,6 +37,16 @@ except:
        hashlib = None
        import sha
 
+try:
+       import tidylib
+except:
+       tidylib = None
+
+try:
+       import mx.Tidy as mxtidy
+except:
+       mxtidy = None
+
 try:
        import feedfinder
 except:
@@ -115,11 +125,22 @@ def sanitise_html(html, baseurl, inline, config):
                        html = "<p>" + html
 
        if config["tidyhtml"]:
-               import mx.Tidy
-               args = { "wrap": 0, "numeric_entities": 1 }
+               args = {"numeric_entities": 1,
+                       "output_html": 1,
+                       "output_xhtml": 0,
+                       "output_xml": 0,
+                       "wrap": 0}
                plugins.call_hook("mxtidy_args", config, args, baseurl, inline)
-               output = mx.Tidy.tidy(html, None, None,
-                                     **args)[2]
+               plugins.call_hook("tidy_args", config, args, baseurl, inline)
+               if tidylib is not None:
+                       # Disable PyTidyLib's somewhat unhelpful defaults.
+                       tidylib.BASE_OPTIONS = {}
+                       output = tidylib.tidy_document(html, args)[0]
+               elif mxtidy is not None:
+                       output = mxtidy.tidy(html, None, None, **args)[2]
+               else:
+                       # No Tidy bindings installed -- do nothing.
+                       output = "<body>" + html + "</body>"
                html = output[output.find("<body>") + 6
                              : output.rfind("</body>")].strip()