Path: csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python3': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'lookup': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'index': 0.13; 'def': 0.13; '(but': 0.15; 'skip:p 40': 0.15; '[10]': 0.16; 'footnote': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'delta': 0.22; 'keyerror:': 0.22; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'room': 0.27; 'skip:# 10': 0.27; 'yield': 0.27; 'cat': 0.29; '[1]': 0.32; 'run': 0.33; 'changing': 0.34; 'except': 0.34; 'skip:. 20': 0.35; 'skip:i 20': 0.36; 'basic': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'yes': 0.62; 'repeat': 0.67; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Footnotes in ReST Date: Sun, 04 Oct 2015 12:14:16 +0200 Organization: None References: <560fbe70$0$1586$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd82a1.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443953667 news.xs4all.nl 23849 [2001:888:2000:d::a6]:42431 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97389 Peter Otten wrote: > Changing numbers to make room for a new footnote is not much harder (but > less convenient as you have to repeat it for every new footnote): > >>>> def replace(match, n=2): > ... index = int(match.group(1)) > ... if index >= n: > ... index += 1 > ... return "[{}]".format(index) > ... >>>> print(re.compile(r"\[(\d+)\]").sub(replace, text)) > blah blah blah [1]_ and blah blah blah [3]_. > blah blah [4]_ blah ... blah blah > blah blah [1000]_. > > .. [1] fe > .. [3] fi > .. [4] fo > ... > .. [1000] fum Those who remember the old basic dialects might [1]_ like $ cat insert_footnote_basic.py #!/usr/bin/env python3 import re import sys def step(start, delta): n = start while True: yield n n += delta def insert_footnote(text): lookup = {} steps = step(10, 10) def replace(match): index = int(match.group(1)) try: new_index = lookup[index] except KeyError: new_index = lookup[index] = next(steps) return "[{}]".format(new_index) return re.compile(r"\[(\d+)\]").sub(replace, text) if __name__ == "__main__": sys.stdout.write( insert_footnote(sys.stdin.read()) ) $ cat sample.rst blah blah blah [10]_ and blah blah blah [20]_. [21] this is new blah blah [30]_ blah ... blah blah blah blah [9990]_. .. [10] fe .. [20] fi .. [21] yes it is .. [30] fo ... .. [9990] fum $ ./insert_footnote_basic.py < sample.rst blah blah blah [10]_ and blah blah blah [20]_. [30] this is new blah blah [40]_ blah ... blah blah blah blah [50]_. .. [10] fe .. [20] fi .. [30] yes it is .. [40] fo ... .. [50] fum $ .. [1] or run away screaming ;)