Allow custom handling of <script> and <style>
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.
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))
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.
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.
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.
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)
This would be very good to have. There are already javascript and css minifiers for python available -- maybe those could be hooked in automatically?