User:Skagedal/Footnote renumbering tool

Version 0.1. This script was written for the Schizophrenia article. I actually don't know how generalizable it is.


 * 1) !/usr/bin/python
 * 2) This script will renumber the footnotes on a page, so that the first footnote is number 1,
 * 3) the second number 2, etc.
 * 4) For instance, if you have a page that looks like:
 * 5)    Philip J. Fry is a character in the Futurama series
 * 6)    ==Notes==
 * 7)    *  Yes, he really is a character
 * 8)    *  By Matt Groening
 * 9)    *  Silly name, huh?
 * 10) This script will change it to
 * 11)    Philip J. Fry is a character in the Futurama series.
 * 12)    ==Notes==
 * 13)    *  Yes, he really is a character
 * 14)    *  By Matt Groening
 * 15)    *  Silly name, huh?
 * 16) As you see, it will not (yet?) reorder your list of footnotes.  You'll have to
 * 17) do that yourself.
 * 18) Usage: Give the input on stdin, and you'll get output on stdout.
 * 1)    *  By Matt Groening
 * 2)    *  Silly name, huh?
 * 3) As you see, it will not (yet?) reorder your list of footnotes.  You'll have to
 * 4) do that yourself.
 * 5) Usage: Give the input on stdin, and you'll get output on stdout.
 * 1) Usage: Give the input on stdin, and you'll get output on stdout.
 * 1) Usage: Give the input on stdin, and you'll get output on stdout.

import sys, re

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

content = sys.stdin.read


 * 1) Create a map from old footnote number to new footnote number

map = {} new_num = 0 for match in re_fn.finditer(content): fnum = int(match.group(1)) if not map.has_key(fnum): # first encounter of this footnote new_num = new_num + 1 map[fnum] = new_num


 * 1) Change all footnotes in string s.  Returns the changed string.

def change_footnotes(re, s): match = re.search(s) if match: fnum = int(match.group(1)) pre = s[:match.start(1)] post = change_footnotes(re, s[match.end(1):]) if not map.has_key(fnum): sys.stderr.write("I think you have a with no matching .\n") return pre + str(fnum) + post return pre + str(map[fnum]) + post else: return s

print change_footnotes(re_fn, change_footnotes (re_fnb, content))