'Add visible link targets for external links'

def add_visible_links(self, tree, show_urls='inline'):

    def make_footnote_ref(doc, label):
        'Create    a    footnote_reference    node    with    children'
        footnote_ref = nodes.footnote_reference('[#]_')
        footnote_ref.append(nodes.Text(label))
        doc.note_autofootnote_ref(footnote_ref)
        return footnote_ref
    def make_footnote(doc, label, uri):
        'Create    a    footnote    node    with    children'
        footnote = nodes.footnote(uri)
        para = nodes.paragraph()
        para.append(nodes.Text(uri))
        footnote.append(para)
        footnote.insert(0, nodes.label('', label))
        doc.note_autofootnote(footnote)
        return footnote
    def footnote_spot(tree):
        'Find    or    create    a    spot    to    place    footnotes.\n\n                                                The    function    returns    the    tuple    (parent,    index).'
        fns = tree.traverse(nodes.footnote)
        if fns:
            fn = fns[(-1)]
            return (fn.parent, (fn.parent.index(fn) + 1))
        for node in tree.traverse(nodes.rubric):
            if ((len(node.children) == 1) and (node.children[0].astext() == FOOTNOTES_RUBRIC_NAME)):
                return (node.parent, (node.parent.index(node) + 1))
        doc = tree.traverse(nodes.document)[0]
        rub = nodes.rubric()
        rub.append(nodes.Text(FOOTNOTES_RUBRIC_NAME))
        doc.append(rub)
        return (doc, (doc.index(rub) + 1))
    if (show_urls == 'no'):
        return
    if (show_urls == 'footnote'):
        doc = tree.traverse(nodes.document)[0]
        (fn_spot, fn_idx) = footnote_spot(tree)
        nr = 1
    for node in tree.traverse(nodes.reference):
        uri = node.get('refuri', '')
        if ((uri.startswith('http:') or uri.startswith('https:') or uri.startswith('ftp:')) and (uri not in node.astext())):
            idx = (node.parent.index(node) + 1)
            if (show_urls == 'inline'):
                uri = (self.link_target_template % {'uri': uri})
                link = nodes.inline(uri, uri)
                link['classes'].append(self.css_link_target_class)
                node.parent.insert(idx, link)
            elif (show_urls == 'footnote'):
                label = (FOOTNOTE_LABEL_TEMPLATE % nr)
                nr += 1
                footnote_ref = make_footnote_ref(doc, label)
                node.parent.insert(idx, footnote_ref)
                footnote = make_footnote(doc, label, uri)
                fn_spot.insert(fn_idx, footnote)
                footnote_ref['refid'] = footnote['ids'][0]
                footnote.add_backref(footnote_ref['ids'][0])
                fn_idx += 1
