User:Skagedal/Footnote renumbering tool

From Wikipedia, the free encyclopedia

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

#!/usr/bin/python
#
# This script will renumber the footnotes on a page, so that the first footnote is number 1,
# the second number 2, etc.
#
# For instance, if you have a page that looks like:
#
#    Philip J. Fry{{fn|34}} is a character{{fn|21}} in the Futurama{{fn|25}} series
#    ==Notes==
#    * {{fnb|21}} Yes, he really is a character
#    * {{fnb|25}} By Matt Groening
#    * {{fnb|34}} Silly name, huh?
#
# This script will change it to
#
#    Philip J. Fry{{fn|1}} is a character{{fn|2}} in the Futurama{{fn|3}} series.
#    ==Notes==
#    * {{fnb|2}} Yes, he really is a character
#    * {{fnb|3}} By Matt Groening
#    * {{fnb|1}} Silly name, huh?
#
# As you see, it will not (yet?) reorder your list of footnotes.  You'll have to 
# do that yourself.
#
# 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)

content = sys.stdin.read()

# 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 fnum not in map:
		# first encounter of this footnote 
		new_num += 1
		map[fnum] = new_num

# 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 fnum not in map:
			print >> sys.stderr, "I think you have a {{Fnb|" + \
					 str(fnum) + "}} with no matching {{Fn|" + \
					 str(fnum) + "}}."
			return pre + str(fnum) + post
		return pre + str(map[fnum]) + post
	else:
		return s

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