From e06929c2183706d4f1a095d98a89ce1cb1fb40b9 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Mon, 28 May 2007 03:33:12 +0000 Subject: [PATCH] Further refactoring to remove hacks. Move everything into the ConfigDoc facade object. Add parameters to plain.xsl. Optionally singleton-ize HTML Purifier. Add loadArrayFromForm to Config object. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1105 48356398-32a2-884e-a903-53898d9a118a --- configdoc/generate.php | 51 +++------------------- configdoc/library/ConfigDoc.php | 28 +++++++++++- configdoc/library/ConfigDoc/HTMLXSLTProcessor.php | 6 +++ configdoc/library/ConfigDoc/XMLSerializer.php | 3 +- .../ConfigDoc/XMLSerializer/ConfigSchema.php | 4 +- configdoc/styles/plain.xsl | 8 ++-- library/HTMLPurifier.php | 17 ++++++++ library/HTMLPurifier/Config.php | 17 ++++++++ smoketests/configForm.php | 47 +++++++++++++------- 9 files changed, 113 insertions(+), 68 deletions(-) diff --git a/configdoc/generate.php b/configdoc/generate.php index 7e73663a..2fe95cc4 100644 --- a/configdoc/generate.php +++ b/configdoc/generate.php @@ -7,12 +7,11 @@ /* TODO: -- make XML format richer (see below) +- make XML format richer (see XMLSerializer_ConfigSchema) - extend XSLT transformation (see the corresponding XSLT file) - allow generation of packaged docs that can be easily moved - multipage documentation - determine how to multilingualize -- factor out code into classes */ // there are several hacks for the configForm.php smoketest @@ -37,53 +36,17 @@ require_once 'library/ConfigDoc.auto.php'; // --------------------------------------------------------------------------- // Load copies of HTMLPurifier_ConfigDef and HTMLPurifier -// hack -if (defined('HTMLPURIFIER_CUSTOM_SCHEMA')) { - // included from somewhere else - $var = HTMLPURIFIER_CUSTOM_SCHEMA; - $schema = $$var; - chdir(dirname(__FILE__)); -} else { - $schema = HTMLPurifier_ConfigSchema::instance(); -} -$purifier = new HTMLPurifier(); - - -// --------------------------------------------------------------------------- -// Generate types.xml, a document describing the constraint "type" - -$types_serializer = new ConfigDoc_XMLSerializer_Types(); -$types_document = $types_serializer->serialize($schema); -$types_document->save('types.xml'); - - -// --------------------------------------------------------------------------- -// Generate configdoc.xml, a document documenting configuration directives - -$schema_serializer = new ConfigDoc_XMLSerializer_ConfigSchema(); -$dom_document = $schema_serializer->serialize($schema); -$dom_document->save('configdoc.xml'); - +$schema = HTMLPurifier_ConfigSchema::instance(); // --------------------------------------------------------------------------- // Generate final output using XSLT // determine stylesheet name -$xsl_stylesheet_name = 'plain'; -$xsl_stylesheet = "styles/$xsl_stylesheet_name.xsl"; - -// transform -$xslt_processor = new ConfigDoc_HTMLXSLTProcessor(); -$xslt_processor->importStylesheet($xsl_stylesheet); -$html_output = $xslt_processor->transformToHTML($dom_document); - -// hack -if (!defined('HTMLPURIFIER_CUSTOM_SCHEMA')) { - // write it to a file (todo: parse into seperate pages) - file_put_contents("$xsl_stylesheet_name.html", $html_output); -} elseif (defined('HTMLPURIFIER_SCRIPT_LOCATION')) { - $html_output = str_replace('styles/plain.css', HTMLPURIFIER_SCRIPT_LOCATION . 'styles/plain.css', $html_output); -} +$xsl_stylesheet_name = 'plain'; // use $_GET in the future +$configdoc = new ConfigDoc(); +$html_output = $configdoc->generate($schema, $xsl_stylesheet_name); + +file_put_contents("$xsl_stylesheet_name.html", $html_output); // --------------------------------------------------------------------------- // Output for instant feedback diff --git a/configdoc/library/ConfigDoc.php b/configdoc/library/ConfigDoc.php index 87b813a6..7332ba5a 100644 --- a/configdoc/library/ConfigDoc.php +++ b/configdoc/library/ConfigDoc.php @@ -7,7 +7,33 @@ require_once 'ConfigDoc/XMLSerializer/ConfigSchema.php'; class ConfigDoc { - // useless... + function generate($schema, $xsl_stylesheet_name = 'plain', $parameters = array()) { + // generate types document, describing type constraints + $types_serializer = new ConfigDoc_XMLSerializer_Types(); + $types_document = $types_serializer->serialize($schema); + $types_document->save('types.xml'); + + // generate configdoc.xml, documents configuration directives + $schema_serializer = new ConfigDoc_XMLSerializer_ConfigSchema(); + $schema_document = $schema_serializer->serialize($schema); + $schema_document->save('configdoc.xml'); + + // setup transformation + $xsl_stylesheet = dirname(__FILE__) . "/../styles/$xsl_stylesheet_name.xsl"; + $xslt_processor = new ConfigDoc_HTMLXSLTProcessor(); + $xslt_processor->setParameters($parameters); + $xslt_processor->importStylesheet($xsl_stylesheet); + + return $xslt_processor->transformToHTML($schema_document); + } + + /** + * Remove any generated files + */ + function cleanup() { + unlink('types.xml'); + unlink('configdoc.xml'); + } } diff --git a/configdoc/library/ConfigDoc/HTMLXSLTProcessor.php b/configdoc/library/ConfigDoc/HTMLXSLTProcessor.php index e86e8450..64966c5b 100644 --- a/configdoc/library/ConfigDoc/HTMLXSLTProcessor.php +++ b/configdoc/library/ConfigDoc/HTMLXSLTProcessor.php @@ -51,6 +51,12 @@ class ConfigDoc_HTMLXSLTProcessor return $out; } + public function setParameters($options) { + foreach ($options as $name => $value) { + $this->xsltProcessor->setParameter('', $name, $value); + } + } + } ?> \ No newline at end of file diff --git a/configdoc/library/ConfigDoc/XMLSerializer.php b/configdoc/library/ConfigDoc/XMLSerializer.php index fbcd102b..cbab5d3b 100644 --- a/configdoc/library/ConfigDoc/XMLSerializer.php +++ b/configdoc/library/ConfigDoc/XMLSerializer.php @@ -9,8 +9,7 @@ class ConfigDoc_XMLSerializer { protected function appendHTMLDiv($document, $node, $html) { - // oh no, it's a global! - global $purifier; + $purifier = HTMLPurifier::getInstance(); $html = $purifier->purify($html); $dom_html = $document->createDocumentFragment(); $dom_html->appendXML($html); diff --git a/configdoc/library/ConfigDoc/XMLSerializer/ConfigSchema.php b/configdoc/library/ConfigDoc/XMLSerializer/ConfigSchema.php index f55a4876..57675a2a 100644 --- a/configdoc/library/ConfigDoc/XMLSerializer/ConfigSchema.php +++ b/configdoc/library/ConfigDoc/XMLSerializer/ConfigSchema.php @@ -95,8 +95,8 @@ class ConfigDoc_XMLSerializer_ConfigSchema extends ConfigDoc_XMLSerializer foreach ($info->descriptions as $file => $file_descriptions) { foreach ($file_descriptions as $line => $description) { $dom_description = $dom_document->createElement('description'); - // hack - if (!defined('HTMLPURIFIER_CUSTOM_SCHEMA')) { + // refuse to write $file if it's a full path + if (str_replace('\\', '/', realpath($file)) != $file) { $dom_description->setAttribute('file', $file); $dom_description->setAttribute('line', $line); } diff --git a/configdoc/styles/plain.xsl b/configdoc/styles/plain.xsl index eb118778..3952bf0d 100644 --- a/configdoc/styles/plain.xsl +++ b/configdoc/styles/plain.xsl @@ -12,19 +12,21 @@ indent = "no" media-type = "text/html" /> + + - Configuration Documentation - <xsl:value-of select="/configdoc/title" /> + <xsl:value-of select="$title" /> - <xsl:value-of select="/configdoc/title" /> - +
-

