def parse_animal_companion(sb, book): ac = { 'type': 'animal_companion', 'source': book, 'name': filter_name(sb.name.strip()), 'sections': [] } text = [] for key, value in sb.keys: animal_companion_parse_function(key)(ac, value) for detail in sb.details: if detail.__class__ == StatBlockSection and detail.name.endswith( 'th-Level Advancement'): advancement = { 'level': detail.name, 'source': book, 'type': 'animal_companion', 'name': filter_name(ac['name']) } for key, value in detail.keys: animal_companion_parse_function(key)(advancement, value) ac['sections'].append(advancement) else: text.append(detail) if len(text) > 0: ac['text'] = ''.join(text) return ac
def parse_animal_companion(sb, book): ac = { 'type': 'animal_companion', 'subtype': 'base', 'source': book, 'name': filter_name(sb.name.strip()), 'sections': [] } text = [] for key, value in sb.keys: animal_companion_parse_function(key)(ac, value) for detail in sb.details: if detail.__class__ in (StatBlockSection, StatBlockHeading) and detail.name.endswith( 'th-Level Advancement'): advancement = { 'level': detail.name[:3], 'source': book, 'type': 'animal_companion', 'name': filter_name(ac['name']) } for key, value in detail.keys: animal_companion_parse_function(key)(advancement, value) advancement['subtype'] = 'advancement' ac['sections'].append(advancement) else: text.append(detail) if ac["name"].endswith('th-Level Advancement'): ac['level'] = ac['name'][:3] ac['subtype'] = "advancement" if len(text) > 0: collapse_text(ac, text) return ac
def parse_section(sb, book, no_sb=False, keys=True): section = { 'type': 'section', 'source': book, 'name': filter_name(sb.name.strip()) } text = [] sections = [] sectiontext = [] ns = None if keys: for key in sb.keys: text.append("<p class='stat-block-1'><b>%s </b>%s</p>" % key) for detail in sb.details: if isinstance(detail, StatBlockSection): sections.append(parse_stat_block(detail, book, no_sb=no_sb)) elif isinstance(detail, StatBlockHeading): sections.append(parse_stat_block(sb, book, no_sb=no_sb)) elif isinstance(detail, dict): if len(sectiontext) > 0: section['text'] = ''.join(sectiontext) sectiontext = [] ns = None sections.append(detail) elif detail.__class__ == Heading: if len(sectiontext) > 0: ns['text'] = ''.join(sectiontext) sectiontext = [] ns = { 'type': 'section', 'source': book, 'name': filter_name(detail.name) } sections.append(ns) else: if len(sections) == 0: if isinstance(detail, dict): text.append(detail) else: text.append(unicode(detail)) else: if not ns: ns = {'type': 'section', 'source': book} sections.append(ns) if isinstance(detail, dict) or isinstance(detail, Heading): sectiontext.append(detail) else: sectiontext.append(unicode(detail)) if len(text) > 0: collapse_text(section, text) if len(sectiontext) > 0: collapse_text(ns, sectiontext) if len(sections) > 0: section['sections'] = sections return section
def parse_item(sb, book): item = { 'type': 'item', 'source': book, 'name': filter_name(sb.name.strip()) } text = [] for key, value in sb.keys: item_parse_function(key, sb.name.strip())(item, value) for detail in sb.details: if isinstance( detail, StatBlockSection) and detail.name.startswith("Construction"): for key, value in detail.keys: item_parse_function(key, detail.name.strip())(item, value) elif isinstance(detail, StatBlockSection): sections = item.setdefault('sections', []) sections.append(parse_section(detail, book)) elif isinstance(detail, StatBlockHeading): sections.append(parse_stat_block(sb, book, no_sb=no_sb)) else: if isinstance(detail, dict) or isinstance(detail, Heading): text.append(detail) else: text.append(unicode(detail)) if len(text) > 0: collapse_text(item, text) return item
def parse_section(sb, book, no_sb=False, keys=True): section = {'type': 'section', 'source': book, 'name': filter_name(sb.name.strip())} text = [] sections = [] sectiontext = [] ns = None if keys: for key in sb.keys: text.append("<p class='stat-block-1'><b>%s </b>%s</p>" % key) for detail in sb.details: if isinstance(detail, StatBlockSection): sections.append(parse_stat_block(detail, book, no_sb=no_sb)) elif isinstance(detail, StatBlockHeading): sections.append(parse_stat_block(sb, book, no_sb=no_sb)) elif isinstance(detail, dict): if len(sectiontext) > 0: section['text'] = ''.join(sectiontext) sectiontext = [] ns = None sections.append(detail) elif detail.__class__ == Heading: if len(sectiontext) > 0: ns['text'] = ''.join(sectiontext) sectiontext = [] ns = {'type': 'section', 'source': book, 'name': filter_name(detail.name)} sections.append(ns) else: if len(sections) == 0: if isinstance(detail, dict): text.append(detail) else: text.append(unicode(detail)) else: if not ns: ns = {'type': 'section', 'source': book} sections.append(ns) if isinstance(detail, dict) or isinstance(detail, Heading): sectiontext.append(detail) else: sectiontext.append(unicode(detail)) if len(text) > 0: collapse_text(section, text) if len(sectiontext) > 0: collapse_text(ns, sectiontext) if len(sections) > 0: section['sections'] = sections return section
def parse_haunt(sb, book): section = { 'type': 'haunt', 'source': book, 'name': filter_name(''.join(sb.html.pop(0).findAll(text=True))) } section['body'] = ''.join([unicode(elem) for elem in sb.html]) return section
def parse_vehicle(sb, book): vehicle = {'type': 'vehicle', 'source': book, 'name': filter_name(sb.name.strip())} text = [] for key, value in sb.keys: vehicle_parse_function(key)(vehicle, value) for detail in sb.details: for key, value in detail.keys: vehicle_parse_function(key)(vehicle, value) return vehicle
def parse_section(sb, book, no_sb=False, keys=True): section = {"type": "section", "source": book, "name": filter_name(sb.name.strip())} text = [] sections = [] sectiontext = [] ns = None if keys: for key in sb.keys: text.append("<p class='stat-block-1'><b>%s </b>%s</p>" % key) for detail in sb.details: if detail.__class__ == StatBlockSection: sections.append(parse_stat_block(detail, book, no_sb=no_sb)) elif detail.__class__ == StatBlockHeading: sections.append(parse_stat_block(sb, book, no_sb=no_sb)) elif detail.__class__ == dict: if len(sectiontext) > 0: section["text"] = "".join(sectiontext) sectiontext = [] ns = None sections.append(detail) elif detail.__class__ == Heading: if len(sectiontext) > 0: ns["text"] = "".join(sectiontext) sectiontext = [] ns = {"type": "section", "source": book, "name": filter_name(detail.name)} sections.append(ns) else: if len(sections) == 0: text.append(unicode(detail)) else: if not ns: ns = {"type": "section", "source": book} sections.append(ns) sectiontext.append(unicode(detail)) if len(text) > 0: section["text"] = "".join(text) if len(sectiontext) > 0: ns["text"] = "".join(sectiontext) if len(sections) > 0: section["sections"] = sections return section
def parse_vehicle(sb, book): vehicle = { 'type': 'vehicle', 'source': book, 'name': filter_name(sb.name.strip()) } text = [] for key, value in sb.keys: vehicle_parse_function(key)(vehicle, value) for detail in sb.details: for key, value in detail.keys: vehicle_parse_function(key)(vehicle, value) return vehicle
def parse_section(sb, book): section = {'type': 'section', 'source': book, 'name': filter_name(sb.name.strip())} text = [] sections = [] sectiontext = [] ns = None for detail in sb.details: if detail.__class__ == StatBlockSection: sections.append(parse_stat_block(detail, book)) elif detail.__class__ == StatBlockHeading: sections.append(parse_stat_block(sb, book)) elif detail.__class__ == dict: if len(sectiontext) > 0: section['text'] = ''.join(sectiontext) sectiontext = [] ns = None sections.append(detail) elif detail.__class__ == Heading: if len(sectiontext) > 0: ns['text'] = ''.join(sectiontext) sectiontext = [] ns = {'type': 'section', 'source': book, 'name': filter_name(detail.name)} sections.append(ns) else: if len(sections) == 0: text.append(unicode(detail)) else: if not ns: ns = {'type': 'section', 'source': book} sections.append(ns) sectiontext.append(unicode(detail)) if len(text) > 0: section['text'] = ''.join(text) if len(sectiontext) > 0: ns['text'] = ''.join(sectiontext) if len(sections) > 0: section['sections'] = sections return section
def parse_haunt(sb, book): details = [] for detail in sb.details: details.append(detail) sb.details = details haunt = parse_section(sb, book, keys=False) for key, value in sb.keys: haunt_parse_function(key)(haunt, value) haunt["type"] = "haunt" if "CR" in haunt["name"]: names = haunt["name"].split("CR") haunt["name"] = filter_name(names.pop(0).strip()) haunt["cr"] = int(names.pop(0).strip()) return haunt
def parse_haunt(sb, book): details = [] for detail in sb.details: details.append(detail) sb.details = details haunt = parse_section(sb, book, keys=False) for key, value in sb.keys: haunt_parse_function(key)(haunt, value) haunt['type'] = 'haunt' if 'CR' in haunt['name']: names = haunt['name'].split('CR') haunt['name'] = filter_name(names.pop(0).strip()) haunt['cr'] = int(names.pop(0).strip()) return haunt
def parse_creature_type(sb, book): section = {'type': 'section', 'subtype': 'creature_type', 'source': book, 'name': filter_name(sb.name.strip())} desc_text = "" trait_text = "" for key, value in sb.keys: if key == 'Traits': trait_text = trait_text + value else: desc_text = desc_text + value fields = dict(sb.keys) section['text'] = _list_text(desc_text) traits = {'type': 'section', 'source': book, 'name': 'Traits'} traits['text'] = _list_text(trait_text) section['sections'] = [traits] return section
def parse_settlement(sb, book): settlement = {'type': 'settlement', 'source': book, 'name': filter_name(sb.name.strip())} text = [] for key, value in sb.keys: settlement_parse_function(key)(settlement, value) for detail in sb.details: for key, value in detail.keys: settlement_parse_function(key)(settlement, value) if len(detail.details) > 0: sections = settlement.setdefault('sections', []) sec = parse_section(detail, book, keys=False) if sec['name'] == 'DEMOGRAPHICS': sec['name'] = 'Notable NPCs' sections.append(sec) return settlement
def parse_animal_companion(sb, book): ac = {'type': 'animal_companion', 'source': book, 'name': filter_name(sb.name.strip()), 'sections': []} text = [] for key, value in sb.keys: animal_companion_parse_function(key)(ac, value) for detail in sb.details: if detail.__class__ == StatBlockSection and detail.name.endswith('th-Level Advancement'): advancement = {'level': detail.name, 'source': book, 'type': 'animal_companion', 'name': filter_name(ac['name'])} for key, value in detail.keys: animal_companion_parse_function(key)(advancement, value) ac['sections'].append(advancement) else: text.append(detail) if len(text) > 0: ac['text'] = ''.join(text) return ac
def parse_item(sb, book): item = {"type": "item", "source": book, "name": filter_name(sb.name.strip())} text = [] for key, value in sb.keys: item_parse_function(key)(item, value) for detail in sb.details: if detail.__class__ == StatBlockSection and detail.name == "Construction": for key, value in detail.keys: item_parse_function(key)(item, value) elif detail.__class__ == StatBlockSection: sections = item.setdefault("sections", []) sections.append(parse_section(detail, book)) else: text.append(unicode(detail)) if len(text) > 0: item["text"] = "".join(text) return item
def parse_creature(sb, book): names = sb.name.split('CR') creature = {'type': 'creature', 'source': book, 'name': filter_name(names[0])} if len(names) > 1: creature['cr'] = names[1].strip() sections = [] text = [] descriptors = [] for tup in sb.keys: if tup[0] == 'descriptor': descriptors.append(tup) for tup in descriptors: sb.keys.remove(tup) if len(descriptors) > 0: parse_creature_descriptors(creature, descriptors) for key, value in sb.keys: creature_parse_function(key)(creature, value) for detail in sb.details: if detail.name.lower() == 'base statistics': detail.name = 'Statistics' if detail.name.lower() == 'environment': detail.name = 'Ecology' if detail.__class__ == StatBlockSection and detail.name.lower() in ['defense', 'offense', 'statistics', 'ecology']: for key, value in detail.keys: creature_parse_function(key)(creature, value) for subd in detail.details: newsec = {'type': 'section', 'source': book, 'text': unicode(subd)} sections.append(newsec) elif detail.__class__ == StatBlockSection and detail.name.lower() in ['special abilities']: special_abilities = {'type': 'section', 'subtype': 'special_abilities', 'source': book, 'name': 'Special Abilities', 'sections': []} for key in detail.keys: newsec = {'type': 'section', 'source': book, 'name': key[0], 'text': key[1]} special_abilities['sections'].append(newsec) sections.append(special_abilities) for subd in detail.details: newsec = {'type': 'section', 'source': book, 'text': unicode(subd)} sections.append(newsec) else: text.append(unicode(detail)) if len(text) > 0: creature['text'] = ''.join(text) if len(sections) > 0: creature['sections'] = sections return creature
def parse_trap(sb, book): details = [] for detail in sb.details: if detail.__class__ in (StatBlockHeading, StatBlockSection) and detail.name == "Effects": sb.keys.extend(detail.keys) details.extend(detail.details) else: details.append(detail) sb.details = details trap = parse_section(sb, book) for key, value in sb.keys: trap_parse_function(key)(trap, value) trap["type"] = "trap" if "CR" in trap["name"]: names = trap["name"].split("CR") trap["name"] = filter_name(names.pop(0).strip()) trap["cr"] = int(names.pop(0).strip()) return trap
def parse_trap(sb, book): details = [] for detail in sb.details: if detail.__class__ in (StatBlockHeading, StatBlockSection) and detail.name == 'Effects': sb.keys.extend(detail.keys) details.extend(detail.details) else: details.append(detail) sb.details = details trap = parse_section(sb, book, keys=False) for key, value in sb.keys: trap_parse_function(key)(trap, value) trap['type'] = 'trap' if 'CR' in trap['name']: names = trap['name'].split('CR') trap['name'] = filter_name(names.pop(0).strip()) trap['cr'] = names.pop(0).strip() return trap
def parse_item(sb, book): item = { 'type': 'item', 'source': book, 'name': filter_name(sb.name.strip()) } text = [] for key, value in sb.keys: item_parse_function(key)(item, value) for detail in sb.details: if detail.__class__ == StatBlockSection and detail.name == "Construction": for key, value in detail.keys: item_parse_function(key)(item, value) elif detail.__class__ == StatBlockSection: sections = item.setdefault('sections', []) sections.append(parse_section(detail, book)) else: text.append(unicode(detail)) if len(text) > 0: item['text'] = ''.join(text) return item
def parse_animal_companion(sb, book): ac = {'type': 'animal_companion', 'subtype': 'base', 'source': book, 'name': filter_name(sb.name.strip()), 'sections': []} text = [] for key, value in sb.keys: animal_companion_parse_function(key)(ac, value) for detail in sb.details: if detail.__class__ in (StatBlockSection, StatBlockHeading) and detail.name.endswith('th-Level Advancement'): advancement = {'level': detail.name[:3], 'source': book, 'type': 'animal_companion', 'name': filter_name(ac['name'])} for key, value in detail.keys: animal_companion_parse_function(key)(advancement, value) advancement['subtype'] = 'advancement' ac['sections'].append(advancement) else: text.append(detail) if ac["name"].endswith('th-Level Advancement'): ac['level'] = ac['name'][:3] ac['subtype'] = "advancement" if len(text) > 0: collapse_text(ac, text) return ac
def parse_item(sb, book): item = {'type': 'item', 'source': book, 'name': filter_name(sb.name.strip())} text = [] for key, value in sb.keys: item_parse_function(key, sb.name.strip())(item, value) for detail in sb.details: if isinstance(detail, StatBlockSection) and detail.name.startswith("Construction"): for key, value in detail.keys: item_parse_function(key, detail.name.strip())(item, value) elif isinstance(detail, StatBlockSection): sections = item.setdefault('sections', []) sections.append(parse_section(detail, book)) elif isinstance(detail, StatBlockHeading): sections.append(parse_stat_block(sb, book, no_sb=no_sb)) else: if isinstance(detail, dict) or isinstance(detail, Heading): text.append(detail) else: text.append(unicode(detail)) if len(text) > 0: collapse_text(item, text) return item
def parse_creature(sb, book): name = sb.name cr = None if name.find('CR') > -1: name, cr = name.split('CR') creature = {'type': 'creature', 'source': book, 'name': filter_name(name)} if cr: creature['cr'] = cr.strip() sections = [] text = [] descriptors = [] for tup in sb.keys: if tup[0] == 'descriptor': descriptors.append(tup) for tup in descriptors: sb.keys.remove(tup) if len(descriptors) > 0: parse_creature_descriptors(creature, descriptors) for key, value in sb.keys: creature_parse_function(key)(creature, value) for detail in sb.details: if detail.name.lower() == 'base statistics': detail.name = 'Statistics' if detail.name.lower() == 'environment': detail.name = 'Ecology' if detail.__class__ == StatBlockSection and detail.name.lower() in ['defense', 'offense', 'statistics', 'ecology']: for key, value in detail.keys: creature_parse_function(key)(creature, value) for subd in detail.details: if isinstance(subd, dict) or isinstance(subd, Heading): sections.append(subd) else: newsec = {'type': 'section', 'source': book, 'text': unicode(subd)} sections.append(newsec) elif detail.__class__ == StatBlockSection and detail.name.lower() in ['special abilities']: special_abilities = {'type': 'section', 'subtype': 'special_abilities', 'source': book, 'name': 'Special Abilities', 'sections': []} for key in detail.keys: newsec = {'type': 'section', 'source': book, 'name': key[0], 'text': key[1]} special_abilities['sections'].append(newsec) sections.append(special_abilities) for subd in detail.details: if isinstance(subd, dict) or isinstance(subd, Heading): sections.append(subd) else: newsec = {'type': 'section', 'source': book, 'text': unicode(subd)} sections.append(newsec) elif detail.__class__ == StatBlockSection and detail.name.lower() in ['tactics']: sections.append(parse_stat_block(detail, book, no_sb=True)) else: if isinstance(detail, dict) or isinstance(detail, Heading): text.append(detail) else: text.append(unicode(detail)) if len(text) > 0: collapse_text(creature, text) if len(sections) > 0: level = has_heading(sections) while level: sections = title_collapse_pass(sections, level) level = level - 1 if level == 0: sections = sections_pass(sections, creature['source']) creature['sections'] = sections return creature
def parse_haunt(sb, book): section = {'type': 'haunt', 'source': book, 'name': filter_name(''.join(sb.html.pop(0).findAll(text=True)))} section['body'] = ''.join([unicode(elem) for elem in sb.html]) return section
def fxn(spell, value): value = colon_filter(value) effect = spell.setdefault('effects', []) effect.append({'name': filter_name(field), 'text': value})
def parse_creature(sb, book): names = sb.name.split('CR') creature = { 'type': 'creature', 'source': book, 'name': filter_name(names[0]) } if len(names) > 1: creature['cr'] = names[1].strip() sections = [] text = [] descriptors = [] for tup in sb.keys: if tup[0] == 'descriptor': descriptors.append(tup) for tup in descriptors: sb.keys.remove(tup) if len(descriptors) > 0: parse_creature_descriptors(creature, descriptors) for key, value in sb.keys: creature_parse_function(key)(creature, value) for detail in sb.details: if detail.name.lower() == 'base statistics': detail.name = 'Statistics' if detail.name.lower() == 'environment': detail.name = 'Ecology' if detail.__class__ == StatBlockSection and detail.name.lower() in [ 'defense', 'offense', 'statistics', 'ecology' ]: for key, value in detail.keys: creature_parse_function(key)(creature, value) for subd in detail.details: newsec = { 'type': 'section', 'source': book, 'text': unicode(subd) } sections.append(newsec) elif detail.__class__ == StatBlockSection and detail.name.lower() in [ 'special abilities' ]: special_abilities = { 'type': 'section', 'subtype': 'special_abilities', 'source': book, 'name': 'Special Abilities', 'sections': [] } for key in detail.keys: newsec = { 'type': 'section', 'source': book, 'name': key[0], 'text': key[1] } special_abilities['sections'].append(newsec) sections.append(special_abilities) for subd in detail.details: newsec = { 'type': 'section', 'source': book, 'text': unicode(subd) } sections.append(newsec) else: text.append(unicode(detail)) if len(text) > 0: creature['text'] = ''.join(text) if len(sections) > 0: creature['sections'] = sections return creature