How can I erase spaces and newlines using Mako patterns? My 12362 line HTML file kills IE

I use the Mako template system on my Pylons website and am having trouble removing spaces.

My reasoning for removing spaces is the generated HTML file, which is issued as 12363 lines of code. This, I suppose, is the reason Internet Explorer hangs when it tries to download it.

I want my HTML file to look nice and neat, so I can easily make changes to it, and the generated output looks as ugly and messy as required to reduce file size and memory usage.

The Mako documentation http://www.makotemplates.org/docs/filtering.html says you can use the flag trim, but that doesn't work. Code example:

<div id="content">
    ${next.body() | trim}
</div>

The only way I was able to split the lines of a newline is to add \(backslash) to the end of each line. This is pretty annoying when coding views, and I'd rather have a centralized solution.

How to remove spaces / newlines?

+3
source share
3 answers

, html . , . .

<%!
    def better_trim(html):
        clean_html = ''
        for line in html:
            clean_html += line.strip()
        return clean_html
%>

<div id="content">
    ${next.body() | better_trim}
</div>
0

, IE - . , , IE-, . , , ( ) , .

- HTML- .

0

WSGI (, , ).

The following is a brief example that may help you get started. Note: I wrote this code without testing or even running it; it may work for the first time, but probably it will not meet your needs, so you will need to expand it yourself.

class HTMLMinifyMiddleware(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        resp_body = self.app(environ, start_response)

        for i, part in enumerate(resp_body):
            resp_body[i] = ' '.join(part.split())

        return resp_body
0
source

Source: https://habr.com/ru/post/1763008/


All Articles