I'm trying to come up with a python script to automatically number footnotes in pandoc markdown. Given input like this:
This is a testing document for testing purposes only.[^0] This is a testing document for testing purposes only. This is a testing document for testing purposes only.[^121][^5] This is a testing document for testing purposes only.
[^0]: Footnote contents.
[^0]: Footnote contents.
[^0]: Footnote contents.
It should produce output like this:
This is a testing document for testing purposes only.[^1] This is a testing document for testing purposes only. This is a testing document for testing purposes only.[^2][^3] This is a testing document for testing purposes only.
[^1]: Footnote contents.
[^2]: Footnote contents.
[^3]: Footnote contents.
I've been able to make the basic functionality work, but I'm stuck on how to cover the case of two footnotes on one line. Perhaps the loops should not be line based? Or should I opt for some sort of nested loop, replacing nth occurence of a pattern (which, as I understand from this question is not trivial)?
And since I'm trying to learn as much as possible from this, feel free to drop any comments or pointers for further improvements. Thanks!
Here is the script I have so far:
import re
from sys import argv
script, first = argv
i=0
n=0
buff = ''
# open the file specified through the first argument
f = open(first, 'rU')
for line in f:
if re.search(r'\[\^[0-9]+\]:', line):
i += 1
line2 = re.sub(r'\[\^[0-9]+\]:', '[^' + str(i) + ']:', line)
buff += line2
elif re.search(r'\[\^[0-9]+\]', line):
n += 1
line3 = re.sub(r'\[\^[0-9]+\]', '[^' + str(n) + ']', line)
buff += line3
else:
buff += line
print buff
f.close()