import re, fileinput, sys import utilities as u import description_helper as dsh import gradio as gr # HTML section headers actions_header = """

Actions

""" cantrips_header = """

Cantrips

""" spells_header = """

Known Spells

""" spell_slot_header = """

Spell Slots

""" legendary_actions_header = """

Legendary Actions

""" # Assigning strings to variables for replacing location of dependencies for the webpage to local static folders # Path is ../../ for the html files location in output/dated_folder/ break_tag = "
" def build_html_base( mon_name, mon_size, mon_type, mon_subtype, mon_alignment, mon_armor_class, mon_hp, mon_hit_dice, mon_speed, mon_abilities, mon_saving_throws, mon_skills, mon_damage_resistance, mon_senses, mon_languages, mon_challenge_rating, mon_xp, mon_actions, mon_description, mon_image_path, mon_cantrips = False, mon_spells = False, mon_spell_slots = False, mon_legendary_actions = False ) : # Combine the properties that will go on a single line if mon_subtype != "" : mon_properties = f"{mon_size}, {mon_type}, {mon_subtype}, {mon_alignment}" else: mon_properties = f"{mon_size}, {mon_type}, {mon_alignment}" mon_abilities = parse_abilities_from_text(mon_abilities) # Template for the page html_base = f""" {mon_name}

{mon_name}

{mon_properties}

image

{mon_description}

Armor Class : {mon_armor_class} Hit Points: {mon_hp} Hit Dice : {mon_hit_dice} Speed: {mon_speed}

STR DEX CON INT WIS CHA
{mon_abilities[0]} {mon_abilities[1]} {mon_abilities[2]} {mon_abilities[3]} {mon_abilities[4]} {mon_abilities[5]}

Saving Throws : {mon_saving_throws}
Skills : {mon_skills}
Resistances : {mon_damage_resistance}
Senses : {mon_senses}
Languages : {mon_languages}
Challenge Rating : {mon_challenge_rating} ({mon_xp})""" if mon_actions : print("Actions : True") parsed_actions = parse_actions_from_text(mon_actions) html_file_as_text = f"""{html_base}
{actions_header} {''.join(parsed_actions)}""" else: print("Actions : False") html_file_as_text = html_base if mon_cantrips: print(mon_cantrips) mon_cantrips = mon_cantrips.replace("Cantrips", '') mon_cantrips = parse_cantrips_from_text(mon_cantrips) html_file_as_text = html_file_as_text + cantrips_header + mon_cantrips if mon_spells : print(mon_spells) mon_spells = mon_spells.replace("Known Spells",'') mon_spells = parse_spells_from_text(mon_spells) html_file_as_text = html_file_as_text + spells_header + mon_spells if mon_spell_slots: print(mon_spell_slots) mon_spell_slots = mon_spell_slots.replace("Spell Slots", '') mon_spell_slots = parse_spell_slots_from_text(mon_spell_slots) html_file_as_text = html_file_as_text + spell_slot_header + mon_spell_slots else: print("Spells : False") if mon_legendary_actions: print("Legendary Actions : True") mon_legendary_actions = mon_legendary_actions.replace("Legendary Actions \n\n", '') mon_legendary_actions = parse_legendary_action_from_text(mon_legendary_actions) html_file_as_text = html_file_as_text +legendary_actions_header + mon_legendary_actions else: print("Legendary Actions : False") # Open a file path that will receive the processed text u.gen_file_name(mon_name) mon_file_path = f"{u.file_name_list[0]}/{u.file_name_list[1]}.html" with open(mon_file_path, 'w') as clean_html: clean_html.write(html_file_as_text) clean_html.close() # Clear link list and append with new entries del u.link_list[:] u.link_list.append(u.file_name_list[0]+'/' + u.file_name_list[1] +'.html') u.link_list.append(mon_type) #Passing back a file path that Gradio can access and is local return mon_file_path def parse_actions_from_text(edited_text): html_content = '
' actions = [] action_entries = edited_text.strip().split('\n\n') print(action_entries) for entry in action_entries: parts = entry.split(';') action_dict = { "name": parts[0].split(": ")[1].strip(), "desc": parts[1].split("Description: ")[1].strip() } actions.append(action_dict) for action in actions: html_content += f"
{action['name']} :
‘{action['desc']}
" html_content += "
" html_content=html_content.rstrip('br') html_content += '
' return html_content def parse_abilities_from_text(abilities): abilities_list = [] ability_entries = abilities.strip().split('\n') for entry in ability_entries: parts = entry.split(':') ability_value = parts[1] abilities_list.append(ability_value) return abilities_list def parse_cantrips_from_text(cantrips): html_content = '
' cantrips_list = [] cantrip_entries = cantrips.strip().split('\n\n') for entry in cantrip_entries: parts = entry.split(';') cantrip_dict = { "name": parts[0], "desc": parts[1].split("Description: ")[1].strip() } cantrips_list.append(cantrip_dict) for cantrip in cantrips_list: html_content += f"
{cantrip['name']} :
‘{cantrip['desc']}’
" html_content += "
" html_content=html_content.rstrip('br') html_content += '
' return html_content def parse_spells_from_text(spells): html_content = '
' spells_list = [] spell_entries = spells.strip().split('\n\n') for entry in spell_entries: print(f"Spell entry = {entry}") parts = entry.split(';') spell_name_part = parts[0] level_desc_part = parts[1] # Extract the spell's name (before 'level:') name = spell_name_part.strip() # Further split level and description level_part = level_desc_part.split(", Description:")[0].strip() description_part = level_desc_part.split(", Description:")[1].strip() if ", Description:" in level_desc_part else "" # Extract the level (assuming it follows 'Level: ' directly) level = level_part.replace("Level: ", "").strip() print(f"Level = {level}") # Assemble the dictionary for this spell spell_dict = { "name": name, "level": level, "desc": description_part } spells_list.append(spell_dict) for spell in spells_list: html_content += f"
{spell['name']} :
‘Level : {spell['level']} {spell['desc']}’
" html_content += "
" html_content=html_content.rstrip('br') html_content += '
' return html_content def parse_spell_slots_from_text(spell_slots): html_content = '
' spell_slots_list = [] spell_slot_entries = spell_slots.strip().split('\n\n') for entry in spell_slot_entries: if '0' not in entry: parts = entry.split(':') spell_slot_dict = { "level": parts[0], "number": parts[1] } spell_slots_list.append(spell_slot_dict) for spell_slot in spell_slots_list: html_content += f"
{spell_slot['level']}:
‘{spell_slot['number']}’
" html_content += "
" html_content=html_content.rstrip(' br') html_content += '
' return html_content def parse_legendary_action_from_text(legendary_actions): html_content = '
' parts = legendary_actions.split('\n\n') description = parts[0].strip() description = description + "
" print(f"Description : {description}") actions_text = parts[1:] actions = ';'.join(actions_text) actions = actions.split(';') print(f"actions : {actions}") legendary_actions_dict = { "description": description, "actions":[] } html_content += description # Process each action for action in actions: print(f"action to be parsed: {action}") action_split = action.split(':') print(f"Split Action : {action_split}") legendary_actions_dict['actions'].append({ "name": action_split[0], "desc": action_split[1] }) for action in legendary_actions_dict['actions']: print(action) html_content += f"
{action['name']}:
‘{action['desc']}’
" html_content += "
" html_content=html_content.rstrip(' br') html_content += '
' return html_content