diff --git a/obj2html/obj2html.py b/obj2html/obj2html.py index 4ff34eb..d298d2b 100644 --- a/obj2html/obj2html.py +++ b/obj2html/obj2html.py @@ -1,8 +1,22 @@ import os import json +import re +import copy TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'index.html') +def simple_mustache(input_string, value_dict): + output_string = copy.deepcopy(input_string) + mustache_variables = re.findall(r"{{.*}}",output_string) + for v_text in mustache_variables: + v_name = v_text[2:-2] + if '.' in v_name: + dict_name, dict_subname = v_name.split(".") + v = value_dict[dict_name][dict_subname] + else: + v = value_dict[v_name] + output_string = output_string.replace(v_text, str(v)) + return output_string def obj2html( obj_path, output_html_path=None, @@ -18,8 +32,13 @@ def obj2html( with open(TEMPLATE_PATH, "r") as f: html_template = f.read() - html_string = html_template.replace("{{obj_3d}}", js_string) - html_string = html_string.replace("{{camera.fov}}", str(camera['fov'])) + + data_dict = { + "obj_3d": js_string, + "camera": camera, + } + + html_string = simple_mustache(html_template, data_dict) if output_html_path != None: with open(output_html_path, "w") as f: