This short article, explaining how to trim unnecessary code (comments, empty lines) from HTML documents, is a follow-up to an article published a couple of weeks ago on this blog: Building Web Applications With Apache Ant. Basically, the idea is to use Ant’s optional replaceregexp
task as shown below:
Update: Use this code very carefully as it is dangerous territory (Thanks to my co-worker Ryan Grove for pointing out some of the shortcomings)
Awesome. Keep the ant scripts coming they are great.
Pingback: Web Application Construction Tools » Chris Norton
Pingback: ThemePassion - Best stuff about design! » Trimming comments in HTML documents using Apache Ant
How do you prevent the regex from removing Javascript enclosed in HTML comments in order to hide it from browsers, which have Javascript disabled?
I fiddled around a lot but never managed to get ant ignore comments which end with //->. It always results in an infinite loop. :-(
@Steve
Do not wrap inline JavaScript code inside HTML comments. Nobody uses Netscape 1 anymore…
Here’s one for removing console.log within a combined yui compressed file.
<replaceregexp file="my_combined_file.js" match="(console\.log\(.*\))" flags="g" replace="\/\/\1"></replaceregexp>