9

I'd like to automatically pretty-print (indentation, mostly) the HTML output that my PHP scripts generate. I've been messing with Tidy, but have found that in its efforts to validate and clean my code, Tidy is changing way too much. I know Tidy's intentions are good but I'm really just looking for an HTML beautifier. Is there a simpler library out there that can run in PHP and just do the pretty-printing? Or, is there a way to configure Tidy to skip all the validation stuff and just beautify?

brianjcohen
  • 965
  • 2
  • 10
  • 14
  • 1
    see [htmLawed](http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/tidy.htm) if it suit your need – jayxhj Oct 16 '15 at 03:41

4 Answers4

4

The behaviour that you've observed when using Tidy is a result of the underlying use of DOM API. Instead of manipulating the provided source code, DOM API will reconstruct the whole source, thus making fixes along the way.

I've written Dindent, which is a library that uses Regex. It does not do anything beyond adding the indentation and removing whitespaces. However, I advise against using this implementation beyond development purposes.

Gajus
  • 69,002
  • 70
  • 275
  • 438
  • The original demo URL died. Here is a link to a working demo. http://gajus.com/sandbox/dindent/sandbox/ Note that while the demo URL might stop working in the future, the codebase itself is stable. Unless your markup is written using HTML6 (future!), it will work as expected. – Gajus Oct 19 '15 at 22:02
3

I've never used Tidy but it seems pretty customizable.

Here's the quick reference of configuration options: http://tidy.sourceforge.net/docs/quickref.html

But really, with tools like Firebug, I've never seen the need to Tidy HTML output.

Brian Ortiz
  • 1,821
  • 1
  • 20
  • 47
  • Brian - you are right that Firebug will organize/indent HTML, which is fine for development. The point here is to clean up the HTML so that, when viewing source via a browser, the code appears legible and clean. This is for presentation, not development, purposes. For example, if a prospective client views-source on my site, he can see that it's not a "mess". – brianjcohen Dec 24 '10 at 18:50
  • 1
    Tidy is a great solution; however, to help you apply it with PHP, a quick lookup shows http://devzone.zend.com/article/761 as a helpful resource. – PlagueEditor Dec 24 '10 at 18:50
  • Plague - I had already read that article, but thank you for posting. Unfortunately it doesn't answer the original question either, which is whether it's possible to skip all the validating and just do code-beautifying? – brianjcohen Dec 24 '10 at 18:52
  • Surely if a client knows enough about HTML to know when it's messy then they'll also know that it's irrelevant. – Brian Ortiz Dec 24 '10 at 18:58
  • 1
    I asked an objective question: are there existing libraries in PHP that can beautify HTML output without validating it? – brianjcohen Dec 24 '10 at 19:05
  • 1
    As a matter of fact, the tendency these days is to minify and obfuscate your code for security reasons, which is the opposite of indenting and making it look good. Trust me on this: I've never had any client look at my HTML and say: "Wow, looks nicely indented and all!" – alcfeoh Oct 19 '15 at 17:42
2

Since you do not want to have it validate for whatever reason, I will not suggest htmlpurifier ; ). Why not just use an IDE to get everything indented nicely, like Alt-Shift-F in Netbeans.

brian_d
  • 11,190
  • 5
  • 47
  • 72
  • I agree, probably code has to be valid for parser, and if code is written from scratch with good IDE, no need for this scripts. – Dejan Marjanović Dec 24 '10 at 22:08
  • You're assuming it's a single HTML document on disk. The HTML is generated through a template system through multiple conditional includes and by multiple developers and designers. In the effort to keep individual sub-templates legible, they generally all have their own indentation scheme. By any measure, the smartest thing here is to beautify the code in post-processing (smarty outputfilter). – brianjcohen Dec 24 '10 at 22:59
0

Facing the same problem i currently use a combination of two commands:

 cat template-home.php | js-beautify --type html | prettier --parser php

js-beautify formats the html bits and prettier formats the php code

Julian Weimer
  • 120
  • 1
  • 2
  • 9