Esempio n. 1
0
def minify_js():
    content = ''
    urls = request.forms.get('urls').split(',')

    for url in urls:
        r = requests.get(url)
        content += r.text

    content = "window.appWillLaunch = function () {yum.enable(false);%s};" % js_minify(
        content)

    savefile('js/app.js', content)

    return js_minify(content)
Esempio n. 2
0
def download_file(request):
    if request.method == "POST":
        z = js_minify(request.POST.get('code'))
        res = HttpResponse(str(z),content_type="application/js")
        res['Content-Disposition'] = 'attachment; filename=yourname.js'
        return res
    return HttpResponse('<script>alert("error")</script>')
Esempio n. 3
0
def process_js(filepath, source, dest, config):
    with open(filepath, 'r+', encoding='utf-8') as js:
        lines = js.readlines()
        dest = os.path.join(dest, filepath[len(source) + 1:])
        new_file = '\n'.join(lines)
        if config['MINIFY']['js'] == 'True':
            new_file = js_minify(new_file)
        save_file(new_file, dest)
Esempio n. 4
0
def easy_minify(data, tool=None):
    try:
        if not tool:
            data = css_html_js_minify.html_minify(data)
        else:
            if tool == 'css':
                data = css_html_js_minify.css_minify(data)
            elif tool == 'js':
                data = css_html_js_minify.js_minify(data)
    except:
        data = re.sub('\n +<', '\n<', data)
        data = re.sub('>(\n| )+<', '> <', data)

    return last_change(data)
Esempio n. 5
0
def build(files, file_type):
    for filename in files:
        # read file
        f = open(f'{path}/{filename}', "r")
        contents = f.read()
        f.close()
        # minify contents
        if file_type == 'js':
            contents = js_minify(contents)
        if file_type == 'css':
            contents = css_minify(contents, comments=False)
        contents = remove_spaces(contents)
        # save file
        f = open(f'{path}/build/{filename}', "x")
        f.write(contents)
        f.close()
        print(f'Created {path}/build/{filename}')
Esempio n. 6
0
def clean_js():
    if not os.path.exists(gitdir + "/../js"):
        print(" -!- clean_js: JavaScript directory not found.")
    alljs = ""
    for root, dirs, files in os.walk(gitdir + "/../js"):
        for f in files:
            if f == "include_all.js":
                continue  ## This one is unnecessary in the final version, since min.js won't require any additional includes.
            fname = f.split('.')
            if fname[len(fname) -
                     1] == "js" and fname[len(fname) - 2] != "min":
                with open(root + "/" + f, "rb") as ijs:
                    alljs += ijs.read().decode()
                    if alljs[len(alljs) - 1] != ";":
                        alljs += ";"
                    alljs += "\n"
    minjs = js_minify(alljs)
    with open(tgtdir + "/min.js", "wb") as ojs:
        ojs.write(minjs.encode())
Esempio n. 7
0
def show_minified_js(request):
    if request.method == "POST":
        z = js_minify(request.POST.get('code'))
        return JsonResponse({"code":z})
    return HttpResponse('<script>alert("error")</script>')
Esempio n. 8
0
    srcfile = webroot + "/" + filename
    dstfile = srcroot + "/" + basename + ".h"

    varname = basename.upper()

    with open(srcfile, encoding="utf-8") as f:
        content = f.read().replace("${version}", version)

    try:    
        if filename.endswith(".html"):
            content = html_minify(content)
        elif filename.endswith(".css"):
            content = css_minify(content)
        elif filename.endswith(".js") or filename.endswith(".json"):
            content = js_minify(content)
    except:
        print("WARN: Unable to minify")

    with open(dstfile, "w") as dst:
        dst.write("const char ")
        dst.write(varname)
        dst.write("[] PROGMEM = R\"==\"==(")
        dst.write(content)
        dst.write(")==\"==\";\n")
        dst.write("const int ");
        dst.write(varname)
        dst.write("_LEN PROGMEM = ");
        dst.write(str(len(content)))
        dst.write(";");
        
