<?php
/**
 * Tides HTML code
 *
 * @name My_PHPTAL_Filter_Tidy
 */
 
class My_PHPTAL_Filter_Tidy implements PHPTAL_Filter {

         
/**
          *
          * @name filter
          * @param  $xhtml string
          * @return string
          */
         
public function filter($xhtml) {
              
$tidy = new tidy ();
              
$opts = array ('indent' => true'indent-spaces' => 4'wrap' => 0'output-xhtml' => true'force-output' => true);
              
$tidy->parseString ($xhtml$opts'utf8');
              
$tidy->cleanRepair ();
              
$xhtml = (string) $tidy;
              
$xhtml preg_replace ("@([\s]+)?(<(h[1-6])([^>]+)?>)([\s])+@""\n\\1\\2\n"$xhtml);
              
$xhtml preg_replace ("@(<(h[1-6]|li|title)([^>]+)?>)[\s]+@"'\\1'$xhtml);
              
$xhtml preg_replace ("@[\s]+(</(h[1-6])>)@""\\1\n"$xhtml);
              
$xhtml preg_replace ("@[\s]+(</(li|title)>)@"'\\1'$xhtml);
              
$xhtml preg_replace ("@(</head>)([\s]+)@""\\2\\1\\2"$xhtml);
              
$xhtml preg_replace ("@\n\n@""\n"$xhtml);
              return 
$xhtml;
         } 
// end filter();
    
// end class tidyFilter;
?>