A command-line HTML pretty-printer: Making messy HTML readable
Start tidying up your HTML mess using the tidy
command:
tidy -i -m messy.html
This command not only formats (-i
) your HTML but it also overwrites (-m
) the file in-place. If you don't have tidy in your environment, you need to install it.
Browse the Tidy Project page on GitHub for the latest version and additional info. This tool can deal with the untidiest of HTML and is regularly updated to handle modern standards.
Introduction to Tidy: Installation and usage
To install tidy-html5
in a Linux system using Homebrew, run:
brew install tidy-html5
Start tidying up by following this command-line statement:
tidy -i -o pretty.html messy.html
This will beautify (-i
) the html code in messy.html
and output (-o
) the result to a new file named pretty.html
.
Advanced usage of Tidy for a better clean up
Custom configurations
Tidy allows even more flexibility via .tidyrc
. Set your own formatting preferences with this configuration file:
tidy -config .tidyrc messy.html
No more tediously setting options every time you need to tidy up - it's a .tidyrc kinda party in here.
Tidy & modern web
Thanks to the folks at HTMLTidy Community Group (HTACG), we have a Tidy variant tailor-fit to the ever-evolving web standards. It's your go-to tool for HTML5 compatibility.
Legacy code handling
Who says you can't have the best of both worlds? Tidy can make your old HTML code shine again while ensuring everything still works. Old code? New standards? No problem!
Common issues with Tidy
Understanding errors and warnings
Tidy has a knack for telling you what's wrong. Use those error messages and warnings to improve your code and your understanding. It's like having your personal HTML coach. "Remember, every error is an opportunity."
Edge cases and fixes
Sometimes, Tidy may not completely tidy things up if your HTML is way too messy. Review and manually fix the remaining issues. Tidy does its job but let's face it, it's not a miracle worker.
Other consideration when tidying your HTML
There's more than one way to tidy up
Tidy has some cool cousins: Beautiful Soup for Python, js-beautify
for JavaScript, and Pandoc for various documents, that can bring magic to your code as well.
Validation matters
Got your code tidied up? Run it through W3C Markup Validation to ensure it's not just tidy, but also valid. "Compliance is king and queen."
Prettification for production
For performance-oriented prettification, turn to html-minifier
and see your HTML shrunk for maximum speed, because "size does matter when it comes to performance."
Was this article helpful?