Skip to content

Commit

Permalink
Account for toctrees with :maxdepth:, :titlesonly: , and nested tocs
Browse files Browse the repository at this point in the history
The `titlesonly` option doesn't apply to nested toctrees, only direct entries. The `maxdepth` option does apply to nested toctrees though.

Replacement of toctrees mostly reflects that now.

Doesn't work if a nested toctree has a nested toctree and titlesonly option though (oh well)
  • Loading branch information
TDKorn committed Nov 26, 2023
1 parent 6d74410 commit 5feb8a3
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions sphinx_readme/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,10 @@ def parse_toctrees(self, app: Sphinx, doctree: nodes.document, docname: str) ->
def _parse_toctree(self, toctree, docname, tocs, is_subtoc=False):
toc = {
'caption': toctree.get('caption'),
'titles_only': toctree.get('titlesonly'),
'entries': []
}
if toctree.get('maxdepth') != 1 and not toctree.get('titlesonly'):
if toctree.get('maxdepth') != 1:
toc['maxdepth'] = toctree.get('maxdepth')

for text, entry in toctree.get('entries', []):
Expand Down Expand Up @@ -333,6 +334,9 @@ def _parse_subtree(self, tree: nodes.bullet_list, tocs: Dict[str, nodes.bullet_l
is_subtoc=True, tocs=tocs,
toctree=child,
)
for entry in sub_toc['entries']:
entry['is_subtoc'] = True

sub_trees.extend(sub_toc['entries'])
continue

Expand Down Expand Up @@ -571,14 +575,15 @@ def replace_toctrees(self, rst_src: str, rst: str) -> str:
toctrees = re.findall(pattern, rst, re.M | re.DOTALL)

for toctree, info in zip(toctrees, self.toctrees[rst_src]):
titles_only = info.get('titles_only')
maxdepth = info.get('maxdepth', 1)
repl = ""

if info['caption']:
repl += f"**{info['caption']}**\n\n"

for entry in info['entries']:
repl = self._replace_toctree_entry(rst_src, entry, repl, maxdepth)
repl = self._replace_toctree_entry(rst_src, entry, repl, maxdepth, titles_only)

last_line = repl.strip().split('\n')[-1]
last_indent = len(last_line) - len(last_line.lstrip())
Expand All @@ -589,10 +594,14 @@ def replace_toctrees(self, rst_src: str, rst: str) -> str:

return rst

def _replace_toctree_entry(self, rst_src, entry, repl, maxdepth, indentation = "", depth = 0):
def _replace_toctree_entry(self, rst_src, entry, repl, maxdepth, titles_only, indentation = "", depth = 0):
if depth == maxdepth:
return repl

if depth == 1 and titles_only:
if not entry.get('is_subtoc'):
return repl

# Replace each entry with a link to html docs
target = f"{self.config.html_baseurl}/{entry['entry']}.html{entry.get('anchor', '')}"
link, subs = format_hyperlink(target, text=entry['title'])
Expand All @@ -606,14 +615,13 @@ def _replace_toctree_entry(self, rst_src, entry, repl, maxdepth, indentation = "
repl += "\n" # Add new line for new level
for sub_entry in entry["entries"]:
repl = self._replace_toctree_entry(
rst_src, sub_entry, repl, maxdepth,
rst_src, sub_entry, repl, maxdepth, titles_only,
indentation=f"{indentation} ",
depth=depth + 1
)
repl += "\n"
return repl


def replace_rst_images(self, rst_src: str, rst: str) -> str:
"""Replaces filepaths in ``image`` directives with repository links
Expand Down

0 comments on commit 5feb8a3

Please sign in to comment.