Câu trả lời trước của tôi không thành công nếu bạn có phân cấp của bảng nội dung vì vậy tôi đã viết chỉ thị đơn giản toctree-filt
có thể lọc các mục dựa trên tiền tố của mục nhập. Ví dụ, đưa ra một chỉ thị toctree-filt
như
.. toctree-filt::
:maxdepth: 1
user-manual
:internal:supervisor-api
:draft:new-feature
:erik:erik-maths
api
và thiết lập danh sách loại trừ để ['draft','erik']
sẽ dẫn đến một toctree hiệu quả trông giống như
.. toctree-filt::
:maxdepth: 1
user-manual
supervisor-api
api
Thêm các dòng sau để conf.py
của bạn:
sys.path.append(os.path.abspath('../sphinx-ext/'))
extensions = ['toctree_filter']
toc_filter_exclude = ['draft','erik']
Đặt mã sau vào /sphinx_ext
bên cạnhcủa bạnthư mục:
import re
from sphinx.directives.other import TocTree
def setup(app):
app.add_config_value('toc_filter_exclude', [], 'html')
app.add_directive('toctree-filt', TocTreeFilt)
return {'version': '1.0.0'}
class TocTreeFilt(TocTree):
"""
Directive to notify Sphinx about the hierarchical structure of the docs,
and to include a table-of-contents like tree in the current document. This
version filters the entries based on a list of prefixes. We simply filter
the content of the directive and call the super's version of run. The
list of exclusions is stored in the **toc_filter_exclusion** list. Any
table of content entry prefixed by one of these strings will be excluded.
If `toc_filter_exclusion=['secret','draft']` then all toc entries of the
form `:secret:ultra-api` or `:draft:new-features` will be excuded from
the final table of contents. Entries without a prefix are always included.
"""
hasPat = re.compile('^\s*:(.+):(.+)$')
# Remove any entries in the content that we dont want and strip
# out any filter prefixes that we want but obviously don't want the
# prefix to mess up the file name.
def filter_entries(self, entries):
excl = self.state.document.settings.env.config.toc_filter_exclude
filtered = []
for e in entries:
m = self.hasPat.match(e)
if m != None:
if not m.groups()[0] in excl:
filtered.append(m.groups()[1])
else:
filtered.append(e)
return filtered
def run(self):
# Remove all TOC entries that should not be on display
self.content = self.filter_entries(self.content)
return super().run()
Bây giờ chỉ cần thay đổi toctree
chỉ hiện tại của bạn để toctree-filt
và bạn tốt để cuộn. Lưu ý rằng Sphinx sẽ đăng lỗi vì nó sẽ tìm các tệp không được bao gồm trong tài liệu. Bạn không chắc chắn cách khắc phục điều đó.
Nguồn
2017-10-06 07:01:11
Câu trả lời này có thể giúp: http://stackoverflow.com/a/22024580/407651 – mzjn