Example #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())
Example #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())
Example #3
0
def minify_css(app, exception):
    if exception is not None or not app.config["html_theme_options"].get(
            "css_minify", False):
        app.multiprocess_manager.shutdown()
        return
    import glob
    from css_html_js_minify.css_minifier import css_minify

    css_files = glob.glob(os.path.join(app.outdir, "**", "*.css"),
                          recursive=True)
    print("Minifying {0} css files".format(len(css_files)))
    for css_file in css_files:
        colorized = console.colorize("blue", css_file)
        msg = "minifying css file {0}".format(colorized)
        sys.stdout.write("\033[K" + msg + "\r")
        with open(css_file, "r", encoding="utf-8") as content:
            css = css_minify(content.read())
        with open(css_file, "w", encoding="utf-8") as content:
            content.write(css)
    print()
    app.multiprocess_manager.shutdown()
Example #4
0
def minify_css(value):
    return mark_safe(css_minify(value))
Example #5
0
import os
import sys
import sass
from css_html_js_minify import css_minifier

FILENAME = "main"

css_file = open(f"../css/{FILENAME}.css", "w", encoding="utf-8")
default_stdout = sys.stdout
sys.stdout = css_file

with open(f"{FILENAME}.scss", "r") as scss_file:
    print(str(sass.compile(string=scss_file.read())))
    sys.stdout = default_stdout

print(f"{FILENAME}.scss compilated to {FILENAME}.css")

min_css_file = open(f"../css/{FILENAME}.min.css", "w", encoding="utf-8")
default_stdout = sys.stdout
sys.stdout = min_css_file

with open(f"../css/{FILENAME}.css", "r") as css_file:
    print(css_minifier.css_minify(css_file.read()))
    sys.stdout = default_stdout

print(f"{FILENAME}.css minimized to {FILENAME}.min.css")
Example #6
0
def minify_css(value):
    return mark_safe(css_minify(value))
Example #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)