tags:

views:

233

answers:

2

Does anyone know of a linux-compatible command line html formatter? You know, something where I could pass it a file that looks like:

<html>
<body>
<p>
hi
</p>
</body>
</html>

And it gives me:

<html>
    <body>
        <p>
            hi
        </p>
    </body>
</html>

I'm using PHP to generate this html, so if there is some handy way to do this via php that I'm missing?

+2  A: 

Take a look at PHP's Tidy offering.

Doug Hays
+1  A: 

You are probably looking for Tidy. It cleans and formats XML and HTML. There is also a PHP extension, but you would probably need to buffer your output and pass it through there.

Edit:
A code example:

ob_start();
// output your html
$output = ob_get_flush();

// Specify configuration
$config = array(
           'indent'         => true,
           'output-xhtml'   => true,
           'wrap'           => 200);

// Tidy
$tidy = new tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();

// Output
echo $tidy;

I didn't test this, but it should work.

mooware
Thanks, I noticed Tidy, but I didn't notice it had an indent option.
ryeguy