Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 75d4235

Browse filesBrowse files
committed
Don't treat comments in code blocks as headings
1 parent 8ecacb0 commit 75d4235
Copy full SHA for 75d4235

File tree

Expand file treeCollapse file tree

1 file changed

+21
-13
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-13
lines changed

‎tutorial/toc.py

Copy file name to clipboardExpand all lines: tutorial/toc.py
+21-13Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
import argparse as ap
44
import pathlib
55
import re
6-
from collections import namedtuple
6+
from typing import NamedTuple
77

88
import nbformat
99
from nbformat import NotebookNode
1010

1111

12-
TocEntry = namedtuple("TocEntry", ["level", "text", "anchor"])
12+
class TocEntry(NamedTuple):
13+
"""Table of contents entry"""
14+
15+
level: int
16+
text: str
17+
anchor: str
1318

1419

1520
def extract_markdown_cells(notebook: NotebookNode) -> str:
@@ -22,29 +27,32 @@ def extract_markdown_cells(notebook: NotebookNode) -> str:
2227
def extract_toc(notebook: str) -> list[TocEntry]:
2328
"""Extract the table of contents from a markdown string"""
2429
toc = []
25-
line_re = re.compile(r"(#+)\s+(.+)")
26-
for line in notebook.splitlines():
27-
if groups := re.match(line_re, line):
28-
heading, text, *_ = groups.groups()
29-
level = len(heading)
30+
31+
# Regex trick: use a capture group to match the heading level discarding code blocks
32+
line_re = re.compile(r"```py.*\n#|^(#{1,6})\s+(.+)", re.MULTILINE)
33+
34+
for match in re.findall(line_re, notebook):
35+
if all(match):
36+
level, text = match
3037
anchor = "-".join(text.replace("`", "").split())
31-
toc.append(TocEntry(level, text, anchor))
38+
toc.append(TocEntry(len(level), text, anchor))
39+
3240
return toc
3341

3442

3543
def markdown_toc(toc: list[TocEntry]) -> str:
3644
"""Build a string representation of the toc as a nested markdown list"""
37-
lines = []
38-
for entry in toc:
39-
line = f"{' ' * entry.level}- [{entry.text}](#{entry.anchor})"
40-
lines.append(line)
41-
return "\n".join(lines)
45+
return "\n".join(
46+
f"{' ' * entry.level}- [{entry.text}](#{entry.anchor})" for entry in toc
47+
)
4248

4349

4450
def build_toc(nb_path: pathlib.Path, placeholder: str = "[TOC]") -> NotebookNode:
4551
"""Build a table of contents for a notebook and insert it at the location of a placeholder"""
4652
# Read the notebook
4753
nb_obj: NotebookNode = nbformat.read(nb_path, nbformat.NO_CONVERT)
54+
55+
# Extract markdown cells
4856
md_cells = extract_markdown_cells(nb_obj)
4957

5058
# Build tree

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.