def _do_links(self, text):
MAX_LINK_TEXT_SENTINEL = <NUM_LIT>  <EOL>anchor_allowed_pos = <NUM_LIT:0><EOL>curr_pos = <NUM_LIT:0><EOL>while True: <EOL><INDENT>try:<EOL><INDENT>start_idx = text.index('<STR_LIT:[>', curr_pos)<EOL><DEDENT>except ValueError:<EOL><INDENT>break<EOL><DEDENT>text_length = len(text)<EOL>bracket_depth = <NUM_LIT:0><EOL>for p in range(start_idx+<NUM_LIT:1>, min(start_idx+MAX_LINK_TEXT_SENTINEL,<EOL>text_length)):<EOL><INDENT>ch = text[p]<EOL>if ch == '<STR_LIT:]>':<EOL><INDENT>bracket_depth -= <NUM_LIT:1><EOL>if bracket_depth < <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT><DEDENT>elif ch == '<STR_LIT:[>':<EOL><INDENT>bracket_depth += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>curr_pos = start_idx + <NUM_LIT:1><EOL>continue<EOL><DEDENT>link_text = text[start_idx+<NUM_LIT:1>:p]<EOL>if "<STR_LIT>" in self.extras and link_text.startswith("<STR_LIT>"):<EOL><INDENT>normed_id = re.sub(r'<STR_LIT>', '<STR_LIT:->', link_text[<NUM_LIT:1>:])<EOL>if normed_id in self.footnotes:<EOL><INDENT>self.footnote_ids.append(normed_id)<EOL>result = '<STR_LIT>''<STR_LIT>'% (normed_id, normed_id, len(self.footnote_ids))<EOL>text = text[:start_idx] + result + text[p+<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>curr_pos = p+<NUM_LIT:1><EOL><DEDENT>continue<EOL><DEDENT>p += <NUM_LIT:1><EOL>if p == text_length:<EOL><INDENT>return text<EOL><DEDENT>if text[p] == '<STR_LIT:(>': <EOL><INDENT>match = self._tail_of_inline_link_re.match(text, p)<EOL>if match:<EOL><INDENT>is_img = start_idx > <NUM_LIT:0> and text[start_idx-<NUM_LIT:1>] == "<STR_LIT:!>"<EOL>if is_img:<EOL><INDENT>start_idx -= <NUM_LIT:1><EOL><DEDENT>url, title = match.group("<STR_LIT:url>"), match.group("<STR_LIT:title>")<EOL>if url and url[<NUM_LIT:0>] == '<STR_LIT:<>':<EOL><INDENT>url = url[<NUM_LIT:1>:-<NUM_LIT:1>]  <EOL><DEDENT>url = url.replace('<STR_LIT:*>', self._escape_table['<STR_LIT:*>']).replace('<STR_LIT:_>', self._escape_table['<STR_LIT:_>'])<EOL>if title:<EOL><INDENT>title_str = '<STR_LIT>' % (<EOL>_xml_escape_attr(title)<EOL>.replace('<STR_LIT:*>', self._escape_table['<STR_LIT:*>'])<EOL>.replace('<STR_LIT:_>', self._escape_table['<STR_LIT:_>']))<EOL><DEDENT>else:<EOL><INDENT>title_str = '<STR_LIT>'<EOL><DEDENT>if is_img:<EOL><INDENT>result = '<STR_LIT>'% (url.replace('<STR_LIT:">', '<STR_LIT>'),<EOL>_xml_escape_attr(link_text),<EOL>title_str, self.empty_element_suffix)<EOL>if "<STR_LIT>" in self.extras:<EOL><INDENT>result = result.replace('<STR_LIT:">', self._escape_table['<STR_LIT:">'])<EOL><DEDENT>curr_pos = start_idx + len(result)<EOL>text = text[:start_idx] + result + text[match.end():]<EOL><DEDENT>elif start_idx >= anchor_allowed_pos:<EOL><INDENT>result_head = '<STR_LIT>' % (url, title_str)<EOL>result = '<STR_LIT>' % (result_head, link_text)<EOL>if "<STR_LIT>" in self.extras:<EOL><INDENT>result = result.replace('<STR_LIT:">', self._escape_table['<STR_LIT:">'])<EOL><DEDENT>curr_pos = start_idx + len(result_head)<EOL>anchor_allowed_pos = start_idx + len(result)<EOL>text = text[:start_idx] + result + text[match.end():]<EOL><DEDENT>else:<EOL><INDENT>curr_pos = start_idx + <NUM_LIT:1><EOL><DEDENT>continue<EOL><DEDENT><DEDENT>else:<EOL><INDENT>match = self._tail_of_reference_link_re.match(text, p)<EOL>if match:<EOL><INDENT>is_img = start_idx > <NUM_LIT:0> and text[start_idx-<NUM_LIT:1>] == "<STR_LIT:!>"<EOL>if is_img:<EOL><INDENT>start_idx -= <NUM_LIT:1><EOL><DEDENT>link_id = match.group("<STR_LIT:id>").lower()<EOL>if not link_id:<EOL><INDENT>link_id = link_text.lower()  <EOL><DEDENT>if link_id in self.urls:<EOL><INDENT>url = self.urls[link_id]<EOL>url = url.replace('<STR_LIT:*>', self._escape_table['<STR_LIT:*>']).replace('<STR_LIT:_>', self._escape_table['<STR_LIT:_>'])<EOL>title = self.titles.get(link_id)<EOL>if title:<EOL><INDENT>before = title<EOL>title = _xml_escape_attr(title).replace('<STR_LIT:*>', self._escape_table['<STR_LIT:*>']).replace('<STR_LIT:_>', self._escape_table['<STR_LIT:_>'])<EOL>title_str = '<STR_LIT>' % title<EOL><DEDENT>else:<EOL><INDENT>title_str = '<STR_LIT>'<EOL><DEDENT>if is_img:<EOL><INDENT>result = '<STR_LIT>'% (url.replace('<STR_LIT:">', '<STR_LIT>'),<EOL>link_text.replace('<STR_LIT:">', '<STR_LIT>'),<EOL>title_str, self.empty_element_suffix)<EOL>if "<STR_LIT>" in self.extras:<EOL><INDENT>result = result.replace('<STR_LIT:">', self._escape_table['<STR_LIT:">'])<EOL><DEDENT>curr_pos = start_idx + len(result)<EOL>text = text[:start_idx] + result + text[match.end():]<EOL><DEDENT>elif start_idx >= anchor_allowed_pos:<EOL><INDENT>result = '<STR_LIT>'% (url, title_str, link_text)<EOL>result_head = '<STR_LIT>' % (url, title_str)<EOL>result = '<STR_LIT>' % (result_head, link_text)<EOL>if "<STR_LIT>" in self.extras:<EOL><INDENT>result = result.replace('<STR_LIT:">', self._escape_table['<STR_LIT:">'])<EOL><DEDENT>curr_pos = start_idx + len(result_head)<EOL>anchor_allowed_pos = start_idx + len(result)<EOL>text = text[:start_idx] + result + text[match.end():]<EOL><DEDENT>else:<EOL><INDENT>curr_pos = start_idx + <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>curr_pos = match.end()<EOL><DEDENT>continue<EOL><DEDENT><DEDENT>curr_pos = start_idx + <NUM_LIT:1><EOL><DEDENT>return text<EOL>
Turn Markdown link shortcuts into XHTML <a> and <img> tags.

        This is a combination of Markdown.pl's _DoAnchors() and
        _DoImages(). They are done together because that simplified the
        approach. It was necessary to use a different approach than
        Markdown.pl because of the lack of atomic matching support in
        Python's regex engine used in $g_nested_brackets.