def build_html_use_template(html_str, input_path, project_path, file_path, data): """ 生成线上html代码 :param html_str: html模板 :param project_path: 项目路径 :return: """ # 处理路径 file_path = os.path.dirname(file_path) input_path = os.path.dirname(input_path) # 编译代码 html_str = handle_html_code(html_str, input_path, project_path) # 编译组件 html_str = compile_assembly(html_str, project_path, input_path) # hash静态资源文件 html_str = hash_css_and_js_version(html_str, file_path, data, project_path) # 分离静态资源 html_str = separate_style(html_str, data) html_str = separate_javascript(html_str, data) # 删除重复的css和js html_str = remove_repeat_link_and_script(html_str) # 压缩html html_str = handle_html(html_str) # 为生成的代码打标示 html_str = tip.html_tip + html_str return html_str
def render_html_use_template(html_str, output_file_path, project_path, input_file_path): """ 渲染html代码 :param html_str: html模板 :param output_file_path: 输出的html文件路径 :param project_path: 项目路径 :return: """ # 处理路径 output_path = os.path.dirname(output_file_path) input_path = os.path.dirname(input_file_path) html_str = handle_html_code(html_str, input_path, project_path) html_str = handle_css_and_js_version(html_str, output_path, project_path) # 编译组件 html_str = compile_assembly(html_str, project_path, input_path) # 删除重复的css和js html_str = remove_repeat_link_and_script(html_str) html_str = handle_html(html_str) html_str = tip.html_tip + html_str return html_str
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