def _do_link_patterns(self, text):
link_from_hash = {}<EOL>for regex, repl in self.link_patterns:<EOL><INDENT>replacements = []<EOL>for match in regex.finditer(text):<EOL><INDENT>if hasattr(repl, "<STR_LIT>"):<EOL><INDENT>href = repl(match)<EOL><DEDENT>else:<EOL><INDENT>href = match.expand(repl)<EOL><DEDENT>replacements.append((match.span(), href))<EOL><DEDENT>for (start, end), href in reversed(replacements):<EOL><INDENT>escaped_href = (<EOL>href.replace('<STR_LIT:">', '<STR_LIT>')  <EOL>.replace('<STR_LIT:*>', self._escape_table['<STR_LIT:*>'])<EOL>.replace('<STR_LIT:_>', self._escape_table['<STR_LIT:_>']))<EOL>link = '<STR_LIT>' % (escaped_href, text[start:end])<EOL>hash = _hash_text(link)<EOL>link_from_hash[hash] = link<EOL>text = text[:start] + hash + text[end:]<EOL><DEDENT><DEDENT>for hash, link in list(link_from_hash.items()):<EOL><INDENT>text = text.replace(hash, link)<EOL><DEDENT>return text<EOL>
Caveat emptor: there isn't much guarding against link
        patterns being formed inside other standard Markdown links, e.g.
        inside a [link def][like this].

        Dev Notes: *Could* consider prefixing regexes with a negative
        lookbehind assertion to attempt to guard against this.