de

designoir

IndentHTML

IndentHTML makes your (X)HTML source code look “nicer”. Since this class generates easy readable output, it’s the straight opposite of TrimHTML. Note that the source code should be well-formed according to the rules of XML, because the class does not include a SGML parser.

IndentHTML.php

The constructor takes one optional string argument in order to customize the indent char(s). Sensible values are blanks and "\t", two blanks are used by default.

Requires: Preprocess

<?php

/**
 * class IndentHTML
 * Copyright (C) 2006  Dao Gottwald  <dao at design-noir.de>
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 *   http://www.opensource.org/licenses/lgpl-license.php
 *
 * @version  0.3.4
 * @url      http://design-noir.de/webdev/PHP/IndentHTML/
 */

require_once 'Preprocess.php';

class IndentHTML extends Preprocess {
    private
        $char;
    
    public function __construct ($char = '  ') {
        $this->char = $char;
    }
    
    public function run ($text) {
        $this->text =& $text;
        $t1 = $this->_escape_init_plain();
        $this->_escape_comments_cdata();
        $t2 = $this->_escape_init();
        $this->_escape_content ('pre');
        $t3 = $this->_escape_init_plain();
        $this->_escape_content ('textarea');
        $this->_escape_inline_tags();
        $text = preg_replace ('~\s+~', ' ', $text);
        $text = str_replace (' <', '<', $text);
        $text = str_replace ('> ', '>', $text);
        $this->_escape_init();
        $this->_escape_content ('option','td','th');
        $text2 = '';
        $lvl = 0;
        $len = strlen ($text);
        $closing_tag = $is_tag = false;
        $has_content = true;
        for ($i = 0; $i < $len; $i++) {
            if ($text[$i] == '<') {
                $was_closing_tag = $closing_tag;
                $closing_tag = $i+1 < $len && ($text[$i+1] == '/' || $text[$i+1] == '!');
                if ($closing_tag) {
                    $lvl -= $lvl>0;
                }
                if (($has_content || !$closing_tag || $was_closing_tag) && $i) {
                    $text2 .= "\n" . str_repeat ($this->char, $lvl);
                }
                $is_tag = true;
            } elseif ($text[$i] == '>') {
                if (!$closing_tag) {
                    $closing_tag = $i && $text[$i-1] == '/';
                }
                if (!$closing_tag) {
                    $lvl++;
                }
                $has_content = $is_tag = false;
            } elseif (!$has_content && !$is_tag) {
                $has_content = true;
                $text2 .= "\n" . str_repeat ($this->char, $lvl);
            }
            $text2 .= $text[$i];
        }
        $text = $text2;
        $this->_escape_fin();
        $this->_escape_init($t3);
        $this->_escape_fin();
        $this->_escape_init($t2);
        $this->_escape_fin();
        $this->_escape_init($t1);
        $this->_escape_fin();
        return $text;
    }
}

?>

Example #1

indent.php

Run and view the source code.

<?php

ob_start (array (new IndentHTML('    '), 'run'));

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head><title>Lorem Ipsum</title>
    </head><body>
<h1>
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit
        </h1>
    <p>
Etiam faucibus ligula ut arcu. Maecenas sed ipsum. Fusce sit amet leo at felis placerat convallis. <br/>
            Aenean vulputate nibh ut justo. <a href="#">Pellentesque</a>.
</p><p>
    Quisque viverra dui a enim molestie blandit.
    <strong>Suspendisse</strong> orci metus, volutpat sit amet, lobortis id, cursus in, mi.
Cras faucibus nisl sit amet diam. Maecenas imperdiet aliquet lacus. Suspendisse neque.
        </p>        <pre>
        .                            .                      .
.                  .             -)------+====+       .
                         -)----====    ,'   ,'   .                 .
            .                  `.  `.,;___,'                .
                                 `, |____l_\
                   _,....------c==]""______ |,,,,,,.....____ _
  .      .        "-:_____________  |____l_|]'''''''''''       .     .
                                ,'"",'.   `.
       .                 -)-----====   `.   `.              LS
                   .            -)-------+====+       .            .
           .                               .
Incom's T-65B X-wing Space
Superiority Starfighter (1)</pre>
        
        <table><caption>
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit
            </caption>
        <thead>
            <tr>
            <th>
                foo
                </th><th>
    bar
        </th>
</tr>
        </thead>
        <tbody>
            <tr><td>
                Cras posuere aliquam diam.
                Donec venenatis, diam ac vestibulum gravida.
    </td>
                <td>
            Vivamus aliquet.
                        Pellentesque habitant morbi tristique senectus et netus et.
            </td>
            </tr>        </tbody>
        </table>Ipsum.</body></html>

Comments

  1. Moe wrote on May 27, 2009 10:42 AM BST ():
    <3

HTML will not be interpreted.

Last changed on December 23, 2006 Dão G., 2005–2010
aggressiv akt andromeda bar beine blue efeu frontal fugaetu industriell komet land noir rost rot sonnenblume splash split winter wolke zeit