Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TOC depth option #282

Merged
merged 1 commit into from Nov 16, 2017
Merged

Add TOC depth option #282

merged 1 commit into from Nov 16, 2017

Conversation

MattX
Copy link
Contributor

@MattX MattX commented Nov 15, 2017

As discussed in issue #277.

@@ -243,6 +243,10 @@ def __init__(self, html4tags=False, tab_width=4, safe_mode=None,
assert isinstance(self.extras, dict)
if "toc" in self.extras and "header-ids" not in self.extras:
self.extras["header-ids"] = None # "toc" implies "header-ids"
if self.extras["toc"] is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is if self.extras["toc"] is None checking for? Some testing on my own and it doesn't seem to catch if extras = {'toc': {}} or if extras=['toc']. This might need to be more robust. Maybe something like (pseudo):

default_toc_depth = 6

try:
    self._toc_depth = self.extras["toc"].get("depth", default_toc_depth)
except TypeError:
    self._toc_depth = default_toc_depth

What's the default depth in master branch right now? Is 6 an appropriate default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe both cases you mention are actually are covered:

  • extras=['toc'] -> at this point in the code, self.extras has been coerced to a dictionary (there's even an assert on line 243)
  • extras={'toc': {}} -> this will be caught by the call to .get(), which returns 6 if the dictionary doesn't contain the key depth.

The check is for the case where the user supplied a list, in which case it will be converted by code above (lines 238/241) to a dict with all values set to None, or simply if the user passed extras={'toc': None}. I can still add exception-catching if you'd like, but I don't think it's necessary unless the user is passing strange values in the extras dict.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good look, you're right. I made an assumption that it was still a possible list at that point. No change necessary there.

In regards to the default depth, where did 6 come from? In master is the depth just 1?

Copy link
Contributor Author

@MattX MattX Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I've taken the convention that depth = maximum header level that will be included in the TOC. I believe 6 is the maximum header level, so setting the depth to 6 (or more) will just include all headers in the TOC.

@nicholasserra
Copy link
Collaborator

Thanks for this :) Left some comments.

@nicholasserra nicholasserra merged commit a28426a into trentm:master Nov 16, 2017
@nicholasserra
Copy link
Collaborator

Thank you again

@MattX
Copy link
Contributor Author

MattX commented Nov 16, 2017

Yeah no problem, I needed this for a personal project and I didn't want to maintain a separate branch so it was pretty self-interested haha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants