Saturday, September 17, 2005

How to Make Drupal 4.6.3 render XML, MathML, with the Chameleon Theme

Update: If you are really interested in MathML, I recommend using XML all the way through authoring and publishing. See MMADE's samples page for some results of this route.

In chameleon.theme, change:

$output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"; $output .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"$language\" xml:lang=\"$language\">\n";

to:

if((stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml"))||(stristr($_SERVER["HTTP_USER_AGENT"],"Validator"))) { $output = "<?xml version=\"1.0\"?>\n"; $output .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN\" \"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd\">\n"; $output .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$language\">\n"; } else { $output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"; $output .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"$language\" xml:lang=\"$language\">\n"; }

In common.inc, change:

$output = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";

to:

if((stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml"))||(stristr($_SERVER["HTTP_USER_AGENT"],"Validator"))) { $output = "<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=utf-8\" />\n"; } else { $output = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"; }

and:

drupal_set_header('Content-Type: text/html; charset=utf-8');

to:

if((stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml"))||(stristr($_SERVER["HTTP_USER_AGENT"],"Validator"))) { drupal_set_header('Content-Type: application/xhtml+xml; charset=utf-8'); } else { drupal_set_header('Content-Type: text/html; charset=utf-8'); }

Now, load IE, since it does not emit application/xhtml+xml in HTTP_ACCEPT. Clear all of your cached data, so that IE does not use the old data for your site. Load "http://yoursite.com/cron.php" in IE. Load about:blank in IE. Clear all of your cached data again. Shut down IE. Reload IE. Reload your site in IE. Log in. Change your site theme to chameleon (or away from it and back again). Log out. Clear the other browser's cache. You're now done.

Several caveats exist with this solution: Drupal frequently renders non-well-formed pages. These could include the administrative logs screen, any story in which a tease "cuts" a tag, or just any node that contains non-well-formed tags. Until Drupal comes up with a way to fix them, automatic teasers can be eliminated by setting the tease length to unlimited, with subsequent regeneration of the problem teasers, or by judicious use of <!--break-->. All node items must be validated by someone working in an application/xhtml+xml browser, etc.

Credit for the idea to also serve XML pages to validators goes to Brian Puccio for his post on Drupal and XML. Thanks also go to eldarin for helping me sort out the headers.

Thursday, September 1, 2005

For those who want to know what Kettering's beautiful side looks like

For those who want to know what Kettering's beautiful side looks like, please see Joel's post "The Landscape of Kettering".