Пример #1
0
    def test_shuffle(self):

        mini = Minifier(self.html)
        self.assertEqual(
            mini.minify(),
            "<!DOCTYPE html><html><head><title>1 2 3 4 5</title></head><body>esto es una prueba</body></html>"
        )
Пример #2
0
    def generate_zip_file(zip_files_map, minify_resources=False):
        temp_dir = tempfile.mkdtemp()
        temp_write_dir = os.path.abspath(os.path.join(temp_dir, "files"))
        os.makedirs(temp_write_dir)

        for path_map in zip_files_map:
            rel_path = path_map["zipPath"]
            local_path = path_map["localPath"]
            if rel_path is not None:
                rel_path = rel_path.strip(os.sep)
                temp_output_file_path = os.path.abspath(
                    os.path.join(temp_write_dir, rel_path))
                if not os.path.exists(os.path.dirname(temp_output_file_path)):
                    os.makedirs(os.path.dirname(temp_output_file_path))
                if minify_resources:
                    r_file = open(local_path, 'r')
                    w_file = open(temp_output_file_path, 'w')
                    if local_path.endswith('.js'):
                        w_file.write(jsmin(r_file.read(), quote_chars="'\"`"))
                    elif local_path.endswith('.html') or local_path.endswith(
                            '.css'):
                        html_content = Minifier(r_file.read())
                        w_file.write(html_content.minify())
                    else:
                        shutil.copyfile(local_path, temp_output_file_path)
                    r_file.close()
                    w_file.close()
                else:
                    shutil.copyfile(path_map["localPath"],
                                    temp_output_file_path)

        return shutil.make_archive(
            os.path.abspath(os.path.join(temp_dir, "archive")), 'zip',
            temp_write_dir)
Пример #3
0
 def convert(value, config_name, minify_resources=False):
     if (type(value) == type("")):
         if value.startswith("<") == False or value.lower().endswith(
                 "</html>") == False:
             raise ExportError(config_name +
                               ": String should be a valid HTML template")
         if minify_resources:
             html_content = Minifier(value)
             return html_content.minify()
         else:
             return value
     else:
         raise ExportError(
             "'%s' of type '%s' is unsupported. Supported data types is string"
             % (config_name, type(value).__name__))
Пример #4
0
#coding=utf-8

import urllib2
import urllib
import re
import time
import threading
import socket
import os
import json

import sys  

from html_minifier.minify import Minifier
minifier = Minifier()

reload(sys)  
sys.setdefaultencoding('utf8')  

socket.setdefaulttimeout(500)


error_file_list=[]


# list_pattern=re.compile(r'{\'title\':\'.*?\'<!--VIDEOSTR-->\'}', re.S)
# content_search=re.compile(r'<div class="body" id="content_body">(.*?)</div>', re.S).search
# file_name_search=re.compile(r'\d{4}/\d{2}/\d{2}').search

# list_pattern=re.compile(r'<li><span class="left ml20">(.*?)</a>\n</span><span class="left ml20">2014-06-(?:19|20)', re.S)
list_pattern=re.compile(r'<li>(.*?)</li>', re.S)
Пример #5
0
    def __process_config_values(self,
                                minify_resources=False,
                                export_bulk=True):
        configs = self.__configs.copy()
        zip_files_map = []

        configs["clientName"] = "Python"
        if not export_bulk:
            configs["exportBulk"] = ""

        if Constants.EXPORT_CONFIG_NAME_INPUTSVG in configs:
            self.__resolve_zip_path_config(
                configs, zip_files_map, Constants.EXPORT_CONFIG_NAME_INPUTSVG,
                Constants.EXPORT_CONFIG_ZIP_PATH_INPUTSVG)

        if Constants.EXPORT_CONFIG_NAME_CALLBACK_FILE_PATH in configs:
            self.__resolve_zip_path_config(
                configs, zip_files_map,
                Constants.EXPORT_CONFIG_NAME_CALLBACK_FILE_PATH,
                Constants.EXPORT_CONFIG_ZIP_PATH_CALLBACK_FILE_PATH)

        if Constants.EXPORT_CONFIG_NAME_DASHBOARD_LOGO in configs:
            ext = os.path.splitext(
                configs[Constants.EXPORT_CONFIG_NAME_DASHBOARD_LOGO])[1]
            self.__resolve_zip_path_config(
                configs, zip_files_map,
                Constants.EXPORT_CONFIG_NAME_DASHBOARD_LOGO,
                Constants.EXPORT_CONFIG_ZIP_PATH_DASHBOARD_LOGO + ext)

        if Constants.EXPORT_CONFIG_NAME_OUTPUT_FILE_DEFINITION in configs:
            self.__resolve_zip_path_config(
                configs, zip_files_map,
                Constants.EXPORT_CONFIG_NAME_OUTPUT_FILE_DEFINITION,
                Constants.EXPORT_CONFIG_ZIP_PATH_OUTPUT_FILE_DEFINITION)

        if Constants.EXPORT_CONFIG_NAME_TEMPLATE_FILE_PATH in configs:
            fileContent = configs[
                Constants.EXPORT_CONFIG_NAME_TEMPLATE_FILE_PATH]
            # If it is not a file but html content, then save the content to a temp file and set the path of that temp file
            if (fileContent.startswith("<")):
                tmp = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
                if minify_resources:
                    html_content = Minifier(fileContent)
                    tmp.writelines(html_content.minify())
                else:
                    tmp.writelines(fileContent)
                tmp.close()
                configs[
                    Constants.EXPORT_CONFIG_NAME_TEMPLATE_FILE_PATH] = tmp.name

            template_zip_files_map, prefixed_template_zip_path = Utils.create_template_zip_paths(
                configs[Constants.EXPORT_CONFIG_NAME_TEMPLATE_FILE_PATH],
                configs.get(Constants.EXPORT_CONFIG_NAME_RESOURCE_FILE_PATH,
                            None))
            configs[
                Constants.
                EXPORT_CONFIG_NAME_TEMPLATE_FILE_PATH] = prefixed_template_zip_path
            zip_files_map.extend(template_zip_files_map)

        if Constants.EXPORT_CONFIG_NAME_ASYNC_CAPTURE in configs:
            bool_val = configs[Constants.EXPORT_CONFIG_NAME_ASYNC_CAPTURE]
            configs[Constants.EXPORT_CONFIG_NAME_ASYNC_CAPTURE] = str(
                bool_val).lower()

        if len(zip_files_map) > 0:
            zip_file_path = Utils.generate_zip_file(zip_files_map,
                                                    minify_resources)
            configs[Constants.EXPORT_CONFIG_NAME_PAYLOAD] = zip_file_path

        return configs