2017 © Pedro Peláez
 

library dindent

HTML indentation library for development and testing.

image

gajus/dindent

HTML indentation library for development and testing.

  • Monday, June 5, 2017
  • by gajus
  • Repository
  • 9 Watchers
  • 111 Stars
  • 33,502 Installations
  • PHP
  • 10 Dependents
  • 0 Suggesters
  • 28 Forks
  • 13 Open issues
  • 5 Versions
  • 10 % Grown

The README.md

Dindent

Build Status Coverage Status Latest Stable Version License, (*1)

Dindent (aka., "HTML beautifier") will indent HTML for development and testing. Dedicated for those who suffer from reading a template engine produced markup., (*2)

Abuse Case

Dindent will not sanitize or otherwise manipulate your output beyond indentation., (*3)

If you are looking to remove malicious code or make sure that your document is standards compliant, consider the following alternatives:, (*4)

If you need to indent your code in the development environment, beware that earlier mentioned libraries will attempt to fix your markup (that's their primary purpose; indentation is a by-product)., (*5)

Regex

There is a good reason not to use regular expression to parse HTML. However, DOM parser will rebuild the whole HTML document. It will add missing tags, close open block tags, or remove anything that's not a valid HTML. This is what Tidy does, DOM, etc. This behavior is undesirable when debugging HTML output. Regex based parser will not rebuild the document. Dindent will only add indentation, without otherwise affecting the markup., (*6)

The above is also the reason why Chrome DevTools is not a direct replacement for Dindent., (*7)

Use

$indenter = new \Gajus\Dindent\Indenter();
$indenter->indent('[..]');

In the above example, [..] is a placeholder for:, (*8)

<!DOCTYPE html>
<html>
<head></head>
<body>
    <script>
    console.log('te> <st');
    function () {
        test; <!-- <a> -->
    }
    </script>
    <div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <div><table border="1" style="background-color: red;"><tr><td>A cell    test!</td>
<td colspan="2" rowspan="2"><table border="1" style="background-color: green;"><tr> <td>Cell</td><td colspan="2" rowspan="2"></td></tr><tr>
        <td><input><input><input></td></tr><tr><td>Cell</td><td>Cell</td><td>Ce
            ll</td></tr></table></td></tr><tr><td>Test <span>Ce       ll</span></td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table></div></div>
</body>
</html>

Dindent will convert it to:, (*9)

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <script>
    console.log('te> <st');
    function () {
        test; <!-- <a> -->
    }
    </script>
        <div>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
            <div>
                <table border="1" style="background-color: red;">
                    <tr>
                        <td>A cell test!</td>
                        <td colspan="2" rowspan="2">
                            <table border="1" style="background-color: green;">
                                <tr>
                                    <td>Cell</td>
                                    <td colspan="2" rowspan="2"></td>
                                </tr>
                                <tr>
                                    <td>
                                        <input>
                                        <input>
                                        <input>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Cell</td>
                                    <td>Cell</td>
                                    <td>Ce ll</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>Test <span>Ce ll</span></td>
                    </tr>
                    <tr>
                        <td>Cell</td>
                        <td>Cell</td>
                        <td>Cell</td>
                    </tr>
                </table>
            </div>
        </div>
    </body>
</html>

Options

Indenter constructor accepts the following options that control indentation:, (*10)

Name Description
indentation_character Character(s) used for indentation. Defaults to 4 whitespace characters.

Set element type

HTML elements are either "inline" elements or "block-level" elements., (*11)

An inline element occupies only the space bounded by the tags that define the inline element. The following example demonstrates the inline element's influence:, (*12)

<p>This is an <span>inline</span> element within a block element.</p>

A block-level element occupies the entire space of its parent element (container), thereby creating a "block." Browsers typically display the block-level element with a new line both before and after the element. The following example demonstrates the block-level element's influence:, (*13)

<div>
    <p>This is a block element within a block element.</p>
</div>

Dindent identifies the following elements as "inline":, (*14)

  • b, big, i, small, tt
  • abbr, acronym, cite, code, dfn, em, kbd, strong, samp, var
  • a, bdo, br, img, span, sub, sup

This is a subset of the inline elements defined in the MDN., (*15)

All other elements are treated as block., (*16)

You can set element type to either block or inline using setElementType method:, (*17)

$indenter = new \Gajus\Dindent\Indenter();
/**
 * @param string $element_name Element name, e.g. "b".
 * @param ELEMENT_TYPE_BLOCK|ELEMENT_TYPE_INLINE $type
 * @return null
 */
$indenter->setElementType('foo', \Gajus\Dindent\Indenter::ELEMENT_TYPE_BLOCK);
$indenter->setElementType('bar', \Gajus\Dindent\Indenter::ELEMENT_TYPE_INLINE);

CLI

Dindent can be used via the CLI script ./bin/dindent.php., (*18)

php ./bin/dindent.php

Indent HTML.

Options:
    --input=./input_file.html
        Input file
    --indentation_character="    "
        Character(s) used for indentation. Defaults to 4 whitespace characters.
    --inline
        A list of comma separated "inline" element names.
    --block
        A list of comma separated "block" element names.

Examples:
    ./dindent.php --input="./input.html"
        Indent "input.html" file and print the output to STDOUT.

    ./dindent.php --input="./input.html" | tee ./output.html
        Indent "input.html" file and dump the output to "output.html".

    ./dindent.php --input="./input.html" --indentation_character="\t"
        Indent "input.html" file using tab to indent the markup.

    ./dindent.php --input="./input.html" --inline="div,p"
        Indent "input.html" file treating <div> and <p> elements as inline.

    ./dindent.php --input="./input.html" --block="span,em"
        Indent "input.html" file treating <span> and <em> elements as block.

Known issues

  • Does not treat comments nicely and IE conditional blocks.

Installation

The recommended way to use Dindent is through Composer., (*19)

{
    "require": {
        "gajus/dindent": "2.0.*"
    }
}

The Versions

05/06 2017

dev-master

9999999-dev https://github.com/gajus/dindent

HTML indentation library for development and testing.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-mbstring *

 

The Development Requires

by Gajus Kuizinas

html format indent

08/10 2014

2.0.2

2.0.2.0 https://github.com/gajus/dindent

HTML indentation library for development and testing.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3

 

The Development Requires

by Gajus Kuizinas

html format indent

08/10 2014

2.0.1

2.0.1.0 https://github.com/gajus/dindent

HTML indentation library for development and testing.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3

 

The Development Requires

by Gajus Kuizinas

html format indent

17/06 2014

2.0.0

2.0.0.0 https://github.com/gajus/dindent

HTML indentation library for development and testing.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3

 

The Development Requires

by Gajus Kuizinas

html format indent

16/04 2014

1.0.0

1.0.0.0 https://github.com/gajus/dindent

HTML indentation library for development and testing.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3

 

The Development Requires

by Gajus Kuizinas

html format indent