def parse_sections(self): sections = [] section_heading_tags = ['h2', 'h3', 'h4', 'h5', 'h6', 'hr'] section_body_tags = ['ul', 'ol', 'p', 'a'] tags = tuple(section_heading_tags + section_body_tags) events = xmlutils.find_elements_by_tag(self.stream.parse_tree, tag_names=tags, max_depth=1) for e in events: if e.tag in section_heading_tags: title = None if e.tag in ('h2', 'h3', 'h4', 'h5', 'h6'): title = stringify_events(e) section = Section(title=title) sections.append(section) elif e.tag == 'ul' or e.tag == 'ol': if not sections: section = Section() sections.append(section) parts = self.parse_parts(e) sections[-1].add_parts(parts) elif e.tag == 'p': if not sections: section = Section() sections.append(section) parts = self.parse_parts(e) sections[-1].add_parts(parts) return sections
def test_parse_suffix_section(app): source = """ ## Section 1 - [1A](./1A.md) - [1B](./1B.md) --- [2A](./2A.md) [2B](./2B.md) [2C](./2C.md) """ parser = SummaryParser(app, source) actual = parser.parse_sections() expected = [ Section(title='Section 1', parts=[ Part('1A', source='./1A.md', level=0), Part('1B', source='./1B.md', level=0), ]), Section(parts=[ Part('2A', source='./2A.md'), Part('2B', source='./2B.md'), Part('2C', source='./2C.md'), ]), ] assert actual == expected
def test_parse_sections_with_multiple_sections_where_first_section_has_no_heading( app): source = """ - [1A](./1A.md) - [1B](./1B.md) - [1C](./1C.md) ## Section 2 - [2A](./2A.md) """ parser = SummaryParser(app, source) actual = parser.parse_sections() expected = [ Section(parts=[ Part('1A', source='./1A.md', level=0), Part('1B', source='./1B.md', level=0), Part('1C', source='./1C.md', level=0), ]), Section(title='Section 2', parts=[ Part('2A', source='./2A.md', level=0), ]), ] assert actual == expected
def test_parse_sections_with_multiple_sections(app): source = """ ## Section 1 - [1A](./1A.md) - [1B](./1B.md) - [1C](./1C.md) --- - [2A](./2A.md) - [2B](./2B.md) - [2C](./2C.md) - [2D](./2D.md) #### Section 3 - [3A](./3A.md) """ parser = SummaryParser(app, source) actual = parser.parse_sections() expected = [ Section(title='Section 1', parts=[ Part('1A', source='./1A.md', level=0), Part('1B', source='./1B.md', level=0), Part('1C', source='./1C.md', level=0), ]), Section(parts=[ Part('2A', source='./2A.md', level=0), Part('2B', source='./2B.md', level=0), Part('2C', source='./2C.md', level=0), Part('2D', source='./2D.md', level=0), ]), Section(title='Section 3', parts=[ Part('3A', source='./3A.md', level=0), ]), ] assert actual == expected
def test_parse_sections_with_one_section_and_no_heading(app): source = """ - [1A](./1A.md) - [1B](./1B.md) - [1C](./1C.md) """ parser = SummaryParser(app, source) actual = parser.parse_sections() expected = [ Section(parts=[ Part('1A', source='./1A.md', level=0), Part('1B', source='./1B.md', level=0), Part('1C', source='./1C.md', level=0), ]) ] assert actual == expected
def test_parse_section_with_one_section_excluding_non_links(app): src = """ - [First](./first.md) - [Second](./second.md) - Item 1 - Item 2 - [Third](./third.md)""" parser = SummaryParser(app, src) actual = parser.parse_sections() expected = [ Section(parts=[ Part('First', source='./first.md', level=0), Part('Second', source='./second.md', level=0), Part('Third', source='./third.md', level=0), ]) ] assert actual == expected