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.  * This file is released under the LGPL
  16.  * "GNU Lesser General Public License"
  17.  * More information can be found here:
  18.  * {@link http://www.gnu.org/copyleft/lesser.html}
  19.  *
  20.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  21.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  22.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  23.  * @link       http://dwoo.org/
  24.  * @version    0.9.1
  25.  * @date       2008-05-30
  26.  * @package    Dwoo
  27.  */
  28. {
  29.     /**
  30.      * tab count to auto-indent the source
  31.      *
  32.      * @var int 
  33.      */
  34.     protected static $tabCount = -1;
  35.  
  36.     /**
  37.      * stores the additional data (following a tag) of the last call to open/close/singleTag
  38.      *
  39.      * @var string 
  40.      */
  41.     protected static $lastCallAdd '';
  42.  
  43.     /**
  44.      * formats the input using the singleTag/closeTag/openTag functions
  45.      *
  46.      * It is auto indenting the whole code, excluding <textarea>, <code> and <pre> tags that must be kept intact.
  47.      * Those tags must however contain only htmlentities-escaped text for everything to work properly.
  48.      * Inline tags are presented on a single line with their content
  49.      *
  50.      * @param Dwoo $dwoo the dwoo instance rendering this
  51.      * @param string $input the xhtml to format
  52.      * @return string formatted xhtml
  53.      */
  54.     public function process($input)
  55.     {
  56.         self::$tabCount = -1;
  57.  
  58.         // auto indent all but textareas & pre (or we have weird tabs inside)
  59.         $input preg_replace_callback("#(<[^>]+>)(\s*)([^<]*)#"array('self''tagDispatcher')$input);
  60.  
  61.         return $input;
  62.     }
  63.  
  64.     /**
  65.      * helper function for format()'s preg_replace call
  66.      *
  67.      * @param array    $input    array of matches (1=>tag, 2=>whitespace(optional), 3=>additional non-html content)
  68.      * @return string the indented tag
  69.      */
  70.     protected static function tagDispatcher($input)
  71.     {
  72.         // 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
  73.         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["{
  74.             return $input[1$input[3];
  75.         }
  76.         // 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
  77.         if (substr($input[1],0,10== "</textarea" || substr($input[1],0,5== "</pre" || substr($input[1],0,6== "</code" || substr($input[1],-2== "/>"{
  78.             return self::singleTag($input[1],$input[3],$input[2]);
  79.         }
  80.         // it's the closing tag
  81.         if ($input[0][1]=="/"){
  82.             return self::closeTag($input[1],$input[3],$input[2]);
  83.         }
  84.         // opening tag
  85.         return self::openTag($input[1],$input[3],$input[2]);
  86.     }
  87.  
  88.     /**
  89.      * returns an open tag and adds a tab into the auto indenting
  90.      *
  91.      * @param string $tag content of the tag
  92.      * @param string $add additional data (anything before the following tag)
  93.      * @param string $whitespace white space between the tag and the additional data
  94.      * @return string 
  95.      */
  96.     protected static function openTag($tag,$add,$whitespace)
  97.     {
  98.         $tabs str_pad('',self::$tabCount++,"\t");
  99.  
  100.         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)) {
  101.             // if it's one of those tag it's inline so it does not require a leading line break
  102.             $result $tag $whitespace str_replace("\n","\n".$tabs,$add);
  103.         elseif (substr($tag,0,9== '<!DOCTYPE'{
  104.             // it's the doctype declaration so no line break here either
  105.             $result $tabs $tag;
  106.         else {
  107.             // normal block tag
  108.             $result "\n".$tabs $tag;
  109.  
  110.             if (!empty($add)) {
  111.                 $result .= "\n".$tabs."\t".str_replace("\n","\n\t".$tabs,$add);
  112.             }
  113.         }
  114.  
  115.         self::$lastCallAdd $add;
  116.  
  117.         return $result;
  118.     }
  119.  
  120.     /**
  121.      * returns a closing tag and removes a tab from the auto indenting
  122.      *
  123.      * @param string $tag content of the tag
  124.      * @param string $add additional data (anything before the following tag)
  125.      * @param string $whitespace white space between the tag and the additional data
  126.      * @return string 
  127.      */
  128.     protected static function closeTag($tag,$add,$whitespace)
  129.     {
  130.         $tabs str_pad('',--self::$tabCount,"\t");
  131.  
  132.         // if it's one of those tag it's inline so it does not require a leading line break
  133.         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)) {
  134.             $result $tag $whitespace str_replace("\n","\n".$tabs,$add);
  135.         else {
  136.             $result "\n".$tabs.$tag;
  137.  
  138.             if (!empty($add)) {
  139.                 $result .= "\n".$tabs."\t".str_replace("\n","\n\t".$tabs,$add);
  140.             }
  141.         }
  142.  
  143.         self::$lastCallAdd $add;
  144.  
  145.         return $result;
  146.     }
  147.  
  148.     /**
  149.      * returns a single tag with auto indenting
  150.      *
  151.      * @param string $tag content of the tag
  152.      * @param string $add additional data (anything before the following tag)
  153.      * @return string 
  154.      */
  155.     protected static function singleTag($tag,$add,$whitespace)
  156.     {
  157.         $tabs str_pad('',self::$tabCount,"\t");
  158.  
  159.         // if it's img, br it's inline so it does not require a leading line break
  160.         // 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
  161.         if (preg_match('#^<(img|br|/textarea|/pre|/code)(?: [^>]*|)>#'$tag)) {
  162.             $result $tag.$whitespace;
  163.  
  164.             if (!empty($add)) {
  165.                 $result .= str_replace("\n","\n".$tabs,$add);
  166.             }
  167.         else {
  168.             $result "\n".$tabs.$tag;
  169.  
  170.             if (!empty($add)) {
  171.                 $result .= "\n".$tabs.str_replace("\n","\n".$tabs,$add);
  172.             }
  173.         }
  174.  
  175.         self::$lastCallAdd $add;
  176.  
  177.         return $result;
  178.     }
  179. }

Documentation generated on Sat, 28 Jun 2008 01:38:26 +0200 by phpDocumentor 1.4.0