def twine_v2(): with open(f"{args.filename}", "r") as f: dialogue_adv = [] page = f.read() soup = BeautifulSoup(page, "html.parser") items = soup.findAll('tw-passagedata') for item in items: dialogue_map[item["name"]] = f"line_{item['pid']}" for item in items: new_dialogue_row = Dialogue_Adv() new_dialogue_chunk = Dialogue() for line in item.string.splitlines(): # handle responses if line.startswith("[["): row_map = line.lstrip("[").rstrip(" ").rstrip("]") row_map = row_map.split("][") new_response = Response() new_response.response_text = row_map[0] new_postrouting = Postrouting() new_postrouting.target_dialogue = dialogue_map[row_map[0]] new_response.post_routing.append(new_postrouting) # TODO: handle post-response routing requirements new_dialogue_row.Response.append(new_response) # set speaker elif line.startswith("@"): line = line.lstrip("@") new_dialogue_chunk.Speaker = line # handle effects elif line.startswith("##"): parse_effects(line, new_dialogue_row.Effects) # handle twine user variables # TODO: rewrite this elif line.startswith("<<"): parse_inline_set(line, new_dialogue_row.Effects.user_vars) # handle dialogue text chunks # TODO: continue rewriting this elif line.startswith("--") or line.startswith("++"): if line.startswith("++"): new_dialogue_chunk.append = True new_dialogue_row.Dialogue_Text.append(new_dialogue_chunk) new_dialogue_chunk = Dialogue() else: new_dialogue_chunk.text += line # TODO: rewrite this new_dialogue_chunk.text = new_dialogue_chunk.text.rstrip("\n") new_dialogue_row.Dialogue_Text.append(new_dialogue_chunk) new_dialogue_row.Name = f"line_{item['pid']}" dialogue_adv.append(new_dialogue_row) f.close with open(f"{args.filename}.json", "w") as of: output_as_list_of_dicts = [] for row in dialogue_adv: add_dict = row.dict() output_as_list_of_dicts.append(add_dict) of.write(json.dumps(output_as_list_of_dicts, indent=4)) print(f"Saved to {args.filename}.json")
import json from dialogue import Dialogue_Adv, Dialogue, Dialogue_Options, Reaction, Response, Postrouting, Gate, Quest, Stats_Effect_Array, Stats_Effect new_dialogue_adv = Dialogue_Adv() new_dialogue_chunk = Dialogue() new_dialogue_chunk.Speaker = "Kristin" new_dialogue_chunk.text = "Hey, how's it going?" new_dialogue_adv.Dialogue.append(new_dialogue_chunk) new_dialogue_chunk.Speaker = "Kristin" new_dialogue_chunk.text = "Virus times, hey?" new_dialogue_adv.Dialogue.append(new_dialogue_chunk) new_dialogue_chunk.Speaker = "Kristin" new_dialogue_chunk.text = "F****d." new_dialogue_adv.Dialogue.append(new_dialogue_chunk) new_response = Response() new_dialogue_adv.Response.append(new_response) print(f"{new_dialogue_adv.dict()}")