Source for file html_format.php

Documentation is available at html_format.php

  1. <?php
  2.  
  3. /**
  4.  * Formats any html output (must be valid xml where every tag opened is closed)
  5.  * using a single tab for indenting. 'pre' and other whitespace sensitive
  6.  * tags should not be affected.
  7.  *
  8.  * It is not recommended to use this on every template if you render multiple
  9.  * templates per page, you should only use it once on the main page template so that
  10.  * everything is formatted in one pass.
  11.  *
  12.  * This software is provided 'as-is', without any express or implied warranty.
  13.  * In no event will the authors be held liable for any damages arising from the use of this software.
  14.  *
  15.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  16.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  17.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  18.  * @link       http://dwoo.org/
  19.  * @version    1.0.0
  20.  * @date       2008-10-23
  21.  * @package    Dwoo
  22.  */
  23. {
  24.     /**
  25.      * tab count to auto-indent the source
  26.      *
  27.      * @var int 
  28.      */
  29.     protected static $tabCount = -1;
  30.  
  31.     /**
  32.      * stores the additional data (following a tag) of the last call to open/close/singleTag
  33.      *
  34.      * @var string 
  35.      */
  36.     protected static $lastCallAdd '';
  37.  
  38.     /**
  39.      * formats the input using the singleTag/closeTag/openTag functions
  40.      *
  41.      * It is auto indenting the whole code, excluding <textarea>, <code> and <pre> tags that must be kept intact.
  42.      * Those tags must however contain only htmlentities-escaped text for everything to work properly.
  43.      * Inline tags are presented on a single line with their content
  44.      *
  45.      * @param Dwoo $dwoo the dwoo instance rendering this
  46.      * @param string $input the xhtml to format
  47.      * @return string formatted xhtml
  48.      */
  49.     public function process($input)
  50.     {
  51.         self::$tabCount = -1;
  52.  
  53.         // auto indent all but textareas & pre (or we have weird tabs inside)
  54.         $input preg_replace_callback("#(<[^>]+>)(\s*)([^<]*)#"array('self''tagDispatcher')$input);
  55.  
  56.         return $input;
  57.     }
  58.  
  59.     /**
  60.      * helper function for format()'s preg_replace call
  61.      *
  62.      * @param array    $input    array of matches (1=>tag, 2=>whitespace(optional), 3=>additional non-html content)
  63.      * @return string the indented tag
  64.      */
  65.     protected static function tagDispatcher($input)
  66.     {
  67.         // textarea, pre, code tags and comments are to be left alone to avoid any non-wanted whitespace inside them so it just outputs them as they were
  68.         if (substr($input[1],0,9== "<textarea" || substr($input[1],0,4== "<pre" || substr($input[1],0,5== "<code" || substr($input[1],0,4== "<!--" || substr($input[1],0,9== "<![CDATA["{
  69.             return $input[1$input[3];
  70.         }
  71.         // closing textarea, code and pre tags and self-closed tags (i.e. <br />) are printed as singleTags because we didn't use openTag for the formers and the latter is a single tag
  72.         if (substr($input[1],0,10== "</textarea" || substr($input[1],0,5== "</pre" || substr($input[1],0,6== "</code" || substr($input[1],-2== "/>"{
  73.             return self::singleTag($input[1],$input[3],$input[2]);
  74.         }
  75.         // it's the closing tag
  76.         if ($input[0][1]=="/"){
  77.             return self::closeTag($input[1],$input[3],$input[2]);
  78.         }
  79.         // opening tag
  80.         return self::openTag($input[1],$input[3],$input[2]);
  81.     }
  82.  
  83.     /**
  84.      * returns an open tag and adds a tab into the auto indenting
  85.      *
  86.      * @param string $tag content of the tag
  87.      * @param string $add additional data (anything before the following tag)
  88.      * @param string $whitespace white space between the tag and the additional data
  89.      * @return string 
  90.      */
  91.     protected static function openTag($tag,$add,$whitespace)
  92.     {
  93.         $tabs str_pad('',self::$tabCount++,"\t");
  94.  
  95.         if (preg_match('#^<(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)(?: [^>]*|)>#'$tag)) {
  96.             // if it's one of those tag it's inline so it does not require a leading line break
  97.             $result $tag $whitespace str_replace("\n","\n".$tabs,$add);
  98.         elseif (substr($tag,0,9== '<!DOCTYPE'{
  99.             // it's the doctype declaration so no line break here either
  100.             $result $tabs $tag;
  101.         else {
  102.             // normal block tag
  103.             $result "\n".$tabs $tag;
  104.  
  105.             if (!empty($add)) {
  106.                 $result .= "\n".$tabs."\t".str_replace("\n","\n\t".$tabs,$add);
  107.             }
  108.         }
  109.  
  110.         self::$lastCallAdd $add;
  111.  
  112.         return $result;
  113.     }
  114.  
  115.     /**
  116.      * returns a closing tag and removes a tab from the auto indenting
  117.      *
  118.      * @param string $tag content of the tag
  119.      * @param string $add additional data (anything before the following tag)
  120.      * @param string $whitespace white space between the tag and the additional data
  121.      * @return string 
  122.      */
  123.     protected static function closeTag($tag,$add,$whitespace)
  124.     {
  125.         $tabs str_pad('',--self::$tabCount,"\t");
  126.  
  127.         // if it's one of those tag it's inline so it does not require a leading line break
  128.         if (preg_match('#^</(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)>#'$tag)) {
  129.             $result $tag $whitespace str_replace("\n","\n".$tabs,$add);
  130.         else {
  131.             $result "\n".$tabs.$tag;
  132.  
  133.             if (!empty($add)) {
  134.                 $result .= "\n".$tabs."\t".str_replace("\n","\n\t".$tabs,$add);
  135.             }
  136.         }
  137.  
  138.         self::$lastCallAdd $add;
  139.  
  140.         return $result;
  141.     }
  142.  
  143.     /**
  144.      * returns a single tag with auto indenting
  145.      *
  146.      * @param string $tag content of the tag
  147.      * @param string $add additional data (anything before the following tag)
  148.      * @return string 
  149.      */
  150.     protected static function singleTag($tag,$add,$whitespace)
  151.     {
  152.         $tabs str_pad('',self::$tabCount,"\t");
  153.  
  154.         // if it's img, br it's inline so it does not require a leading line break
  155.         // if it's a closing textarea, code or pre tag, it does not require a leading line break either or it creates whitespace at the end of those blocks
  156.         if (preg_match('#^<(img|br|/textarea|/pre|/code)(?: [^>]*|)>#'$tag)) {
  157.             $result $tag.$whitespace;
  158.  
  159.             if (!empty($add)) {
  160.                 $result .= str_replace("\n","\n".$tabs,$add);
  161.             }
  162.         else {
  163.             $result "\n".$tabs.$tag;
  164.  
  165.             if (!empty($add)) {
  166.                 $result .= "\n".$tabs.str_replace("\n","\n".$tabs,$add);
  167.             }
  168.         }
  169.  
  170.         self::$lastCallAdd $add;
  171.  
  172.         return $result;
  173.     }
  174. }

Documentation generated on Sat, 18 Jul 2009 21:05:06 +0200 by phpDocumentor 1.4.0