def read_table_header(self, reader: LineReader) -> Tuple[nodes.row, List[str]]: try: location = reader.get_source_and_line(incr=1) header = self.parse_row(reader.readline()) if self.parser.is_interrupted(reader): raise IOError elif not self.delimiter_pattern.match(reader.next_line): raise IOError delimiters = self.parse_row(reader.next_line) aligns = [align(d) for d in delimiters] if len(header) != len(delimiters): raise IOError except IOError: reader.step(-1) return None, None reader.step() row = nodes.row() for i, cell in enumerate(header): text = cell.strip() entry = nodes.entry() location.set_source_info(entry) if text: entry += nodes.paragraph(text, text) if aligns[i]: entry['align'] = aligns[i] row += entry return row, aligns
def read_table_body(self, reader: LineReader, aligns: List[str]) -> List[nodes.row]: try: rows = [] while True: if self.parser.is_interrupted(reader): break else: row = self.parse_row(reader.readline()) if len(row) == 1 and row[0] == '': reader.step(-1) break entries = [] for i, align in enumerate(aligns): try: text = row[i].strip() except IndexError: text = '' entry = nodes.entry() if text: entry += nodes.paragraph(text, text) entry.source, entry.line = reader.get_source_and_line() if align: entry['align'] = align entries.append(entry) rows.append(nodes.row('', *entries)) except IOError: pass return rows
def run(self, reader: LineReader, document: Element) -> bool: location = reader.get_source_and_line(incr=1) lines = [] underline = None try: for line in reader: lines.append(line.lstrip()) if self.pattern.match(reader.next_line): underline = reader.readline() break elif self.parser.is_interrupted(reader): break except IOError: pass if underline is None: # underline of heading not found. backtracking. reader.step(-len(lines)) return False else: text = ''.join(lines).strip() depth = self.section_level[underline.strip()[0]] section = nodes.section(depth=depth) section += nodes.title(text, text) location.set_source_info(section[0]) get_root_document(document).note_implicit_target(section) document += section return True
def consume_blanklines(self, reader: LineReader, list_item: nodes.list_item) -> None: """Skip over blank lines at beginning of the list item.""" try: while reader.next_line.strip() == '': reader.step() list_item += addnodes.blankline() except IOError: pass
def run(self, reader: LineReader, document: Element) -> bool: lineno = reader.lineno try: multiline_reader = MultiLineReader(reader) target = self.parse_linkref_definition(multiline_reader, document) if target: document += target return True else: reader.step(lineno - reader.lineno) # rollback return False except IOError: reader.step(lineno - reader.lineno) # rollback return False
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 run(self, reader: LineReader, document: Element) -> bool: if reader.lineno != 0: return False reader.readline() field_list = nodes.field_list() field_list.source, field_list.line = reader.get_source_and_line() for line in reader: if self.pattern.match(line): break elif ':' in line: key, value = line.split(':') field_name = nodes.field_name('', key.strip()) field_body = nodes.field_body('', nodes.paragraph('', value.strip())) field_list += nodes.field('', field_name, field_body) else: # Not a frontmatter, rollback lines = len(field_list) + 2 reader.step(-lines) return False document += field_list return True