From 362dd6ee09d7901b748d23e793cbd63a24877e57 Mon Sep 17 00:00:00 2001 From: syntaxaire Date: Wed, 10 Jul 2019 22:02:57 -0400 Subject: [PATCH] Repair of more invalid XML to allow for newlines in description output Minor config formatting --- config.yml | 25 +++++++++++++------------ qud_object_tree.py | 27 +++++++++++++++++++++------ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/config.yml b/config.yml index ac157d5..d279a70 100644 --- a/config.yml +++ b/config.yml @@ -4,21 +4,19 @@ Interface: # Interface options Initial expansion targets: # These object category IDs will be expanded when the tree is initialized: - [Food, MeleeWeapon, MissileWeapon, Armor, Shield, Token, LightSource, Tool, Tonic, Trinket, Energy Cell, Security Card, Barathrumite, SapientMutatedFlower] + [Food, MeleeWeapon, MissileWeapon, Armor, Shield, Token, LightSource, Tool, Tonic, Trinket, + Energy Cell, Security Card, Barathrumite, SapientMutatedFlower] Templates: # Options controlling the generation of wiki infobox templates Fields: # The fields that (if applicable) will be generated for each item, in this order: # (title comes first, but is not listed here) - [image, lv, pv, maxpv, vibro, pvpowered, hp, av, dv, - ma, tohit, ammo, accuracy, shots, maxammo, maxvol, - liquidgen, liquidtype, maxcharge, charge, weight, commerce, - complexity, tier, bits, canbuild, skill, colorstr, renderstr, id, - bookid, lightradius, hunger, thirst, twohanded, metal, - lightprojectile, extra, strength, agility, toughness, - intelligence, willpower, ego, acid, electric, cold, heat, - desc] + [image, lv, pv, maxpv, vibro, pvpowered, hp, av, dv, ma, tohit, ammo, accuracy, shots, maxammo, + maxvol, liquidgen, liquidtype, maxcharge, charge, weight, commerce, complexity, tier, bits, + canbuild, skill, colorstr, renderstr, id, bookid, lightradius, hunger, thirst, twohanded, + metal, lightprojectile, extra, strength, agility, toughness, intelligence, willpower, ego, + acid, electric, cold, heat, desc] Image overrides: # If the image for an object was uploaded under a different name than in the # game data, override it here: @@ -26,6 +24,8 @@ Templates: Wiki: Categories: + # Each wiki category is followed by a list of nodes in the inheritance tree whose child objects + # should be in that category. Objects may also be explicitly specified. Items: [Item] Weapons: [MeleeWeapon, MissileWeapon] Equipment: [Armor, Shield] @@ -33,6 +33,7 @@ Wiki: Thrown weapons: [BaseThrownWeapon] Plants: [Plant] Creatures: [Creature] - Characters: [Phinae Hoshaiah, Barathrumite, Argyve, ElderBob, Mehmet, Warden Ualraig, Warden Esthers, - Eskhind, Meyehind, Liihart, Isahind, Neelahind, Keh, Kesehind, Angohind, Lulihart, AgateSeveranceStar, - Mayor Nuntu, Oboroqoru, Asphodel, Yurl, Euclid] \ No newline at end of file + Characters: [Phinae Hoshaiah, Barathrumite, Argyve, ElderBob, Mehmet, Warden Ualraig, + Warden Esthers, Eskhind, Meyehind, Liihart, Isahind, Neelahind, Keh, Kesehind, + Angohind, Lulihart, AgateSeveranceStar, Mayor Nuntu, Oboroqoru, Asphodel, Yurl, + Euclid] \ No newline at end of file diff --git a/qud_object_tree.py b/qud_object_tree.py index 72946b2..d7be207 100644 --- a/qud_object_tree.py +++ b/qud_object_tree.py @@ -11,13 +11,28 @@ def load(path): """Load ObjectBlueprints.xml from the specified filepath and return a reference to the root.""" - # Do some repair of invalid XML - pattern = re.compile("()|( )") - repaired = [] + # Do some repair of invalid XML: + # First, delete some invalid characters + pat_invalid = re.compile("()|( )") with open(path, 'r', encoding='utf-8') as f: - for line in f: - repaired.append(pattern.sub('', line)) - raw = et.fromstringlist(repaired) + contents = f.read() + contents = re.sub(pat_invalid, '', contents) + + # Second, replace line breaks inside attributes with proper XML line breaks + # ^\s*<[^!][^>]*\n[^>]*> + pat_linebreaks = r"^\s*<[^!][^>]*\n.*?>" + match = re.search(pat_linebreaks, contents, re.MULTILINE) + while match: + before = match.string[:match.start()] + fixed = match.string[match.start():match.end()].replace('\n', ' ') + after = match.string[match.end():] + contents = before + fixed + after + match = re.search(pat_linebreaks, contents, re.MULTILINE) + # Uncomment to have a diff-able file to double check XML repairs. + # with open('test_output.xml', 'w', encoding='utf-8') as f: + # f.write(contents) + + raw = et.fromstring(contents) # Build the Qud object hierarchy from the XML data for element in raw: