--- htmltidy.module 2009-05-23 05:03:49.000000000 +0400 +++ old.htmltidy.module 2007-06-04 18:38:15.000000000 +0400 @@ -31,14 +31,18 @@ /** * Implementation of hook_menu(). */ -function htmltidy_menu() { - $items['admin/settings/htmltidy'] = array( - 'title' => t('HTML Tidy'), - 'description' => t('Configure settings for HTML Tidy.'), - 'page callback' => 'drupal_get_form', - 'page arguments' => array('htmltidy_settings'), - 'access arguments' => array('administer htmltidy') - ); +function htmltidy_menu($may_cache) { + $items = array(); + if ($may_cache) { + $items[] = array( + 'path' => 'admin/settings/htmltidy', + 'title' => t('HTML Tidy'), + 'description' => t('Configure settings for HTML Tidy.'), + 'callback' => 'drupal_get_form', + 'callback arguments' => 'htmltidy_settings', + 'access' => user_access('administer htmltidy') + ); + } return $items; } @@ -155,7 +159,7 @@ function htmltidy_string($input, &$error * documentation for tidy (http://www.w3.org/People/Raggett/tidy/), or weird * stuff starts to happen. */ - if (variable_get('htmltidy_indent', 1)) $args[] = '-i'; + if (variable_get('htmltidy_indent', 1)) $args[] = '--indent auto'; if (!variable_get('htmltidy_verbose', 0)) $args[] = '-q'; if (!variable_get('htmltidy_wrapphp', 1)) $args[] = '--wrap-php no'; if (!variable_get('htmltidy_tidymark', 1)) $args[] = '--tidy-mark no'; @@ -180,6 +184,7 @@ function htmltidy_string($input, &$error $args[] = '--doctype '. variable_get('htmltidy_doctype', 'auto'); $args[] = '-wrap '. variable_get('htmltidy_wordwrap', 0); $args[] = '-utf8'; + $args[] = '-modify'; // modify the input file instead of outputting to stdout. htmltidy_run($input, $args, $output, $errors, $warnings); @@ -546,43 +551,38 @@ function htmltidy_run($input, $args, &$o $output = ''; return 2; } - + + // write input to a file because tidy doesn't take input from stdin. + $dirtyFilename = tempnam(file_directory_temp(), 'drup'); + $f = fopen($dirtyFilename, 'w'); + fwrite($f, $input); + fclose($f); + + // warnings are saved to file + $warningsFilename = tempnam(file_directory_temp(), 'warn'); + $args[] = '-f ' . $warningsFilename; + // Run Tidy with the right options. - $command = $tidypath .' '. implode(' ', $args); - - $descriptorspec = array( - 0 => array("pipe", "r"), // stdin is a pipe that the child will read from - 1 => array("pipe", "w"), // stdout is a pipe that the child will write to - 2 => array("pipe", "w") // stderr is a file to write to - ); - - $process = proc_open($command, $descriptorspec, $pipes); - fwrite($pipes[0], $input); - fclose($pipes[0]); - $stdout = stream_get_contents($pipes[1]); - $stderr = stream_get_contents($pipes[2]); - $return_value = proc_close($process); - + $command = $tidypath .' '. implode(' ', $args) .' '. $dirtyFilename; + system($command, $return_value); + // return_value 0 means success. 1 means warning. 2 means error, the file // will be there, but not have been touched. switch ($return_value) { case 0: $warnings = $errors = array(); - $output = $stdout; + $output = file_get_contents($dirtyFilename); break; case 1: $errors = array(); - foreach(array_filter(split("\n", $stderr)) as $line) { - $warnings[] = trim($line); - } - $output = $stdout; + $warnings = array_map('trim', file($warningsFilename)); + $output = file_get_contents($dirtyFilename); break; case 2: // separate errors and warnings into two different arrays - - foreach(array_filter(split("\n", $stdout)) as $line) { + foreach(file($warningsFilename) as $line) { $line = trim($line); if (preg_match('|^line \d+ column \d+ - Warning:|', $line)) { $warnings[] = $line; @@ -595,5 +595,9 @@ function htmltidy_run($input, $args, &$o break; } + // delete the temporary files. + unlink($dirtyFilename); + unlink($warningsFilename); + return $return_value; }