User:Skagedal/Footnote-to-ref

From Wikipedia, the free encyclopedia

A script to convert the article Schizophrenia from using {{fn|...}} for references to using {{ref|...}}.

The results are here. I'm not really happy yet. Why does it start on number 2??

#!/usr/bin/python
#
# Replaces {{fn}}/{{fnb}} with {{ref}}/{{note}} according to a map
#
# Usage: Give the input on stdin, and you'll get output on stdout.

import sys, re

# Use these regular expressions for enwiki
re_fn = re.compile('\{\{fn\|([0-9]+)\}\}', re.I)
re_fnb = re.compile('\{\{fnb\|([0-9]+)\}\}', re.I)

# Use these for svwiki
#re_fn = re.compile('\{\{fotnot\|([0-9]+)\}\}', re.I)
#re_fnb = re.compile('\{\{fotnotText\|([0-9]+)\|\}\}', re.I)

content = sys.stdin.read()

# Define map for Schizophrenia article, as of
# http://en.wikipedia.org/w/index.php?title=Schizophrenia&oldid=31483882

map = {
        1:  "evans_et_al_2003",
        2:  "kraepelin_1907",
        3:  "turner_1999",
        4:  "bertelsen_2002",
        5:  "verdoux_van_os_2002",
        6:  "tsuang_et_al_2000",
        7:  "mcgorry_et_al_1995",
        8:  "read_2004",
        9:  "torrey_et_al_1994",
        10: "koskenvuo_et_al_1984",
        11: "hoeffer_pollin_1970",
        12: "harrison_owen_2003",
        13: "schifman_et_al_2002",
        14: "bebbington_kuipers_1994",
        15: "nielsen_et_al_1987",
        16: "macmillan_et_al_2001",
        17: "schenkel_et_al_2005",
        18: "janssen_et_al_2004",
        19: "van_os_2004",
        20: "sundquist_et_al_2004",
        21: "davies_et_al_2003",
        22: "susser_et_al_1996",
        23: "huttunen_niskanen_1978",
        24: "read_et_al_2001",
        25: "meyer-lindenberg_et_al_2002",
        26: "healy_2002",
        27: "konradi_heckers_2003",
        28: "coyle_et_al_2003",
        29: "johnstone_et_al_1976",
        30: "flashman_green_2004",
        31: "green_2001",
        32: "spencer_et_al_2004",
        33: "lewis_et_al_2005",
        34: "goldner_et_al_2002",
        35: "ustun_et_al_1999",
        36: "leucht_et_al_2003",
        37: "potkin_et_al_2003",
        38: "cormac_et_al_2002",
        39: "zimmerman_et_al_2005",
        40: "wykes_et_al_2002",
        41: "kulhara_1994",
        42: "harding_et_al_1987",
        43: "whitaker_2001",
        44: "hannerz_2001",
        45: "radomsky_et_al_1999",
        46: "caldwell_gottesman_1990",
        47: "arseneault_et_al_2004a",
        48: "arseneault_et_al_2004b",
        49: "hansen_atchinson_ed_2000",
        50: "zammit_et_al_2003",
        51: "walsh_et_al_2004",
        52: "simpson_et_al_2004",
        53: "fazel_grann_2004",
        54: "leong_silva_2003",
        55: "fitzgerald_et_al_2005",
        56: "crow_1997",
        57: "polimeni_reiss_2002",
        58: "torrey_yolken_2003",
        59: "lahti_et_al_2001"
        }

# First check that the map is sane, i.e., every ref-ID is unique

if len(map) != len(set(map.values())):
        sys.stderr.write("Dude, you have duplicates.\n")
        sys.exit(1)

# Change all footnotes in string s.  Returns the changed string.

def change_footnotes(re, template, s):
        match = re.search(s)
        if match:
                fnum = int(match.group(1))
                pre = s[:match.start()]
                post = change_footnotes(re, template, s[match.end():])
                if not map.has_key(fnum):
                        sys.stderr.write("no entry for " + str(fnum))
                        return pre + template % "???" + post
                return pre + template % map[fnum] + post
        else:
                return s

# replace fn with ref
content = change_footnotes(re_fn, "{{ref|%s}}", content)
# replace fnb with note
content = change_footnotes(re_fnb, "# {{note|%s}} ", content)

print change_footnotes(re_fn, "ref", change_footnotes (re_fnb, "note", content))