12

有谁知道按文件名降序排列 toctree 的任何选项?在升序的情况下,我们可以使用:glob:这样的选项:

.. toctree:
   :glob:

   2011*

这对于用重组文本编写的日常笔记非常方便,这些笔记应该在 Sphinx 文档中报告。

4

3 回答 3

13

没有简单的选项可以对目录树进行反向排序。但是您可以通过在将文档结构写入文件之前对其进行修改来做到这一点。这是一个建议。将以下代码添加到conf.py

def reverse_toctree(app, doctree, docname):
    """Reverse the order of entries in the root toctree if 'glob' is used."""
    if docname == "index":
        for node in doctree.traverse():
            if node.tagname == "toctree" and node.get("glob"):
                node["entries"].reverse()
                break

def setup(app):
    app.connect("doctree-resolved", reverse_toctree)

触发事件时reverse_toctree()调用回调函数。doctree-resolved该函数toctree在文档树中定位节点并就地更改它。

有关 Sphinx 和 Docutils API 的更多详细信息:

于 2011-12-10T17:38:04.993 回答
12

这为 toctree 添加了一个反向选项。

from sphinx.directives import TocTree
from docutils.parsers.rst import directives

class NewTocTree(TocTree):
    option_spec = dict(TocTree.option_spec,
                       reversed=directives.flag)

    def run(self):
        rst = super(NewTocTree, self).run()
        if 'reversed' in self.options:
            rst[0][0]['entries'].reverse()
        return rst

def setup(app):
    app.add_directive('toctree', NewTocTree)

这让您可以:

Contents:

.. toctree::
   :maxdepth: 2
   :reversed:
   :glob:

   20*
于 2012-04-07T20:34:04.057 回答
3

从 Sphinx 1.5+ 开始,您可以将一个内置:reversed:标志添加到目录树中:

.. toctree::
   :glob:
   :reversed:

   2011*

有关详细信息,请参阅Sphinx 文档

于 2018-01-30T18:10:07.730 回答