@@ -0,0 +1,1291 @@
+#!/usr/bin/env python%0A%0Adef clean_links(text):%0A    %22%22%22Remove brackets around a wikilink, keeping the label instead of the page%0A    if it exists.%0A%0A    %22%5B%5Bfoobar%5D%5D%22 will become %22foobar%22, but %22%5B%5Bfoobar%7Ccode words%5D%5D%22 will return%0A    %22code words%22.%0A%0A    Args:%0A        text (str): Full text of a Wikipedia article as a single string.%0A%0A    Returns:%0A        str: A copy of the full text with all wikilinks cleaned.%0A%0A    %22%22%22%0A    good_char_list = %5B%5D%0A    next_char = None%0A    skip = 0%0A%0A    for pos,char in enumerate(text):%0A        try:%0A            next_char = text%5Bpos+1%5D%0A        except IndexError:%0A            next_char = None%0A%0A        # Skip the character%0A        if skip:%0A            skip -= 1%0A            continue%0A%0A        # Otherwise check if we have found a link%0A        if char == '%5B' and next_char == '%5B':%0A            skip = 1%0A            # Check if we are in a comment with%0A            pipe_pos = text.find('%7C', pos)%0A            if pipe_pos == -1:%0A                continue%0A            end_pos = text.find('%5D%5D', pos)%0A            if pipe_pos %3C end_pos:%0A                skip = pipe_pos - pos%0A%0A        elif char == '%5D' and next_char == '%5D':%0A            skip = 1%0A%0A        # Otherwise just append the character%0A        else:%0A            good_char_list.append(char)%0A%0A    return ''.join(good_char_list)%0A

Add Wikilink cleaning script