def run(self, reader: TextReader, document: Element) -> bool: reader.step(1) document += addnodes.bracket(marker="]", can_open=False, position=reader.position - 1) self.process_link_or_image(reader, document) return True
def parse(self, document: TextElement) -> TextElement: """Parses a text and build TextElement.""" if len(document) == 0: return document reader = TextReader(cast(Text, document.pop())) while reader.remain: for _, processor in self.processors: if processor.match(reader): if processor.run(reader, document) is True: break else: if len(document) > 0 and isinstance(document[-1], SparseText): tail = document[-1] tail.spread(end=1) else: tail = SparseText(reader.subject, reader.position, reader.position + 1) document += tail if reader.remain[0] == '\\': # escaped tail.spread(end=1) reader.step(2) else: reader.step(1) return document
def parseBareLinkDestination(self, reader: TextReader, document: Element) -> str: assert reader.consume(re.compile(r'[ \n]*')) if reader.remain == '': # must be empty line! return None parens = 0 start = reader.position while reader.remain: c = reader.remain[0] if c in (' ', '\n'): break elif c == '(': parens += 1 elif c == ')': parens -= 1 if parens < 0: break elif escaped_chars_pattern.match(reader.remain): reader.step() # one more step for escaping reader.step() end = reader.position return self.normalize_link_destination(reader[start:end])
def parse_link_destination(self, reader: TextReader, document: Element) -> Tuple[str, str]: reader.step() destination = LinkDestinationParser().parse(reader, document) title = LinkTitleParser().parse(reader, document) assert reader.consume(re.compile(r'\s*\)')) return destination, title
def wrapper(self, reader: TextReader, document: Element, **kwargs) -> bool: new_reader = TextReader(reader.subject, reader.position) try: ret = func(self, new_reader, document, **kwargs) if ret: reader.position = new_reader.position return ret except UnmatchedTokenError as exc: text = exc.args[0] document += Text(text) reader.step(len(text)) return True except Exception: return False
def parse_link_label(self, reader: TextReader, document: Element, opener: Element = None, closer: Element = None) -> Tuple[object, str]: # NOQA reader.step() refname = LinkLabelParser().parse(reader, document) if refname == '': # collapsed reference link # [...][] refname = reader[opener['position']:closer['position']] target = self.lookup_target(document, refname) if target: destination = target.get('refuri') title = target.get('title') return destination, title else: return LABEL_NOT_MATCHED, None
def test_TextReader(): text = "hello world" reader = TextReader(text) assert reader.remain == 'hello world' reader.step() assert reader.remain == 'ello world' matched = reader.consume(re.compile(r'\w+')) assert matched assert matched.group(0) == 'ello' assert reader.remain == ' world' matched = reader.consume(re.compile(r'\w+')) assert matched is None reader.step(6) assert reader.remain == '' reader.step(1) assert reader.remain == ''
def run(self, reader: TextReader, document: Element) -> bool: marker = reader.consume(self.pattern).group(0) pattern = re.compile(marker + r"([^`]|$)") text = addnodes.SparseText(reader.remain, 0, 0) while reader.remain: if pattern.match(reader.remain): code = re.sub(r'[\r\n]', ' ', str(text), re.S) code = self.trim_single_space(code) document += nodes.literal(code, code) reader.step(len(marker)) return True elif reader.remain[0] == '`': while reader.remain and reader.remain[0] == '`': text.spread(end=1) reader.step() else: text.spread(end=1) reader.step() else: raise UnmatchedTokenError(marker)
def run(self, reader: TextReader, document: Element) -> bool: document += addnodes.SparseText(reader.subject, reader.position + 1, reader.position + 2) reader.step(2) return True