I've googled around but can't find any HTML minifacation scripts.
It occoured to me that maybe there's nothing more to HTML minifacation than removing all unneeded whitespace.
Am I missing something or has my Google Fu been lost?
I've googled around but can't find any HTML minifacation scripts.
It occoured to me that maybe there's nothing more to HTML minifacation than removing all unneeded whitespace.
Am I missing something or has my Google Fu been lost?
Sometimes, depending on the enclosing tags and/or on the CSS, whitespace may be significant.
I've used this regexp for years, without any problems:
s/>\s*</></g
In Python
re.sub(r'>\s*<', '><', html)
Or in PHP
preg_replace('/>\s*</', '><', $html);
This removed all whitespace between tags, but not anywhere, this is fairly safe (but not perfect, there are situations where this will break, but they're rare).
My main reason for doing this isn't speed/file size, but because the whitespace often introduces a, well, space. This would be okay, but when you start mucking about in your DOM with Javascript, spaces are often lost, creating (minor) layout differences.
Consider:
There's a space between the links, but now I do something like:
And there's no space ... I need to manually add the space in my JS, which is fairly ugly & error-prone IMHO.
I recently found a PHP based script that minify your sites HTML - Inline css - Inline javascript on the fly it is called as Dynamic website compressor
You can find some good references here to things like HTML tidy and others.
If you don't want to use one of those options, Prototype has a means to clean the whitespace in the DOM. You could do that on your own and copy it via 'View Generated Source' in the Firefox extension Web Developer Toolbar. Then you can replace the original html with prototype's fix. Sorry for not making that apparent nickf.
(I recommend the first link)
Couldn't JavaScript be used as a decompresser for a compressed HTML string, for instance have a DEV build for the uncompressed format, run a 'publish' script to compress the DEV build to production and attach a JavaScript to the HTML source (with the whitespace and such removed as before)?
The bandwidth would be reduced on the server, but the downside is there is a lot more client strain for decompressing the string to HTML. Also JavaScript would need to be enabled and be able to parse the decompressed string to HTML.
I am not saying its a definite solution, but something that might work - it all depends on if your looking in regards to bandwidth without the users JavaScript permissions/systems spec, or such.
Otherwise look for obfuscation scripts, a simple google search produced http://tinyurl.com/phpob - dependent on what your looking for there should be a software package available.
If I am on the wrong lines, please shout and I will see what else I can do.
Good Luck!
I haven’t tried it yet, but htmlcompressor is an HTML minifier, if you fancy giving one a try.