User:Skagedal/Footnote renumbering tool

In today's world, User:Skagedal/Footnote renumbering tool has become a topic of increasing interest to people of all ages and walks of life. Whether we are talking about User:Skagedal/Footnote renumbering tool on a personal, professional or social level, its importance and relevance are undeniable. From its origins to its impact today, User:Skagedal/Footnote renumbering tool has been the subject of debate, reflection and study by experts and enthusiasts alike. In this article, we will explore some of the most relevant and current aspects of User:Skagedal/Footnote renumbering tool, as well as its influence on our daily lives. Get ready to immerse yourself in the fascinating world of 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.

#!/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\|(+)\}\}', re.I)
re_fnb = re.compile('\{\{fnb\|(+)\}\}', 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 = 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
		post = change_footnotes(re, s)
		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) + post
	else:
		return s

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