@backtrack_onerror
def process_link_or_image(self, reader: TextReader, document: Element) -> bool:
    brackets = list((n for n in document.children if isinstance(n, addnodes.bracket)))
    openers = list((d for d in brackets if d['can_open']))
    if (len(openers) == 0):
        return True
    opener = openers.pop()
    closer = brackets.pop()
    if (not opener['active']):
        opener.replace_self(Text(opener['marker']))
        closer.replace_self(Text(closer['marker']))
        return True
    try:
        if reader.remain.startswith('('):
            (destination, title) = self.parse_link_destination(reader, document)
        elif reader.remain.startswith('['):
            (destination, title) = self.parse_link_label(reader, document, opener=opener, closer=closer)
        else:
            destination = None
            title = None
    except (TypeError, ValueError):
        destination = None
        title = None
    if (destination is None):
        refid = reader[opener['position']:closer['position']]
        target = self.lookup_target(document, refid)
        if target:
            destination = target.get('refuri')
            title = target.get('title')
        else:
            opener.replace_self(Text(opener['marker']))
            closer.replace_self(Text(closer['marker']))
            raise
    elif (destination == LABEL_NOT_MATCHED):
        opener.replace_self(Text(opener['marker']))
        closer.replace_self(Text(closer['marker']))
        raise
    node: Element = None
    if (opener['marker'] == '!['):
        from pycmark.transforms import EmphasisConverter
        para = transplant_nodes(document, nodes.paragraph(), start=opener, end=closer)
        EmphasisConverter(para).apply()
        node = nodes.image('', uri=destination, alt=para.astext())
        if title:
            node['title'] = title
    else:
        node = nodes.reference('', refuri=destination)
        transplant_nodes(document, node, start=opener, end=closer)
        if title:
            node['reftitle'] = title
        for n in openers:
            if (n['marker'] == '['):
                n['active'] = False
    document += node
    document.remove(opener)
    document.remove(closer)
    return True
@backtrack_onerror
def process_link_or_image(self, reader: TextReader, document: Element) -> bool:
    brackets = list((n for n in document.children if isinstance(n, addnodes.bracket)))
    openers = list((d for d in brackets if d['can_open']))
    if (len(openers) == 0):
        return True
    opener = openers.pop()
    closer = brackets.pop()
    if (not opener['active']):
        opener.replace_self(Text(opener['marker']))
        closer.replace_self(Text(closer['marker']))
        return True
    try:
        if reader.remain.startswith('('):
            (destination, title) = self.parse_link_destination(reader, document)
        elif reader.remain.startswith('['):
            (destination, title) = self.parse_link_label(reader, document, opener=opener, closer=closer)
        else:
            destination = None
            title = None
    except (TypeError, ValueError):
        destination = None
        title = None
    if (destination is None):
        refid = reader[opener['position']:closer['position']]
        target = self.lookup_target(document, refid)
        if target:
            destination = target.get('refuri')
            title = target.get('title')
        else:
            opener.replace_self(Text(opener['marker']))
            closer.replace_self(Text(closer['marker']))
            raise
    elif (destination == LABEL_NOT_MATCHED):
        opener.replace_self(Text(opener['marker']))
        closer.replace_self(Text(closer['marker']))
        raise
    node: Element = None
    if (opener['marker'] == '!['):
        from pycmark.transforms import EmphasisConverter
        para = transplant_nodes(document, nodes.paragraph(), start=opener, end=closer)
        EmphasisConverter(para).apply()
        node = nodes.image(, uri=destination, alt=para.astext())
        if title:
            node['title'] = title
    else:
        node = nodes.reference(, refuri=destination)
        transplant_nodes(document, node, start=opener, end=closer)
        if title:
            node['reftitle'] = title
        for n in openers:
            if (n['marker'] == '['):
                n['active'] = False
    document += node
    document.remove(opener)
    document.remove(closer)
    return True