You are here

function htmltidy_run in HTML Tidy 7

Same name and namespace in other branches
  1. 5 htmltidy.module \htmltidy_run()
  2. 6 htmltidy.module \htmltidy_run()

Process the input through tidy engine

Parameters

$input: The raw html/xml

$path: full system path of tidy binary

$args: arguments to run tidy with

$output: output to add to

$errors: errors to add to

$warnings: warnings to ad too

Return value

unknown_type return value of tidy 0 - All input files were processed successfully. 1 - There were warnings. 2 - There were errors.

1 call to htmltidy_run()
htmltidy_string in ./htmltidy.module
Process whatever we are given and return the htmltidy response The output and warnings will be returned as arrays by reference.

File

./htmltidy.module, line 378
The htmltidy module uses Tidy (http://tidy.sf.net) to properly format HTML for saving and display.

Code

function htmltidy_run($input, $tidypath, $args, &$output, &$errors, &$warnings) {
  if (!file_exists($tidypath)) {
    watchdog('htmltidy', 'Failed to find htmltidy executable at %htmltidy_apppath, not using tidy', array(
      '%htmltidy_apppath' => $tidypath,
    ), WATCHDOG_WARNING);
    $output = '';
    return 2;
  }

  // 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",
    ),
  );
  $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);

  // 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;
      break;
    case 1:
      $errors = array();
      foreach (array_filter(split("\n", $stderr)) as $line) {
        $warnings[] = trim($line);
      }
      $output = $stdout;
      break;
    case 2:

      // separate errors and warnings into two different arrays
      foreach (array_filter(split("\n", $stdout)) as $line) {
        $line = trim($line);
        if (preg_match('|^line \\d+ column \\d+ - Warning:|', $line)) {
          $warnings[] = $line;
        }
        else {
          $errors[] = $line;
        }
      }
      $output = $input;
      break;
  }
  return $return_value;
}