summaryrefslogtreecommitdiff
path: root/gen_people.py
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@mailbox.org>2025-12-05 22:07:23 +0100
committerAldrik Ramaekers <aldrik@mailbox.org>2025-12-05 22:07:23 +0100
commit183ac879a0ee74d7bdfabb11c51a29e4a64f1e7f (patch)
tree91527955a60919f0d1252c5546f7f05d3e2ee733 /gen_people.py
Diffstat (limited to 'gen_people.py')
-rw-r--r--gen_people.py131
1 files changed, 131 insertions, 0 deletions
diff --git a/gen_people.py b/gen_people.py
new file mode 100644
index 00000000..9f98080f
--- /dev/null
+++ b/gen_people.py
@@ -0,0 +1,131 @@
+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 += '<div class="person"><h3>' + person.get('Name') + '</h3>'
+ html_content += '<p>'
+ for piece in story_pieces:
+ html_content += piece.get('Text')
+ if piece.get('Citation') != None:
+ sup = '⁰¹²³⁴⁵⁶⁷⁸⁹'[citation_count+1]
+ html_content += '<a class="source" title="' + piece.get('Citation').get('Text') + '">⁽' + sup + '⁾</a>'
+ citation_count+=1
+ html_content += ' '
+
+ html_content += '</p></div>'
+ 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 += '<div class="person">'
+ if birthdate.year == 1 and dieddate.year == 1:
+ html_content += '<a href="/people/'+str(parent)+'.html">' + parent_info.get('Name') + '</a>'
+ else:
+ html_content += '<a href="/people/'+str(parent)+'.html">' + parent_info.get('Name') + '</a> (' + (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 += '<br><a href="/people/'+str(parent_info.get('Id'))+'.html">' + parent_info.get('Name') + '</a>'
+ else:
+ html_content += '<br><a href="/people/'+str(parent_info.get('Id'))+'.html">' + parent_info.get('Name') + '</a> (' + (str(birthdate.year) if birthdate.year != 1 else '?') + ' - ' + (str(dieddate.year) if dieddate.year != 1 else '?') + ')'
+
+ html_content += '<div class="h-line"></div>'
+ html_content += '</div>'
+ 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 = '<div class="line"></div>'
+ 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 += '<div class="h-line"></div>'
+ 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 += '<li><a href="/people/'+str(child)+'.html">' + child_info.get('Name') + '</a></li>'
+ else:
+ html_content += '<li><a href="/people/'+str(child)+'.html">' + child_info.get('Name') + '</a> (' + (str(birthdate.year) if birthdate.year != 1 else '?') + ' - ' + (str(dieddate.year) if dieddate.year != 1 else '?') + ')</li>'
+
+ 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}}', '<div class="person"></div>')
+
+ 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}}', '<div class="person"></div>') # 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()