Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java lib to compress html file?

Does anyone know any good java library (or single method) that can strip extra spaces (line breaks, tabs, etc) from an html file? So html file gets turned into 1 line basically.

Thanks.

UPDATE: Looks like there is no library that does that so I created my own open source project for solving this task: http://code.google.com/p/htmlcompressor/

like image 565
serg Avatar asked Mar 06 '09 03:03

serg


3 Answers

Personally, I just enabled HTTP compression in the server and I leave my HTML readable.

But for what you want, you could just use String.replaceAll() with a regex that matching what you have specified. Off the top of my head, something like:

small=large.replaceAll("\\s{2,}"," ");
like image 150
Lawrence Dol Avatar answered Sep 26 '22 02:09

Lawrence Dol


Looks like there is no library that does that so I created my own open source project for solving this task, maybe someone will find it helpful: http://code.google.com/p/htmlcompressor/

like image 22
serg Avatar answered Sep 24 '22 02:09

serg


Be careful with that. Text inside pre and textarea elements will be damaged. In addition, inlined javascript inside script elements will have to be ended with column;. Lastly if you code inlined javascript with html comments (to avoid some old browser buggy behavior) this will eventually comment out the whole inlined javascript code.

Why do you want to do that? If you want to decrease the download size of the html then all you need is a GZIP filter.

like image 28
cherouvim Avatar answered Sep 23 '22 02:09

cherouvim