I’m looking for a way in nokogiri to strip out html whitespace & comment and javascript comment (/* */, //). I’m doing this not because of the size of the document. I’m playing around with rack middleware to do this job. I know I could do via regular expression, but i think it could be troublesome.
If not possible to do with nokogiri, please give me the best regular expression to strip out for the 2 above cases.
What I tried using regular expression:
response = @app.call(env)
body = response.last.body.gsub(/(\n|\t|\r)/, ' ').gsub(/>\s*</, '><').gsub(/<!--[^>]*-->/, ' ').squeeze(' ')
response.last.body = body
response
I think there should be a cleaner way to do rather than using regular expression.
I end up writing a middleware to handle this since there is no exact solution for this.
Here I use very strict regular expression to handle it.
Check the code on my github repo.