Exemplo n.º 1
0
def inline_static_file(path, minify=None):
    """
    Outputs the [minified] contents of a given static file.

    For example, to display the minified CSS file "``inline.css``"::

        <style>
            {% inline_static_file 'inline.css' 'css' %}
        </style>

    The optional ``minify`` argument can be one of css, js, or html.
    """
    p = finders.find(path)

    if not p:
        raise RuntimeError('path=%s not found' % path)
    elif os.path.isdir(p):
        raise RuntimeError('path=%s is not a file' % path)

    with open(p, encoding='utf-8') as f:
        if minify == 'js':
            return mark_safe(js_minify(f.read()))
        elif minify == 'css':
            return mark_safe(css_minify(f.read()))
        elif minify == 'html':
            return mark_safe(html_minify(f.read()))
        else:
            return mark_safe(f.read())
Exemplo n.º 2
0
def inline_static_file(path, minify=None):
    """
    Outputs the [minified] contents of a given static file.

    For example, to display the minified CSS file "``inline.css``"::

        <style>
            {% inline_static_file 'inline.css' 'css' %}
        </style>

    The optional ``minify`` argument can be one of css, js, or html.
    """
    p = finders.find(path)

    if not p:
        raise RuntimeError('path=%s not found' % path)
    elif os.path.isdir(p):
        raise RuntimeError('path=%s is not a file' % path)

    with open(p, encoding='utf-8') as f:
        if minify == 'js':
            return mark_safe(js_minify(f.read()))
        elif minify == 'css':
            return mark_safe(css_minify(f.read()))
        elif minify == 'html':
            return mark_safe(html_minify(f.read()))
        else:
            return mark_safe(f.read())
Exemplo n.º 3
0
def reformat_pages(app, exception):
    if exception is not None or not app.site_pages:
        return
    minify = app.config["html_theme_options"].get("html_minify", False)
    last = -1
    npages = len(app.site_pages)
    transform = "Minifying" if minify else "Prettifying"
    print("{0} {1} files".format(transform, npages))
    transform = transform.lower()
    # TODO: Consider using parallel execution
    for i, page in enumerate(app.site_pages):
        if int(100 * (i / npages)) - last >= 1:
            last = int(100 * (i / npages))
            color_page = console.colorize("blue", page)
            msg = "{0} files... [{1}%] {2}".format(transform, last, color_page)
            sys.stdout.write("\033[K" + msg + "\r")
        with open(page, "r", encoding="utf-8") as content:
            if minify:
                from css_html_js_minify.html_minifier import html_minify

                html = html_minify(content.read())
            else:
                soup = BeautifulSoup(content.read(), features="lxml")
                html = soup.prettify()
        with open(page, "w", encoding="utf-8") as content:
            content.write(html)
    app.site_pages[:] = []
    print()
Exemplo n.º 4
0
def minify_html(value):
    return mark_safe(html_minify(value))
Exemplo n.º 5
0
def minify_html(value):
    return mark_safe(html_minify(value))
Exemplo n.º 6
0
def html_minify(file, comments=False):
    with open(file) as source_fh:
        original = source_fh.read().decode('utf-8')
    return html_minifier.html_minify(original, comments)
Exemplo n.º 7
0
def minify(files):
    global progmem_definitions, webserver_rules

    for f in files:

        if f not in EXCLUSIONS:
            minified = ''
            type_name = ''

            if f.endswith('.html'):
                print('[+] Minifying ' + f + '...')
                minified = html_minifier.html_minify(read_file(f))
                type_name = 'Page'
            elif f.endswith('.css'):
                print('[+] Minifying ' + f + '...')
                minified = css_minifier.css_minify(read_file(f))
                type_name = 'Style'
            elif f.endswith('.js'):
                print('[+] Minifying ' + f + '...')
                minified = jsmin(read_file(f))
                type_name = 'Script'
            elif f.endswith('.json'):
                print('[+] Minifying ' + f + '...')
                minified = minify_json(read_file(f))
                type_name = 'Json'
            elif f.endswith('.lang'):
                print('[+] Minifying ' + f + '...')
                minified = minify_json(read_file(f))
                type_name = 'Lang'

            if minified is not '':
                name = f.split('.')[0]
                try:
                    name = name.split('/')[1]
                except:
                    pass
                name = name + type_name
                hex_formatted_content = ''

                print('[+] Compressing ' + f + '...')

                hex_content = binascii.hexlify(compress(minified))
                hex_content = hex_content.decode('UTF-8')
                hex_content = [
                    hex_content[i:i + 2]
                    for i in range(0, len(hex_content), 2)
                ]

                for char in hex_content:
                    hex_formatted_content += '0x' + char + ', '

                hex_formatted_content = hex_formatted_content[:-2]
                progmem_definitions += 'const char ' + name + \
                    '[] PROGMEM = {' + hex_formatted_content + '};\n'
                rule = """
                server.on("/""" + f + """\", []() {
                    sendProgmem(""" + name + """, sizeof(""" + name + """), \"""" + TYPES[
                    f.split('.')[1]] + """\");
                });
                """
                webserver_rules += textwrap.dedent(rule)