1+ import html
2+ import itertools
13import json
24import sys
3- import random
45
6+ _section_ids = itertools .count (1 )
57
6- def parse_json (json_data : object ) -> str :
8+
9+ def parse_json (json_data : dict ) -> str :
710 """Parse the given json adding it to the HTML file"""
8-
11+
912 body = ""
10- i = 1
13+ i = 1
1114 for key , value in json_data .items ():
12- body += """\t \t <button type="button" class="btn" data-toggle="collapse" data-target="#demo""" + str (i ) + "\" ><b>" + key + """</button></b><br>\n
13- <div id="demo""" + str (i )+ """\" class="collapse">\n """
14- i = i + 1
15+ body += (
16+ '\t \t <button type="button" class="btn" data-toggle="collapse" data-target="#demo'
17+ + str (i )
18+ + '"><b>'
19+ + html .escape (key )
20+ + "</b></button><br>\n "
21+ f' <div id="demo{ i } " class="collapse">\n '
22+ )
23+ i = i + 1
1524 for key1 , value1 in value .items ():
16-
17- if ( type (value1 )== list ) :
18- body += parse_list (value1 )
19-
20- if (( type (value1 )== dict )) :
21- body += parse_dict (value1 )
22- body += "\t \t \t </div>\n "
23-
25+
26+ if type (value1 ) == list :
27+ body += parse_list (value1 )
28+
29+ if type (value1 ) == dict :
30+ body += parse_dict (value1 )
31+ body += "\t \t \t </div>\n "
32+
2433 return body
2534
2635
2736def parse_dict (json_dict : dict ) -> str :
2837 """Parse the given dict from the given json adding it to the HTML file"""
2938
30- dict_text = ""
39+ dict_text = ""
3140 for key , value in json_dict .items ():
32- n = random . randint ( 0 , 999999 )
41+ n = next ( _section_ids )
3342 infos = []
3443 for info in value ["infos" ]:
3544 if info .startswith ("http" ):
36- infos .append (f"<a href='{ info } '>{ info } </a><br>\n " )
45+ infos .append (f"<a href='{ html . escape ( info ) } '>{ html . escape ( info ) } </a><br>\n " )
3746 else :
38- infos .append (str (info ) + "<br>\n " )
39-
40- dict_text += f'\t \t <button type="button" class="btn1" data-toggle="collapse" data-target="#lines{ n } ">{ key } </button><br>\n '
41- dict_text += '<i>' + "" .join (infos ) + '</i>'
47+ infos .append (html .escape (str (info )) + "<br>\n " )
48+
49+ dict_text += (
50+ f'\t \t <button type="button" class="btn1" data-toggle="collapse" data-target="#lines{ n } ">'
51+ + html .escape (key )
52+ + "</button><br>\n "
53+ )
54+ dict_text += "<i>" + "" .join (infos ) + "</i>"
4255 dict_text += f'<div id="lines{ n } " class="collapse1">\n '
43-
56+
4457 if value ["lines" ]:
45- dict_text += "\n " + parse_list (value ["lines" ]) + "\n "
58+ dict_text += "\n " + parse_list (value ["lines" ]) + "\n "
4659
4760 if value ["sections" ]:
48- dict_text += parse_dict (value ["sections" ])
49-
61+ dict_text += parse_dict (value ["sections" ])
62+
63+ dict_text += "\t \t \t </div>\n "
64+
5065 return dict_text
5166
5267
5368def parse_list (json_list : list ) -> str :
5469 """Parse the given list from the given json adding it to the HTML file"""
55- color_text = ""
56- color_class = ""
70+ color_text = ""
71+ color_class = ""
5772
5873 for i in json_list :
59- if "═══" not in i ['clean_text' ]:
60- if (i ['clean_text' ]):
61- color_text += "<div class = \" "
62- text = str (i ['clean_text' ])
63- for color in i ['colors' ]:
64- if (color == 'BLUE' ):
65- style = "#0000FF"
66- color_class = "blue"
67- if (color == 'LIGHT_GREY' ):
68- style = "#adadad"
69- color_class = "light_grey"
70- if (color == 'REDYELLOW' ):
71- style = "#FF0000; background-color: #FFFF00;"
72- color_class = "redyellow"
73- if (color == 'RED' ):
74- style = "#FF0000"
75- color_class = "red"
76- if (color == 'GREEN' ):
77- style = "#008000"
78- color_class = "green"
79- if (color == 'MAGENTA' ):
80- style = "#FF00FF"
81- color_class = "magenta"
82- if (color == 'YELLOW' ):
83- style = "#FFFF00"
84- color_class = "yellow"
85- if (color == 'DARKGREY' ):
86- style = "#A9A9A9"
87- color_class = "darkgrey"
88- if (color == 'CYAN' ):
89- style = "#00FFFF"
90- color_class = "cyan"
91- for replacement in i ['colors' ][color ]:
92- text = text .replace (replacement ," <b style=\" color:" + style + "\" >" + replacement + "</b>" )
93- #class=\""+ color_class + "\" "+ "
94- if "═╣" in text :
95- text = text .replace ("═╣" ,"<li>" )
96- text += "</li>"
97- color_text += "" + color_class + " "
98- color_text += "no_color\" >" + text + "<br></div>\n "
99- return color_text + "\t \t \t </div>\n "
74+ if "═══" in i ["clean_text" ]:
75+ continue
76+ if not i ["clean_text" ]:
77+ continue
78+
79+ color_text += '<div class = "'
80+ text = html .escape (str (i ["clean_text" ]))
81+ style = ""
82+ for color in i ["colors" ]:
83+ if color == "BLUE" :
84+ style = "#0000FF"
85+ color_class = "blue"
86+ if color == "LIGHT_GREY" :
87+ style = "#adadad"
88+ color_class = "light_grey"
89+ if color == "REDYELLOW" :
90+ style = "#FF0000; background-color: #FFFF00;"
91+ color_class = "redyellow"
92+ if color == "RED" :
93+ style = "#FF0000"
94+ color_class = "red"
95+ if color == "GREEN" :
96+ style = "#008000"
97+ color_class = "green"
98+ if color == "MAGENTA" :
99+ style = "#FF00FF"
100+ color_class = "magenta"
101+ if color == "YELLOW" :
102+ style = "#FFFF00"
103+ color_class = "yellow"
104+ if color == "DARKGREY" :
105+ style = "#A9A9A9"
106+ color_class = "darkgrey"
107+ if color == "CYAN" :
108+ style = "#00FFFF"
109+ color_class = "cyan"
110+ for replacement in i ["colors" ][color ]:
111+ escaped = html .escape (replacement )
112+ text = text .replace (
113+ escaped , f' <b style="color:{ style } ">{ escaped } </b>'
114+ )
115+ color_text += "" + color_class + " "
116+
117+ if "═╣" in text :
118+ text = text .replace ("═╣" , "<li>" ) + "</li>"
119+ color_text += 'no_color" >' + text + "<br></div>\n "
120+ return color_text
100121
101122
102123def main ():
124+ global _section_ids
125+ _section_ids = itertools .count (1 )
103126 with open (JSON_PATH ) as JSON_file :
104127 json_data = json .load (JSON_file )
105128 html = HTML_HEADER
106129 html += HTML_INIT_BODY
107130 html += parse_json (json_data )
108131 html += HTML_END
109-
110- with open (HTML_PATH , 'w' ) as f :
132+
133+ with open (HTML_PATH , "w" ) as f :
111134 f .write (html )
112135
113136
@@ -341,7 +364,7 @@ def main():
341364 JSON_PATH = sys .argv [1 ]
342365 HTML_PATH = sys .argv [2 ]
343366 except IndexError as err :
344- print ("Error: Please pass the peas.json file and the path to save the html\n peas2html .py <json_file.json> <HTML_file.html>" )
367+ print ("Error: Please pass the peas.json file and the path to save the html\n json2html .py <json_file.json> <HTML_file.html>" )
345368 sys .exit (1 )
346369
347370 main ()
0 commit comments