Exemple #1
0
def separate_javascript(html_str, data):
    """
    分离js
    :param html_str: html代码
    :return:
    """
    js_data = ''
    soup = BeautifulSoup(html_str, "html.parser")

    scripts = soup.find_all("script")
    for obj in scripts:
        if obj.string and obj.string.strip():
            js_data += ('\n' + obj.string)
            obj.string = ''

    if not js_data:
        return html_str

    # 压缩js
    js_data = handle_javascript(js_data, False, False)

    # 写入分离出的js文件
    version = hashlib.md5(js_data.encode("UTF-8")).hexdigest()
    file_path = data['tools_obtain_static_path'] + '/' + str_to_file_path(
        version, 8) + '.min.js'

    tools.output_file(file_path, js_data)

    link_str = '<script src="%s"></script>' % (
        data['tools_obtain_static_path_prefix'] + '/' +
        str_to_file_path(version, 8) + '.min.js')
    soup.find('body').append(BeautifulSoup(link_str, "html.parser"))

    return str(soup)
Exemple #2
0
def get_after_merge_file_str(dir_path, is_first=True, file_type=r'.*', minify_type=None):
    """
    获取合并后的字符串
    :param dir_path: 文件夹路径
    :param is_first: 是不是第一次遍历,用于压缩
    :param file_type: 用于搜索文件
    :param minify_type: 使用哪一种压缩(html,css,js)
    :return:
    """
    now_str = ''
    if os.path.isdir(dir_path):
        dirs = os.listdir(dir_path)
        for obj in dirs:
            now_str = '\n'.join([now_str, get_after_merge_file_str(dir_path + '/' + obj, is_first=False, file_type=file_type, minify_type=minify_type)])
    else:
        if re.search(file_type, dir_path):
            with open(dir_path, 'r') as fp:
                now_str = '\n'.join([now_str, fp.read()])

    if is_first and minify_type:
        if minify_type == 'html':
            now_str = static_minify.handle_html(now_str)
        elif minify_type == 'css':
            now_str = static_minify.handle_css(now_str)
        else:
            now_str = static_minify.handle_javascript(now_str)

    return now_str
Exemple #3
0
def separate_javascript(html_str, data):
    """
    分离js
    :param html_str: html代码
    :return:
    """
    js_data = ''
    soup = BeautifulSoup(html_str, "html.parser")

    scripts = soup.find_all("script")
    for obj in scripts:
        if obj.string and obj.string.strip():
            js_data += ('\n' + obj.string)
            obj.string = ''

    if not js_data:
        return html_str

    # 压缩js
    js_data = handle_javascript(js_data, False, False)

    # 写入分离出的js文件
    version = hashlib.md5(js_data.encode("UTF-8")).hexdigest()
    file_path = data['tools_obtain_static_path'] + '/' + str_to_file_path(version, 8) + '.min.js'

    tools.output_file(file_path, js_data)

    link_str = '<script src="%s"></script>' % (data['tools_obtain_static_path_prefix'] + '/' + str_to_file_path(version, 8) + '.min.js')
    soup.find('body').append(BeautifulSoup(link_str, "html.parser"))

    return str(soup)
def get_component_js_str(dir_path):
    """
    获取打包好的组件js文件
    :param dir_path: 组件代码文件夹路径
    :return: str, code
    """
    html = get_after_merge_file_str(dir_path,
                                    file_type=r'\.html$',
                                    minify_type='html')
    css = get_after_merge_file_str(dir_path,
                                   file_type=r'\.css$',
                                   minify_type='css')
    js = get_after_merge_file_str(dir_path, file_type=r'\.js$')

    html_js_str, html_js_name = build_js_data(html, 'tools_html')
    css_js_str, css_js_name = build_js_data("<style>" + css + "</style>",
                                            'tools_css')

    # 读取组件配置
    if not os.path.exists(dir_path + '/c.yaml'):
        return None, -1
    with open(dir_path + '/c.yaml', 'r') as fp:
        fp_data = fp.read()
        component_config = yaml.load(fp_data)

    component_js = """
        var component_%s_func = function(){
            %s
            %s
            %s
            this.build_component = function(dom_id, data, func){
              document.getElementById(dom_id).innerHTML = tools_css + ' ' + tools_html;
              component_init(data, func);
            };

            return this;
        };
        this.component_%s = component_%s_func();
    """ % (component_config['name'], css_js_str, html_js_str, js,
           component_config['name'], component_config['name'])
    about_str = tip.js_tip
    return about_str + handle_js_anonymous_function(
        handle_javascript(component_js)), 0
def get_component_js_str(dir_path):
    """
    获取打包好的组件js文件
    :param dir_path: 组件代码文件夹路径
    :return: str, code
    """
    html = get_after_merge_file_str(dir_path, file_type=r'\.html$', minify_type='html')
    css = get_after_merge_file_str(dir_path, file_type=r'\.css$', minify_type='css')
    js = get_after_merge_file_str(dir_path, file_type=r'\.js$')

    html_js_str, html_js_name = build_js_data(html, 'tools_html')
    css_js_str, css_js_name = build_js_data("<style>"+css+"</style>", 'tools_css')

    # 读取组件配置
    if not os.path.exists(dir_path+'/c.yaml'):
        return None, -1
    with open(dir_path+'/c.yaml', 'r') as fp:
        fp_data = fp.read()
        component_config = yaml.load(fp_data)

    component_js = """
        var component_%s_func = function(){
            %s
            %s
            %s
            this.build_component = function(dom_id, data, func){
              document.getElementById(dom_id).innerHTML = tools_css + ' ' + tools_html;
              component_init(data, func);
            };

            return this;
        };
        this.component_%s = component_%s_func();
    """ % (component_config['name'], css_js_str, html_js_str, js, component_config['name'], component_config['name'])
    about_str = tip.js_tip
    return about_str + handle_js_anonymous_function(handle_javascript(component_js)), 0