Skip to content

Commit 0a214a8

Browse files
authored
Merge pull request #133 from bittorala/fix-inner-headings
Fix inner headings
2 parents 22c575e + 56f6667 commit 0a214a8

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

docs/contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Tip: If you use google chrome, you can also view the print version of a page ins
3535

3636
## Code Style
3737

38-
Make sure your code *roughly* follows [PEP-8](https://www.python.org/dev/peps/pep-0008/) and keeps things consistent with the rest of the code. I recommended using [black](https://github.com/psf/black) to automatically format your code.
38+
Make sure your code *roughly* follows [PEP-8](https://www.python.org/dev/peps/pep-0008/) and keeps things consistent with the rest of the code. I recommended using [Ruff](https://github.com/astral-sh/ruff) to automatically format your code.
3939

4040
We use google-style docstrings.
4141

src/mkdocs_print_site_plugin/exclude.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@
88
import fnmatch
99
from typing import List
1010

11-
11+
1212
import fnmatch
1313
import os
1414
from typing import List
1515

16+
1617
def exclude(path: str, exclude_patterns: List[str]) -> bool:
1718
"""
1819
Check if a path should be excluded based on a list of patterns.
19-
20+
2021
Args:
2122
path: The path to check
2223
exclude_patterns: List of glob patterns to exclude
23-
24+
2425
Returns:
2526
True if the path should be excluded, False otherwise
2627
"""
@@ -29,27 +30,27 @@ def exclude(path: str, exclude_patterns: List[str]) -> bool:
2930

3031
if not exclude_patterns:
3132
return False
32-
33+
3334
# Normalize path separators to handle both Windows and Unix paths
34-
path = path.replace('\\', '/')
35-
35+
path = path.replace("\\", "/")
36+
3637
for pattern in exclude_patterns:
3738
# Normalize pattern separators
38-
pattern = pattern.replace('\\', '/')
39-
39+
pattern = pattern.replace("\\", "/")
40+
4041
# Check for directory patterns (ending with /)
41-
if pattern.endswith('/'):
42-
if path.startswith(pattern) or path.startswith(pattern[:-1] + '/'):
42+
if pattern.endswith("/"):
43+
if path.startswith(pattern) or path.startswith(pattern[:-1] + "/"):
4344
return True
4445
# Regular glob pattern matching
4546
elif fnmatch.fnmatch(path, pattern):
4647
return True
4748
# Check if path is in a directory that matches the pattern
48-
elif '/' in path:
49-
path_parts = path.split('/')
49+
elif "/" in path:
50+
path_parts = path.split("/")
5051
for i in range(1, len(path_parts)):
51-
partial_path = '/'.join(path_parts[:i])
52+
partial_path = "/".join(path_parts[:i])
5253
if fnmatch.fnmatch(partial_path, pattern) or partial_path == pattern:
5354
return True
54-
55-
return False
55+
56+
return False

src/mkdocs_print_site_plugin/renderer.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,26 @@ def _toc(self):
231231
"""
232232

233233
def _set_inner_heading_styles(self, id: str, prefix: str, level: int) -> str:
234+
"""
235+
By "inner heading" we mean that even if the heading numbers are fully determined by
236+
the nav's hierarchy, if a page has number 3.2.1, we will add a further numbering
237+
to headings such as h2 and h3 inside the page, so that the first h2 that appears is
238+
3.2.1.1, the next one is 3.2.1.2, etc. In this case we will require that the number
239+
of items in this index be <= toc_depth, which is not the case in the ToC (as its depth
240+
is fully determined by the nav's depth).
241+
"""
234242
result = ""
235243
toc_depth = self.plugin_config.get("toc_depth") or 1
236-
h_level = level + 2
237-
counter_names = [f"counter-{id}-{i}" for i in range(h_level, toc_depth + 1)]
238-
while h_level <= toc_depth:
239-
counters_to_reset = " ".join([f"{x} 1" for x in counter_names[h_level - 1 :]])
244+
# Start from h2's
245+
h_level = 2
246+
counter_names = [f"counter-{id}-{i}" for i in range(h_level, toc_depth - level + 1)]
247+
while h_level <= toc_depth - level:
248+
counters_to_reset = " ".join([f"{x} " for x in counter_names[h_level - 1 :]])
249+
counter_reset = f" counter-reset: {counters_to_reset}; " if len(counters_to_reset) > 0 else ""
240250
counters_to_display = " '.' ".join([f"counter({x})" for x in counter_names[: h_level - 1]])
241251
result += f"""
242-
.print-site-enumerate-headings #{id} h{h_level} {{ counter-reset: {counters_to_reset}; counter-increment: counter-{id}-{h_level} }}
243252
.print-site-enumerate-headings #{id} h{h_level}:before {{ content: '{prefix}.' {counters_to_display} ' ' }}
253+
.print-site-enumerate-headings #{id} h{h_level} {{ {counter_reset} counter-increment: counter-{id}-{h_level} }}
244254
"""
245255
h_level += 1
246256

0 commit comments

Comments
 (0)