Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97371 > unrolled thread

Footnotes in ReST

Started bySteven D'Aprano <steve@pearwood.info>
First post2015-10-03 21:39 +1000
Last post2015-10-04 12:14 +0200
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Footnotes in ReST Steven D'Aprano <steve@pearwood.info> - 2015-10-03 21:39 +1000
    Re: Footnotes in ReST Chris Angelico <rosuav@gmail.com> - 2015-10-03 21:57 +1000
    Re: Footnotes in ReST Laura Creighton <lac@openend.se> - 2015-10-03 14:21 +0200
      Re: Footnotes in ReST Steven D'Aprano <steve@pearwood.info> - 2015-10-04 00:22 +1000
    Re: Footnotes in ReST Peter Otten <__peter__@web.de> - 2015-10-04 11:50 +0200
    Re: Footnotes in ReST Peter Otten <__peter__@web.de> - 2015-10-04 12:14 +0200

#97371 — Footnotes in ReST

FromSteven D'Aprano <steve@pearwood.info>
Date2015-10-03 21:39 +1000
SubjectFootnotes in ReST
Message-ID<560fbe70$0$1586$c3e8da3$5496439d@news.astraweb.com>
I have a document written in Restructured Text format, and I use lots of
footnotes:

    blah blah blah [1]_ and blah blah blah [2]_.
    blah blah [3]_ blah ... blah blah
    blah blah [999]_.

    .. [1] fe
    .. [2] fi
    .. [3] fo
       ...
    .. [999] fum


I need to add a footnote between [2] and [3], but I don't want to have to
renumber the following 997 footnotes by hand. Is there something I can do,
within the syntax of ReST itself, to help?


-- 
Steven

[toc] | [next] | [standalone]


#97372

FromChris Angelico <rosuav@gmail.com>
Date2015-10-03 21:57 +1000
Message-ID<mailman.362.1443873478.28679.python-list@python.org>
In reply to#97371
On Sat, Oct 3, 2015 at 9:39 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> I need to add a footnote between [2] and [3], but I don't want to have to
> renumber the following 997 footnotes by hand. Is there something I can do,
> within the syntax of ReST itself, to help?

Now that they're all numbered manually? Not sure. But if you know in
advance you're going to have that kind of thing, there are other ways
to do footnotes:

https://hg.python.org/peps/file/tip/pep-0498.txt
https://www.python.org/dev/peps/pep-0498/

Otherwise, I'd look at scripting the 997-footnote changes. It
shouldn't be too hard to script - will probably take only twice as
long as doing it manually would have...

ChrisA

[toc] | [prev] | [next] | [standalone]


#97374

FromLaura Creighton <lac@openend.se>
Date2015-10-03 14:21 +0200
Message-ID<mailman.364.1443874896.28679.python-list@python.org>
In reply to#97371
In a message of Sat, 03 Oct 2015 21:39:26 +1000, "Steven D'Aprano" writes:
>I have a document written in Restructured Text format, and I use lots of
>footnotes:
>
>    blah blah blah [1]_ and blah blah blah [2]_.
>    blah blah [3]_ blah ... blah blah
>    blah blah [999]_.
>
>    .. [1] fe
>    .. [2] fi
>    .. [3] fo
>       ...
>    .. [999] fum
>
>
>I need to add a footnote between [2] and [3], but I don't want to have to
>renumber the following 997 footnotes by hand. Is there something I can do,
>within the syntax of ReST itself, to help?
>
>
>-- 
>Steven

You shouldn't have numbered them manually in the first place.
Use '#' instead to get automatic numbered footnotes.
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#footnote-references

Laura

[toc] | [prev] | [next] | [standalone]


#97377

FromSteven D'Aprano <steve@pearwood.info>
Date2015-10-04 00:22 +1000
Message-ID<560fe49c$0$1587$c3e8da3$5496439d@news.astraweb.com>
In reply to#97374
On Sat, 3 Oct 2015 10:21 pm, Laura Creighton wrote:

> In a message of Sat, 03 Oct 2015 21:39:26 +1000, "Steven D'Aprano" writes:
>>I have a document written in Restructured Text format, and I use lots of
>>footnotes:
[...]
> You shouldn't have numbered them manually in the first place.
> Use '#' instead to get automatic numbered footnotes.
>
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#footnote-references

Thanks.

Especially useful is the ability to label footnotes so that they can be
auto-numbered while still being able to refer to them from multiple places:


    If [#note]_ is the first footnote reference, it will show up as
    "[1]".  We can refer to it again as [#note]_ and again see
    "[1]".  We can also refer to it as note_ (an ordinary internal
    hyperlink reference).

    .. [#note] This is the footnote labeled "note".


That's the problem I was trying to solve by manual numbering.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#97388

FromPeter Otten <__peter__@web.de>
Date2015-10-04 11:50 +0200
Message-ID<mailman.375.1443952246.28679.python-list@python.org>
In reply to#97371
Blake Garretson wrote:

> On Oct 3, 2015 7:40 AM, "Steven D'Aprano" <steve@pearwood.info> wrote:
>> I need to add a footnote between [2] and [3], but I don't want to have to
>> renumber the following 997 footnotes by hand. Is there something I can
>> do, within the syntax of ReST itself, to help?
> 
> I would use a regular expression to find and replace all the numbers with
> the auto-numbering feature.  So something like "\[\d+\]_" should be
> replaced with "\[#\]_".

With labeled autonumbers:

>>> text = """\
...     blah blah blah [1]_ and blah blah blah [2]_.
...     blah blah [3]_ blah ... blah blah
...     blah blah [999]_.
... 
...     .. [1] fe
...     .. [2] fi
...     .. [3] fo
...        ...
...     .. [999] fum
... """
>>> print(re.compile(r"\[(\d+)\]").sub(r"[#n\1]", text))
    blah blah blah [#n1]_ and blah blah blah [#n2]_.
    blah blah [#n3]_ blah ... blah blah
    blah blah [#n999]_.

    .. [#n1] fe
    .. [#n2] fi
    .. [#n3] fo
       ...
    .. [#n999] fum

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


[toc] | [prev] | [next] | [standalone]


#97389

FromPeter Otten <__peter__@web.de>
Date2015-10-04 12:14 +0200
Message-ID<mailman.376.1443953667.28679.python-list@python.org>
In reply to#97371
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 ;)


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web