def collapse_text(item, lines): textbuf = [] sectionbuf = [] text = True for line in lines: if not isinstance(line, str) and not isinstance(line, unicode): text = False if text: textbuf.append(line) else: if isinstance(line, str) or isinstance(line, unicode): sectionbuf.append({ 'type': 'section', 'source': item['source'], 'text': line }) else: sectionbuf.append(line) item['text'] = ''.join(textbuf) if len(sectionbuf) > 0: sections = item.setdefault('sections', []) sections.extend(sectionbuf) level = has_heading(sections) while level: sections = title_collapse_pass(sections, level) level = level - 1 if level == 0: sections = sections_pass(sections, item['source']) item['sections'] = sections
def collapse_text(item, lines): textbuf = [] sectionbuf = [] text = True for line in lines: if not isinstance(line, str) and not isinstance(line, unicode): text = False if text: textbuf.append(line) else: if isinstance(line, str) or isinstance(line, unicode): sectionbuf.append({'type': 'section', 'source': item['source'], 'text': line}) else: sectionbuf.append(line) item['text'] = ''.join(textbuf) if len(sectionbuf) > 0: sections = item.setdefault('sections', []) sections.extend(sectionbuf) level = has_heading(sections) while level: sections = title_collapse_pass(sections, level) level = level - 1 if level == 0: sections = sections_pass(sections, item['source']) item['sections'] = sections
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