You are here

function htmltidy_string in HTML Tidy 6

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

Process whatever we are given and return the htmltidy response The output and warnings will be returned as arrays by reference.

Parameters

$input: html string to be tidied

$errors: an array to be filled with error info

$warnings: an array to be filled with warning info

Return value

the tidied string

1 call to htmltidy_string()
htmltidy_fragment in ./htmltidy.module
Tidies an incomplete fragment of HTML by passing it through htmltidy full, then stripping back down to the 'body'.

File

./htmltidy.module, line 89
The theme system, which controls the output of Drupal. The htmltidy module uses Tidy (http://tidy.sf.net) to properly format HTML for saving and display.

Code

function htmltidy_string($input, $format, &$errors, &$warnings) {
  $filter_settings = variable_get("htmltidy_filter_{$format}", array());
  $filter_settings += htmltidy_default_settings();
  if (!file_exists($filter_settings['paths']['app'])) {
    $message = "Failed to find htmltidy executable at '%htmltidy_apppath', not using tidy.";
    $strings = array(
      '%htmltidy_apppath' => $filter_settings['paths']['app'],
    );
    watchdog('htmltidy', $message, $strings, WATCHDOG_WARNING);
    $errors[] = t($message, $strings);
    return '';
  }

  /*
   * Do not pass the parameters their default values as defined in the
   * documentation for tidy (http://www.w3.org/People/Raggett/tidy/), or weird
   * stuff starts to happen.
   */
  if ($filter_settings['format']['indent']) {
    $args[] = '--indent auto';
  }
  if (!$filter_settings['debug']['verbose']) {
    $args[] = '-q';
  }
  if (!$filter_settings['format']['wrapphp']) {
    $args[] = '--wrap-php no';
  }
  if (!$filter_settings['format']['tidymark']) {
    $args[] = '--tidy-mark no';
  }
  if ($filter_settings['format']['clean']) {
    $args[] = '--clean yes';
  }
  if ($filter_settings['format']['xhtml']) {
    $args[] = '--output-xhtml yes';
  }
  if ($filter_settings['format']['enclosetext']) {
    $args[] = '--enclose-text yes';
  }
  if ($filter_settings['format']['encloseblocktext']) {
    $args[] = '--enclose-block-text yes';
  }
  if ($filter_settings['format']['wordcleanup']) {
    $args[] = '--bare yes';
    $args[] = '--word-2000 yes';
    $args[] = '--drop-proprietary-attributes yes';
  }
  if (htmltidy_empty($filter_settings['format']['process_input'], FALSE) && !module_exists('htmltidy_output')) {
    $args[] = '--show-body-only yes';
  }

  // user specified configuration file
  $htmltidy_confpath = $filter_settings['paths']['config'];
  if (!empty($htmltidy_confpath) && file_exists($htmltidy_confpath)) {
    $args[] = '-config ' . $htmltidy_confpath;
  }
  if (!empty($filter_settings['format']['doctype'])) {
    $args[] = '--doctype ' . $filter_settings['format']['doctype'];
  }
  $args[] = '-wrap ' . intval($filter_settings['format']['wordwrap']);
  $args[] = '-utf8';
  $args[] = '-modify';

  // modify the input file instead of outputting to stdout.
  $output = '';
  htmltidy_run($input, $filter_settings['paths']['app'], $args, $output, $errors, $warnings);

  // Output debugging info.
  if ($filter_settings['debug']['warnings'] && user_access('use htmltidy debug mode')) {
    $header = "<style type=\"text/css\"> .htmltidy { border: 1px dashed #aaa; background-color: #eee; padding: 1em;\n" . "margin: 1em; float: left; font-family: \"courier new\", sans-serif; font-size: 8pt; color: #050; } </style>";
    drupal_set_html_head($header);
    if (isset($warnings_filename)) {
      $warnings = file_get_contents($warnings_filename);
      drupal_set_message("<h3>HTMLTidy Debug</h3><kbd>{$apppath} {$cline} -wrap {$wordwrap} -utf8 -f {$warnings_filename} {$dirty_filename}</kbd>");
    }
  }
  return $output;
}