<?php
/*
TODO: normal javascript wrap, support for divs?
11 november 2006 - patch in remove spaces and wrap tags, no more apache crush
12.05.2005 - javascript fix
11.05.2005 - changes in error and separators managment
15.03.2005 - patch for case-insensitive tag search
Copyright(c) 2005 New Media Guru Ltd
email:hide@address.com
url:www.newmediaguru.com
small tidy analog, html beautifer, ver 1.3
*/
// ************************************************************
class sTidy
{
var $space_word =" ";
var $space_counter =0;//how much space from left in html tree
var $errors =array();//calculates number of errors
var $inside_table =0;//private
var $inside_script =0;//private
// ************************************************************
function check_tag_quotes($html){
$re="/<[^\W](.*?){4}[^?>]>/";
return preg_replace_callback($re,array(&$this,'check_tag_quotes_callback'),$html);
}
// ************************************************************
function check_tag_quotes_callback($matches){
$dquota=substr_count($matches[0],'"');
$squota=substr_count($matches[0],"'");
if($dquota){
if($dquota%2){
echo 'sTidy::DQuota count error found<br>';
$matches[0]="\n\n\n<!--DQuota error start-->".$matches[0]."<!--DQuota error end-->\n\n\n";
}
}
if($squota){
if($squota%2){
echo 'sTidy::SQuota count error found<br>';
$matches[0]="\n\n\n<!--SQuota error start-->".$matches[0]."<!--SQuota error end-->\n\n\n";
}
}
return $matches[0];
}
// ************************************************************
function wrap_tags($html){
$re="/([\n]*)(.*)*/i";
return preg_replace_callback($re,array(&$this,'wrap_tags_callback'),$html);
}
// ************************************************************
function wrap_tags_callback($matches){
$re="/([\n]*)<([\/]?)([td]|[tr]|[table]|[tbody]|[textarea]|[script])+([>]|[\s])/i";
$re2="/([\n]*)<([\/]?)([script])+([>]|[\s])/i";
$ecounter=0;//extra counter
$remove_space=0;//remove space for some tags
if($this->inside_script)
{
$found = array ();
if(preg_match($re2, $matches[0], $found)){
switch(strtolower(trim($found[0]))){
case '</script>';
$this->inside_script--;
break;
}
return "\n".trim($matches[0]);
}
return $matches[0];
}
$found = array ();
if(preg_match($re, $matches[0], $found))
{
switch(strtolower(trim($found[0])))
{
case '<script';
case '<script>';
$this->inside_script++;
break;
case '</script>';
$this->inside_script--;
break;
case '<table':
case '<table>':
$this->space_counter++;
$this->inside_table++;
break;
case '</table>':
$this->space_counter--;
$this->inside_table--;
$ecounter=1;
break;
case '<tbody':
case '<tbody>':
$this->space_counter++;
break;
case '</tbody>';
$this->space_counter--;
$ecounter=1;
break;
case '<tr':
case '<tr>':
$this->space_counter++;
break;
case '</tr>':
$this->space_counter--;
$ecounter=1;
break;
case '<td':
case '<td>':
$this->space_counter++;
break;
case '</td>':
$this->space_counter--;
$ecounter=1;
$remove_space=1;//remove unusefull space//IE img patch!!
break;
case '<textarea':
case '<textarea>':
$ecounter=1;
break;
case '</textarea>':
$ecounter=1;
$remove_space=1;//remove unusefull space
break;
default:
break;
}
}else{
($this->inside_table)?$ecounter=1:$ecounter=0;
if(substr(trim($matches[0]),0,4)=="<!--")$ecounter=-$this->space_counter;
}
if($this->space_counter<0)
{
$this->errors[] ='sTidy::Template structure error found<br>';
$this->space_counter=0;
$matches[0] ="\n\n<!--Structure error start-->\n". $this->get_separator($this->space_counter+$ecounter). trim($matches[0]). "\n<!--Structure error end-->\n\n";
}elseif($remove_space==1){
$matches[0]=trim($matches[0]);
}else{
$matches[0]="\n".$this->get_separator($this->space_counter+$ecounter).trim($matches[0]);
}
return $matches[0];
}
// ************************************************************
function explore_tags($html){
$re="/<([\/]?)([td]|[tr]|[table]|[tbody]|[script])[^>]*>/i"; //
return preg_replace_callback($re,array(&$this,'explore_tags_callback'),$html);
}
// ************************************************************
function explore_tags_callback($matches){
return "\n".$matches[0];
}
// ************************************************************
function remove_spaces($html){
$re="/([\n]*)([\s]*)([\t]*)(.*)*/i";
return preg_replace_callback($re,array(&$this,'remove_spaces_callback'),$html);
}
// ************************************************************
function remove_spaces_callback($matches){
return "\n".trim($matches[0]);
}
// ************************************************************
function parse($html=''){
//remove all unused spaces
$html=$this->remove_spaces($html);
//explore some tags
$html=$this->explore_tags($html);
//create new structure
$html=$this->wrap_tags($html);
//check tags for quotes
//$html=$this->check_tag_quotes($html);
$this->show_errors();
return $html;
}
// ************************************************************
function show_errors(){
if($this->space_counter){
echo 'sTidy::Wrong template structure,not found(close tag)<br>';
}elseif($this->space_counter<0){
echo 'sTidy::Wrong template structure,not found(start tag)<br>';
}
}
// ************************************************************
function get_separator($number){
return str_repeat($this->space_word, $number);
}
}
// ************************************************************
?>