htmlmin icon indicating copy to clipboard operation
htmlmin copied to clipboard

Allow custom handling of <script> and <style>

Open oprypin opened this issue 11 years ago • 6 comments

It would be very useful to allow some kind of callbacks that let the user handle CSS and JS in whatever way they want, instead of just leaving them unminified.

oprypin avatar Feb 04 '15 16:02 oprypin

Use case:

import subprocess
import htmlmin

def handle_pre(tag, data):
    if tag == 'script':
        process = subprocess.Popen('uglifyjs --compress --mangle --comments'.split(),
                                   stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        data = process.communicate(input=data.encode('utf-8'))[0].decode('utf-8')
    return data

print(htmlmin.minify('''...''', handle_pre=handle_pre))

oprypin avatar Feb 04 '15 17:02 oprypin

This seems like a reasonable proposal and, hopefully, straightforward to implement. I might refer to the attribute as "pre_content_processor". We'll need to decide whether the callback gets called multiple times for nested pre-content. I lean towards only calling it once for the top-most pre-tag.

If you'd like to submit a pull-request, I'd be happy to review it. Be sure to include tests. I can't promise an estimate for delivering this myself.

mankyd avatar Feb 04 '15 17:02 mankyd

I have implemented one that just calls the function for every handle_data (which is good enough for style/script). Making it into one call for top-most tag seems very difficult to me, but it sure would be better.

oprypin avatar Feb 04 '15 18:02 oprypin

I realize that the changes I made are very minor. This will not be useful for anything outside script and style tags. But it's all I need.

I am not planning to keep working on this.

Thanks for considering this issue.

oprypin avatar Feb 04 '15 18:02 oprypin

This.

It would be nice if htmlmin could allow to minify CSS or JS as well, natively or by letting us plug in a callback.

(Minifying CSS would probably be easy to implement if I were to guess)

JeromeJ avatar Jul 15 '17 21:07 JeromeJ

This would be very good to have. There are already javascript and css minifiers for python available -- maybe those could be hooked in automatically?

girst avatar Nov 28 '17 15:11 girst