def parse(self, reader: LineReader, document: Element) -> None: """Parses a text and build document.""" while not reader.eof(): for _, processor in self.processors: if processor.match(reader): if processor.run(reader, document): break else: raise RuntimeError('Failed to parse')
def test_LineReader(): reader = LineReader(text.splitlines(), source='dummy.md') assert reader.eof() is False assert reader.get_source_and_line() == ('dummy.md', 0) # read first line assert reader.readline() == "Lorem ipsum dolor sit amet, " assert reader.current_line == "Lorem ipsum dolor sit amet, " assert reader.fetch() == "Lorem ipsum dolor sit amet, " assert reader.fetch(1) == "consectetur adipiscing elit, " assert reader.next_line == "consectetur adipiscing elit, " assert reader.fetch(2) == "" assert reader.get_source_and_line() == ('dummy.md', 1) # read second line assert reader.readline() == "consectetur adipiscing elit, " assert reader.current_line == "consectetur adipiscing elit, " assert reader.fetch() == "consectetur adipiscing elit, " assert reader.fetch(1) == "" assert reader.get_source_and_line() == ('dummy.md', 2) # rollback a line reader.step(-1) assert reader.current_line == "Lorem ipsum dolor sit amet, " assert reader.get_source_and_line() == ('dummy.md', 1) # step a line again reader.step() assert reader.current_line == "consectetur adipiscing elit, " # read until the end assert reader.readline() == "" assert reader.readline() == " sed do eiusmod tempor incididunt " assert reader.readline() == " ut labore et dolore magna aliqua." assert reader.readline() == "" assert reader.readline() == "Ut enim ad minim veniam, quis nostrud" assert reader.eof() is True try: assert reader.readline() assert False, "reader does not raise IOError on EOF" except IOError: pass
def test_nested_line_readers(): text = ("> 1. > Blockquote\n" "continued here.\n") reader = LineReader(text.splitlines(True)) reader = BlockQuoteReader(reader) reader = ListItemReader(reader, r'1\.', BlockProcessor(None)) reader = BlockQuoteReader(reader) reader = LazyLineReader(reader) assert reader.readline() == 'Blockquote\n' assert reader.eof() is False assert reader.next_line == 'continued here.\n' assert reader.readline() == 'continued here.\n'