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_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_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_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_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