Configuration Documentation

+

Table of Contents

    diff --git a/library/HTMLPurifier.php b/library/HTMLPurifier.php index dfd252f1..a25414d0 100644 --- a/library/HTMLPurifier.php +++ b/library/HTMLPurifier.php @@ -164,6 +164,23 @@ class HTMLPurifier return $array_of_html; } + /** + * Singleton for enforcing just one HTML Purifier in your system + */ + function &getInstance($prototype = null) { + static $htmlpurifier; + if (!$htmlpurifier || $prototype) { + if (is_a($prototype, 'HTMLPurifier')) { + $htmlpurifier = $prototype; + } elseif ($prototype) { + $htmlpurifier = new HTMLPurifier(HTMLPurifier_Config::create($prototype)); + } else { + $htmlpurifier = new HTMLPurifier(); + } + } + return $htmlpurifier; + } + } diff --git a/library/HTMLPurifier/Config.php b/library/HTMLPurifier/Config.php index 033f069e..f7337369 100644 --- a/library/HTMLPurifier/Config.php +++ b/library/HTMLPurifier/Config.php @@ -298,6 +298,23 @@ class HTMLPurifier_Config } /** + * Loads configuration values from $_GET/$_POST that were posted + * via ConfigForm + * @static + */ + function loadArrayFromForm($array) { + $mq = get_magic_quotes_gpc(); + foreach ($array as $key => $value) { + if (!strncmp($key, 'Null_', 5) && !empty($value)) { + unset($array[substr($key, 5)]); + unset($array[$key]); + } + if ($mq) $array[$key] = stripslashes($value); + } + return @HTMLPurifier_Config::create($array); + } + + /** * Loads configuration values from an ini file * @param $filename Name of ini file */ diff --git a/smoketests/configForm.php b/smoketests/configForm.php index ca32aabd..d488e5cc 100644 --- a/smoketests/configForm.php +++ b/smoketests/configForm.php @@ -3,12 +3,37 @@ require_once 'common.php'; if (isset($_GET['doc'])) { + + if ( + file_exists('testSchema.html') && + filemtime('testSchema.php') < filemtime('testSchema.html') && + !isset($_GET['purge']) + ) { + echo file_get_contents('testSchema.html'); + exit; + } + + if (version_compare('5', PHP_VERSION, '>')) exit('Requires PHP 5 or higher.'); + + // setup schema for parsing require_once 'testSchema.php'; - $new_schema = $custom_schema; - HTMLPurifier_ConfigSchema::instance($old); - define('HTMLPURIFIER_CUSTOM_SCHEMA', 'new_schema'); - define('HTMLPURIFIER_SCRIPT_LOCATION', '../configdoc/'); - require_once '../configdoc/generate.php'; + $new_schema = $custom_schema; // dereference the reference + HTMLPurifier_ConfigSchema::instance($old); // restore old version + + // setup ConfigDoc environment + require_once '../configdoc/library/ConfigDoc.auto.php'; + + // perform the ConfigDoc generation + $configdoc = new ConfigDoc(); + $html = $configdoc->generate($new_schema, 'plain', array( + 'css' => '../configdoc/styles/plain.css', + 'title' => 'Sample Configuration Documentation' + )); + $configdoc->cleanup(); + + file_put_contents('testSchema.html', $html); + echo $html; + exit; } @@ -37,17 +62,7 @@ require_once 'HTMLPurifier/Printer/ConfigForm.php'; require_once 'testSchema.php'; // cleanup ( this should be rolled into Config ) -$get = isset($_GET) ? $_GET : array(); -$mq = get_magic_quotes_gpc(); -foreach ($_GET as $key => $value) { - if (!strncmp($key, 'Null_', 5) && !empty($value)) { - unset($get[substr($key, 5)]); - unset($get[$key]); - } - if ($mq) $get[$key] = stripslashes($value); -} -$config = @HTMLPurifier_Config::create($get); - +$config = HTMLPurifier_Config::loadArrayFromForm($_GET); $printer = new HTMLPurifier_Printer_ConfigForm('?doc'); echo $printer->render($config); -- 2.11.4.GIT