def match_link_title(cls, string, offset): new_offset = shift_whitespace(string, offset) if (new_offset == len(string) or '\n' in string[offset:new_offset] and string[new_offset] == '['): return offset, new_offset, '' if string[new_offset] == '"': closing = '"' elif string[new_offset] == "'": closing = "'" elif string[new_offset] == '(': closing = ')' elif '\n' in string[offset:new_offset]: return offset, offset, '' else: # XXX: Can this actually ever happen? return None offset = new_offset escaped = False for i, c in enumerate(string[offset + 1:], start=offset + 1): if c == '\\' and not escaped: escaped = True elif c == closing and not escaped: new_offset = shift_whitespace(string, i + 1) if '\n' not in string[i + 1:new_offset]: return None return offset, new_offset, string[offset + 1:i] elif escaped: escaped = False return None
def match_link_dest(cls, string, offset): offset = shift_whitespace(string, offset + 1) if offset == len(string): return None if string[offset] == '<': escaped = False for i, c in enumerate(string[offset + 1:], start=offset + 1): if c == '\\' and not escaped: escaped = True elif c == ' ' or c == '\n' or (c == '<' and not escaped): return None elif c == '>' and not escaped: return offset, i + 1, string[offset + 1:i] elif escaped: escaped = False return None else: escaped = False count = 0 for i, c in enumerate(string[offset:], start=offset): if c == '\\' and not escaped: escaped = True elif c in whitespace: break elif not escaped: if c == '(': count += 1 elif c == ')': count -= 1 elif is_control_char(c): return None elif escaped: escaped = False if count != 0: return None return offset, i, string[offset:i]
def test_shift_whitespace(self): string = ' \n\t\rfoo' self.assertEqual(shift_whitespace(string, 0), 4) self.assertEqual(shift_whitespace('', 0), 0)