import json
import datetime
# Load JSON data from a file
def load_json(file_path):
with open(file_path, 'r', encoding='utf-8') as json_file:
return json.load(json_file)
def get_person_by_id(data, fid):
for person in data:
if person.get('Id') == fid:
return person
return None
def get_person_info_html(person):
citation_count = 0
html_content = ''
story_pieces = person.get('Story').get('StoryPieces')
html_content += '
' + person.get('Name') + '
'
html_content += '
'
for piece in story_pieces:
html_content += piece.get('Text')
if piece.get('Citation') != None:
sup = '⁰¹²³⁴⁵⁶⁷⁸⁹'[citation_count+1]
html_content += '⁽' + sup + '⁾'
citation_count+=1
html_content += ' '
html_content += '
'
return html_content
def get_parent_info_html(data, parent):
html_content = ''
parent_info = get_person_by_id(data, parent)
if parent_info == None:
return ''
birthdate = datetime.datetime.strptime(parent_info.get('Birthday'), "%Y-%m-%dT%H:%M:%S")
dieddate = datetime.datetime.strptime(parent_info.get('DiedDay'), "%Y-%m-%dT%H:%M:%S")
html_content += ''
if birthdate.year == 1 and dieddate.year == 1:
html_content += '
' + parent_info.get('Name') + ''
else:
html_content += '
' + parent_info.get('Name') + ' (' + (str(birthdate.year) if birthdate.year != 1 else '?') + ' - ' + (str(dieddate.year) if dieddate.year != 1 else '?') + ')'
if parent_info.get('LastPartnerId') != 0:
parent_info = get_person_by_id(data, parent_info.get('LastPartnerId'))
birthdate = datetime.datetime.strptime(parent_info.get('Birthday'), "%Y-%m-%dT%H:%M:%S")
dieddate = datetime.datetime.strptime(parent_info.get('DiedDay'), "%Y-%m-%dT%H:%M:%S")
if birthdate.year == 1 and dieddate.year == 1:
html_content += '
' + parent_info.get('Name') + ''
else:
html_content += '
' + parent_info.get('Name') + ' (' + (str(birthdate.year) if birthdate.year != 1 else '?') + ' - ' + (str(dieddate.year) if dieddate.year != 1 else '?') + ')'
html_content += '
'
html_content += '
'
return html_content
def generate_html(data, output_file):
for person in data:
html_template = ''
with open('templates/person_template.html', 'r') as file:
html_template = file.read()
## Fill person or couple
html_template = html_template.replace('{{PERSON_INFO1}}', get_person_info_html(person))
if person.get('LastPartnerId') != 0:
person_html = ''
person_html += get_person_info_html(get_person_by_id(data, person.get('LastPartnerId')))
html_template = html_template.replace('{{PERSON_INFO2}}', person_html)
else:
html_template = html_template.replace('{{PERSON_INFO2}}', '')
## Fill children
html_content = ''
if len(person.get('ChildrenIds')) > 0:
html_content += ''
for child in person.get('ChildrenIds'):
child_info = get_person_by_id(data, child)
if child_info == None:
continue
birthdate = datetime.datetime.strptime(child_info.get('Birthday'), "%Y-%m-%dT%H:%M:%S")
dieddate = datetime.datetime.strptime(child_info.get('DiedDay'), "%Y-%m-%dT%H:%M:%S")
if birthdate.year == 1 and dieddate.year == 1:
html_content += '' + child_info.get('Name') + ''
else:
html_content += '' + child_info.get('Name') + ' (' + (str(birthdate.year) if birthdate.year != 1 else '?') + ' - ' + (str(dieddate.year) if dieddate.year != 1 else '?') + ')'
html_template = html_template.replace('{{CHILD_LIST}}', html_content)
## Fill parents
if person.get('ParentId') != 0:
html_content = get_parent_info_html(data, person.get('ParentId'))
html_template = html_template.replace('{{PARENT1}}', html_content)
else:
html_template = html_template.replace('{{PARENT1}}', '')
if person.get('LastPartnerId') != 0:
partner = get_person_by_id(data, person.get('LastPartnerId'))
if partner.get('ParentId') != 0:
html_content = get_parent_info_html(data, partner.get('ParentId'))
html_template = html_template.replace('{{PARENT2}}', html_content)
else:
html_template = html_template.replace('{{PARENT2}}', '') # partner has no parent, make empty block so it aligns.
else:
html_template = html_template.replace('{{PARENT2}}', '')
with open(output_file + str(person.get('Id')) + '.html', 'w', encoding='utf-8') as f:
f.write(html_template)
def main():
json_file_path = 'list.json'
output_html_file = 'people/'
# Load JSON data directly into an array
json_data = load_json(json_file_path)
# Generate HTML from the loaded data
generate_html(json_data, output_html_file)
if __name__ == '__main__':
main()