Esempio n. 9
0
                    )

                fh.close()

            output += "};"

        # generate javascript output
        elif isJs(outputFileName):
            print("Building javascript file '", outputFileName, "'")

            for file in contentFiles:
                print("   ", file)
                fh = open(os.path.join(resDir, file), "r")

                if isJs(file):
                    output += js_minify(fh.read())
                else:
                    print(
                        "  \033[91m\033[1mERROR: NO VALID FILE EXTENSION. SUPPORTED: .js\033[0m"
                    )

                fh.close()

        # generate css output (Input can be css and sass)
        elif isCss(outputFileName):
            print("Building css file '", outputFileName, "'")

            for file in contentFiles:
                print("   ", file)
                fh = open(os.path.join(resDir, file), "r")
Esempio n. 10
0
def bundle():

    #Check to see if output files exist, if so remove them
    if os.path.exists(cssoutput):
        os.remove(cssoutput)

    if os.path.exists(jsoutput):
        os.remove(jsoutput)

    #This part is a WIP. Please don't judge too hard. Trying to look through node_modules to find javascript and css files in packages specified in the config.
    for module in modules:
        fullpath = './node_modules/' + module

        #This one checks if the node module exists or can be found
        if os.path.exists(fullpath):

            #Created a couple different paths to look for
            myfile = fullpath + '/dist/js/' + module + '.min.js'
            myfile2 = fullpath + '/dist/' + module + '.min.js'

            #Check if file exists, if found it performs same bundling operation as the javascript one documented below.
            if os.path.exists(myfile):
                output = "//FILE OUTPUT FROM: " + myfile + "\n"
                #output += js_minify(str(f.read()))
                os.makedirs(os.path.dirname(jsoutput), exist_ok=True)
                with open(myfile, 'r') as mf:
                    with open(jsoutput, 'a+') as out:
                        for line in mf:
                            out.write(line)
                success_msg('Processed: ', myfile)
            #Performing check of second directory
            elif os.path.exists(myfile2):
                f = open(myfile2, "r")
                output = "//FILE OUTPUT FROM: " + myfile2 + "\n"
                output += js_minify(str(f.read()))
                os.makedirs(os.path.dirname(jsoutput), exist_ok=True)
                with open(jsoutput, 'a+') as out:
                    out.write(output + '\n\n')
                f.close()
                success_msg('Processed: ', myfile2)
            else:
                #Lets user know both directories checked on fail.
                error_output('File not found: ', myfile)
                error_output('Also checked: ', myfile2)
        else:
            #Error when node module not found
            error_output('Node module could not be located: ', fullpath)

        checkcss = fullpath + '/dist/css/' + module + '.min.css'
        if os.path.exists(checkcss):
            f = open(checkcss, "r")
            output = "/*FILE OUTPUT FROM: " + checkcss + "*/\n"
            output += css_minify(str(f.read()))
            os.makedirs(os.path.dirname(cssoutput), exist_ok=True)
            with open(cssoutput, 'a+') as out:
                out.write(output + '\n\n')
            f.close()
            success_msg('Processed: ', checkcss)

    #loops through the javascript files specified in config
    for file in jsfiles:

        #create full path using js path in config
        fullpath = jspath + file

        if os.path.exists(fullpath):
            #Check to see if file exists, on success open file, minify, and append to output specified in config. Close files
            f = open(fullpath, "r")
            output = "//FILE OUTPUT FROM: " + fullpath + "\n"
            output += js_minify(str(f.read()))
            os.makedirs(os.path.dirname(jsoutput), exist_ok=True)
            with open(jsoutput, 'a+') as out:
                out.write(output + '\n\n')
            f.close()
            success_msg('Processed: ', fullpath)
        else:
            #File not found
            error_output('File not found: ', fullpath)

        #See if any paths were provided to be forced into bundle
    if forcejs:
        #Loop through paths
        for fullpath in forcejs:
            #Check if they exist
            if os.path.exists(fullpath):
                #Run file tasks
                f = open(fullpath, "r")
                output = "//FILE OUTPUT FROM: " + fullpath + "\n"
                output += js_minify(str(f.read()))
                os.makedirs(os.path.dirname(jsoutput), exist_ok=True)
                with open(jsoutput, 'a+') as out:
                    out.write(output + '\n\n')
                f.close()
                success_msg('Processed: ', fullpath)
            else:
                error_output('File not found: ', file)

    #This works identically to the javascript loop. Just uses the css vars from the config.
    for file in cssfiles:
        fullpath = csspath + file
        if os.path.exists(fullpath):
            f = open(fullpath, "r")
            output = "/*FILE OUTPUT FROM: " + fullpath + "*/\n"
            output += css_minify(str(f.read()))
            os.makedirs(os.path.dirname(cssoutput), exist_ok=True)
            with open(cssoutput, 'a+') as out:
                out.write(output + '\n\n')
            f.close()
            success_msg('Processed: ', fullpath)
        else:
            error_output('File not found: ', fullpath)

    #Same as JS one
    if forcecss:
        for fullpath in forcecss:
            if os.path.exists(fullpath):
                f = open(fullpath, "r")
                output = "//FILE OUTPUT FROM: " + fullpath + "\n"
                output += css_minify(str(f.read()))
                os.makedirs(os.path.dirname(cssoutput), exist_ok=True)
                with open(cssoutput, 'a+') as out:
                    out.write(output + '\n\n')
                f.close()
                success_msg('Processed: ', fullpath)
            else:
                error_output('File not found: ', file)
