Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom handling of <script> and <style> #16

Open
oprypin opened this issue Feb 4, 2015 · 6 comments
Open

Allow custom handling of <script> and <style> #16

oprypin opened this issue Feb 4, 2015 · 6 comments

Comments

@oprypin
Copy link

oprypin commented Feb 4, 2015

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
Copy link
Author

oprypin commented Feb 4, 2015

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))

@mankyd
Copy link
Owner

mankyd commented Feb 4, 2015

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.

@oprypin
Copy link
Author

oprypin commented Feb 4, 2015

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
Copy link
Author

oprypin commented Feb 4, 2015

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.

@JeromeJ
Copy link

JeromeJ commented Jul 15, 2017

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)

@girst
Copy link

girst commented Nov 28, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants