import json import sys import random def parse_json(json_data : object) -> str: """Parse the given json adding it to the HTML file""" body = "" i=1 for key, value in json_data.items(): body += """\t\t
\n
\n""" i=i+1 for key1, value1 in value.items(): if(type(value1)==list): body+=parse_list(value1) if((type(value1)==dict)): body+=parse_dict(value1) body+="\t\t\t
\n" return body def parse_dict(json_dict: dict) -> str: """Parse the given dict from the given json adding it to the HTML file""" dict_text="" for key, value in json_dict.items(): n=random.randint(0,999999) infos = [] for info in value["infos"]: if info.startswith("http"): infos.append(f"{info}
\n") else: infos.append(str(info) + "
\n") dict_text += f'\t\t
\n' dict_text += '' + "".join(infos) + '' dict_text += f'
\n' if value["lines"]: dict_text+="\n" + parse_list(value["lines"]) + "\n" if value["sections"]: dict_text+=parse_dict(value["sections"]) return dict_text def parse_list(json_list: list) -> str: """Parse the given list from the given json adding it to the HTML file""" color_text="" color_class="" for i in json_list: if "═══" not in i['clean_text']: if(i['clean_text']): color_text+= "
"+ replacement + "") #class=\""+ color_class + "\" "+ " if "═╣" in text: text=text.replace("═╣","
  • ") text+="
  • " color_text+= "" + color_class + " " color_text +="no_color\" >"+ text + "
    \n" return color_text + "\t\t\t
    \n" def main(): with open(JSON_PATH) as JSON_file: json_data = json.load(JSON_file) html = HTML_HEADER html += HTML_INIT_BODY html += parse_json(json_data) html += HTML_END with open(HTML_PATH, 'w') as f: f.write(html) HTML_HEADER = """ """ HTML_END = """ """ HTML_INIT_BODY = body = """

    """ # Start execution if __name__ == "__main__": try: JSON_PATH = sys.argv[1] HTML_PATH = sys.argv[2] except IndexError as err: print("Error: Please pass the peas.json file and the path to save the html\npeas2html.py ") sys.exit(1) main()