Esempio n. 11
0
 def run(self, text):
     from css_html_js_minify import js_minify
     return js_minify(text)
Esempio n. 12
0
def generate_fs():
    files = [
        p for p in sorted(fsdir.iterdir())
        if p.is_file() and p.suffix in extensions
    ]
    if all(file.stat().st_mtime < outfile.stat().st_mtime for file in files):
        print('generated file up to date')
        #return

    with outfile.open('w') as fout:
        fout.write('#include "stddef.h"\n')
        fout.write('#include "httpd-fsdata.h"\n\n')
        fout.write('#define file_NULL (struct fsdata_file *) NULL\n')

        filecount = 0

        for file in files:
            if (unify):
                if (file.suffix not in ['.htm', '.html']):
                    continue
                with file.open() as fin:
                    html = fin.read()
                    if (minify): html = html_minify(html)
                    soup = bs4.BeautifulSoup(html, 'html.parser')
                    for style in soup.findAll("link", {"rel": "stylesheet"}):
                        if (style["href"][0:4] == "http"):
                            continue
                        css_path = fsdir / style["href"]
                        if css_path.is_file():
                            with css_path.open() as css_file:
                                css = css_file.read()
                                if (minify): css = css_minify(css)
                                c = bs4.element.NavigableString(css)
                                tag = soup.new_tag('style')
                                tag.insert(0, c)
                                tag['type'] = 'text/css'
                                style.replaceWith(tag)
                    for script in soup.findAll("script"):
                        if not script.has_attr('src'): continue
                        if script['src'][0:4] == 'http': continue
                        js_path = fsdir / script["src"]
                        if js_path.is_file():
                            with js_path.open() as js_file:
                                js = js_file.read()
                                if (minify): js = js_minify(js)
                                j = bs4.element.NavigableString(js)
                                tag = soup.new_tag('script')
                                tag.insert(0, j)
                                script.replaceWith(tag)
                    for image in soup.findAll("img"):
                        if not image.has_attr('src'): continue
                        if image['src'][0:4] == 'http': continue
                        img_path = fsdir / image["src"]
                        if img_path.is_file():
                            with img_path.open() as img_file:
                                img_data = img_file.read()
                                if img_path.suffix == '.svg':
                                    img_tag = '<img src=\'data:image/svg+xml;utf8,{0}\'>'.format(
                                        img_data)
                                elif img_path.suffix == '.png':
                                    img_data = img_data.encode(
                                        'base64').replace('\n', '')
                                    img_tag = '<img src="data:image/png;base64,{0}">'.format(
                                        data_uri)
                                tag = bs4.BeautifulSoup(img_tag, 'html.parser')
                                image.replaceWith(tag)
                    data = soup.decode(formatter="html")
            else:
                with file.open() as fin:
                    data = fin.read()
                    if (minify):
                        if (file.suffix in ['.htm', '.html']):
                            data = html_minify(data)
                        elif (file.suffix in ['.js']):
                            data = js_minify(data)
                        elif (file.suffix in ['.css']):
                            data = css_minify(data)

            print(file.name)

            filename_enc = file.name.translate(str.maketrans('.-', '__'))
            filename_slash = '/' + file.name
            fout.write(
                'static const unsigned int dummy_align__{} = {};\n'.format(
                    filename_enc, filecount))
            fout.write('static const unsigned char data__{}[] = {{\n'.format(
                filename_enc))
            fout.write('\n\n')

            fout.write('/* /{} ({} chars) */\n'.format(file.name,
                                                       len(filename_slash) +
                                                       1))
            filename_hex = ",".join("0x{:02x}".format(c)
                                    for c in filename_slash.encode()) + ','
            filename_hex += '0x00,' * (4 - (len(filename_slash) % 4))
            fout.write('\n'.join(textwrap.wrap(filename_hex, 80)))
            fout.write('\n\n')

            data = data.encode()
            if (file.suffix in gzip_extensions):
                data = gzip_encode(data)
            data_hex = ",".join("0x{:02x}".format(c) for c in data)
            fout.write('/* raw file data ({} bytes) */\n'.format(len(data)))
            fout.write('\n'.join(textwrap.wrap(data_hex, 80)))
            fout.write(',\n};\n')
            fout.write('\n\n')

            filecount += 1

        last_file = ''
        for file in files:
            if (unify):
                if (file.suffix not in ['.htm', '.html']):
                    continue
            filename_enc = file.name.translate(str.maketrans('.-', '__'))
            filename_slash = '/' + file.name
            fout.write('const struct fsdata_file file__{}[] = {{ {{\n'.format(
                filename_enc))
            if last_file == '': fout.write('file_NULL,\n')
            else: fout.write('file__{},\n'.format(last_file))
            fout.write('data__{},\n'.format(filename_enc))
            fout.write('data__{} + {},\n'.format(
                filename_enc,
                len(filename_slash) + (4 - (len(filename_slash) % 4))))
            fout.write('sizeof(data__{}) - {},\n'.format(
                filename_enc,
                len(filename_slash) + (4 - (len(filename_slash) % 4))))
            fout.write('#ifdef HTTPD_FS_STATISTICS\n')
            fout.write('#if HTTPD_FS_STATISTICS == 1\n')
            fout.write('0,\n')
            fout.write('#endif /* HTTPD_FS_STATISTICS */\n')
            fout.write('#endif /* HTTPD_FS_STATISTICS */\n')
            fout.write('} };\n')
            fout.write('\n\n')
            last_file = filename_enc

        fout.write('const struct fsdata_file *fs_root = file__{};\n'.format(
            last_file))
        fout.write('#ifdef HTTPD_FS_STATISTICS\n')
        fout.write('#if HTTPD_FS_STATISTICS == 1\n')
        fout.write('uint16_t fs_count[{}];\n'.format(filecount))
        fout.write('#endif /* HTTPD_FS_STATISTICS */\n')
        fout.write('#endif /* HTTPD_FS_STATISTICS */\n')
Esempio n. 13
0
 def compress_js(self, js):
     from css_html_js_minify import js_minify
     return js_minify(js)