mddoco 2.2.1__tar.gz → 2.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. {mddoco-2.2.1/src/mddoco.egg-info → mddoco-2.2.2}/PKG-INFO +1 -1
  2. {mddoco-2.2.1 → mddoco-2.2.2}/pyproject.toml +1 -1
  3. mddoco-2.2.2/src/mddoco/__init__.py +1 -0
  4. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/cli.py +2 -2
  5. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/converter.py +11 -8
  6. mddoco-2.2.2/src/mddoco/toc.py +63 -0
  7. {mddoco-2.2.1 → mddoco-2.2.2/src/mddoco.egg-info}/PKG-INFO +1 -1
  8. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco.egg-info/SOURCES.txt +2 -1
  9. {mddoco-2.2.1 → mddoco-2.2.2}/tests/test_preprocessor.py +1 -1
  10. mddoco-2.2.2/tests/test_toc.py +65 -0
  11. mddoco-2.2.1/src/mddoco/__init__.py +0 -1
  12. mddoco-2.2.1/src/mddoco/toc.py +0 -15
  13. {mddoco-2.2.1 → mddoco-2.2.2}/LICENSE +0 -0
  14. {mddoco-2.2.1 → mddoco-2.2.2}/README.md +0 -0
  15. {mddoco-2.2.1 → mddoco-2.2.2}/setup.cfg +0 -0
  16. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/graph.py +0 -0
  17. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/mermaid.py +0 -0
  18. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/pdf.py +0 -0
  19. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/preprocessor.py +0 -0
  20. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/renderer.py +0 -0
  21. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/scanner.py +0 -0
  22. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/_site_base.html +0 -0
  23. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/academic-wide.html +0 -0
  24. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/academic.html +0 -0
  25. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/dark-wide.html +0 -0
  26. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/dark.html +0 -0
  27. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/default-wide.html +0 -0
  28. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/default.html +0 -0
  29. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/paged-professional.html +0 -0
  30. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/professional-wide.html +0 -0
  31. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/professional.html +0 -0
  32. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/site-dark.html +0 -0
  33. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/site-professional.html +0 -0
  34. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/site-terminal.html +0 -0
  35. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/site.html +0 -0
  36. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/themes/vanilla.html +0 -0
  37. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco/writer.py +0 -0
  38. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco.egg-info/dependency_links.txt +0 -0
  39. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco.egg-info/entry_points.txt +0 -0
  40. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco.egg-info/requires.txt +0 -0
  41. {mddoco-2.2.1 → mddoco-2.2.2}/src/mddoco.egg-info/top_level.txt +0 -0
  42. {mddoco-2.2.1 → mddoco-2.2.2}/tests/test_themes.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mddoco
3
- Version: 2.2.1
3
+ Version: 2.2.2
4
4
  Summary: Markdown to HTML/PDF document converter
5
5
  Author-email: Phil Massyn <phil.massyn@icloud.com>
6
6
  Project-URL: Homepage, https://github.com/massyn/mddoco
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "mddoco"
7
- version = "2.2.1"
7
+ version = "2.2.2"
8
8
  description = "Markdown to HTML/PDF document converter"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -0,0 +1 @@
1
+ __version__ = "2.2.2"
@@ -8,7 +8,7 @@ from mddoco.pdf import html_to_pdf
8
8
  from mddoco.preprocessor import load_context
9
9
  from mddoco.renderer import check_theme_format, render_html
10
10
  from mddoco.scanner import find_markdown_files
11
- from mddoco.toc import combine_toc
11
+ from mddoco.toc import build_toc
12
12
  from mddoco.writer import write_output
13
13
 
14
14
 
@@ -82,7 +82,7 @@ def main(
82
82
  except Exception as exc:
83
83
  raise click.ClickException(str(exc)) from exc
84
84
  sections = [(p, html) for p, html, _ in raw]
85
- toc_html = combine_toc([t for _, _, t in raw]) if toc else None
85
+ toc_html = build_toc([e for _, _, entries in raw for e in entries]) if toc else None
86
86
  has_mermaid = any('<div class="mermaid">' in html for _, html in sections)
87
87
  has_code = any("<pre><code" in html for _, html in sections)
88
88
 
@@ -5,6 +5,7 @@ import markdown
5
5
  from mddoco.graph import process_graph
6
6
  from mddoco.mermaid import process_mermaid
7
7
  from mddoco.preprocessor import render_j2
8
+ from mddoco.toc import TocEntry, flatten_tokens
8
9
 
9
10
 
10
11
  def convert_file(
@@ -12,12 +13,12 @@ def convert_file(
12
13
  toc: bool = False,
13
14
  toc_depth: int = 3,
14
15
  context: dict | None = None,
15
- ) -> tuple[str, str]:
16
+ ) -> tuple[str, list[TocEntry]]:
16
17
  """Convert a markdown (or .md.j2) file to an HTML fragment.
17
18
 
18
19
  .md.j2 files are rendered with Jinja2 first using the supplied context,
19
20
  then the resulting Markdown is converted as normal.
20
- Returns (html_body, toc_html). toc_html is empty when toc=False.
21
+ Returns (html_body, toc_entries). toc_entries is empty when toc=False.
21
22
  """
22
23
  if path.suffix.lower() == ".j2":
23
24
  text = render_j2(path, context or {})
@@ -36,8 +37,8 @@ def convert_file(
36
37
  html, _ = process_mermaid(html)
37
38
  html, _ = process_graph(html)
38
39
 
39
- toc_html: str = md.toc if toc else ""
40
- return html, toc_html
40
+ toc_entries = flatten_tokens(md.toc_tokens) if toc else []
41
+ return html, toc_entries
41
42
 
42
43
 
43
44
  def convert_files(
@@ -45,10 +46,12 @@ def convert_files(
45
46
  toc: bool = False,
46
47
  toc_depth: int = 3,
47
48
  context: dict | None = None,
48
- ) -> list[tuple[Path, str, str]]:
49
- """Convert markdown files, returning (path, html_body, toc_html) tuples."""
49
+ ) -> list[tuple[Path, str, list[TocEntry]]]:
50
+ """Convert markdown files, returning (path, html_body, toc_entries) tuples."""
50
51
  results = []
51
52
  for p in paths:
52
- html, toc_html = convert_file(p, toc=toc, toc_depth=toc_depth, context=context)
53
- results.append((p, html, toc_html))
53
+ html, toc_entries = convert_file(
54
+ p, toc=toc, toc_depth=toc_depth, context=context
55
+ )
56
+ results.append((p, html, toc_entries))
54
57
  return results
@@ -0,0 +1,63 @@
1
+ from dataclasses import dataclass, field
2
+
3
+
4
+ @dataclass
5
+ class TocEntry:
6
+ """One heading in the table of contents, at its absolute heading level."""
7
+
8
+ level: int
9
+ anchor: str
10
+ label: str
11
+
12
+
13
+ @dataclass
14
+ class _Node:
15
+ entry: TocEntry | None
16
+ children: list["_Node"] = field(default_factory=list)
17
+
18
+ def to_html(self) -> str:
19
+ """Render this node's link (if any) followed by its children as a <ul>."""
20
+ link = (
21
+ f'<a href="#{self.entry.anchor}">{self.entry.label}</a>'
22
+ if self.entry
23
+ else ""
24
+ )
25
+ if not self.children:
26
+ return link
27
+ items = "\n".join(f"<li>{child.to_html()}</li>" for child in self.children)
28
+ return f"{link}<ul>\n{items}\n</ul>"
29
+
30
+
31
+ def flatten_tokens(tokens: list[dict]) -> list[TocEntry]:
32
+ """Flatten Python-Markdown ``toc_tokens`` into entries in document order.
33
+
34
+ ``label`` is the token's ``name``, which Python-Markdown has already
35
+ HTML-escaped, so it is safe to place in markup as-is.
36
+ """
37
+ entries: list[TocEntry] = []
38
+ for token in tokens:
39
+ entries.append(TocEntry(token["level"], token["id"], token["name"]))
40
+ entries.extend(flatten_tokens(token["children"]))
41
+ return entries
42
+
43
+
44
+ def build_toc(entries: list[TocEntry]) -> str:
45
+ """Render entries from all files as one nested <ul>, nested by heading level.
46
+
47
+ Nesting follows the absolute heading level, so an H2 always sits one
48
+ level below the top whatever file it came from. An H2 with no H1 before
49
+ it is placed in an unlabelled top-level item rather than promoted.
50
+ """
51
+ root = _Node(None)
52
+ stack: list[_Node] = [] # stack[d] is the open node at heading level d + 1
53
+ for entry in entries:
54
+ del stack[entry.level - 1 :]
55
+ while len(stack) < entry.level - 1:
56
+ placeholder = _Node(None)
57
+ (stack[-1] if stack else root).children.append(placeholder)
58
+ stack.append(placeholder)
59
+ node = _Node(entry)
60
+ (stack[-1] if stack else root).children.append(node)
61
+ stack.append(node)
62
+
63
+ return root.to_html()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mddoco
3
- Version: 2.2.1
3
+ Version: 2.2.2
4
4
  Summary: Markdown to HTML/PDF document converter
5
5
  Author-email: Phil Massyn <phil.massyn@icloud.com>
6
6
  Project-URL: Homepage, https://github.com/massyn/mddoco
@@ -34,4 +34,5 @@ src/mddoco/themes/site-terminal.html
34
34
  src/mddoco/themes/site.html
35
35
  src/mddoco/themes/vanilla.html
36
36
  tests/test_preprocessor.py
37
- tests/test_themes.py
37
+ tests/test_themes.py
38
+ tests/test_toc.py
@@ -14,7 +14,7 @@ from mddoco.preprocessor import (
14
14
 
15
15
 
16
16
  def test_version():
17
- assert mddoco.__version__ == "2.2.1"
17
+ assert mddoco.__version__ == "2.2.2"
18
18
 
19
19
 
20
20
  # --- _parse_csv_line ---
@@ -0,0 +1,65 @@
1
+ from pathlib import Path
2
+
3
+ from mddoco.converter import convert_file
4
+ from mddoco.toc import TocEntry, build_toc
5
+
6
+
7
+ def _toc_for(tmp_path: Path, *documents: str, toc_depth: int = 3) -> str:
8
+ entries = []
9
+ for i, text in enumerate(documents):
10
+ path = tmp_path / f"{i:02d}.md"
11
+ path.write_text(text, encoding="utf-8")
12
+ _, file_entries = convert_file(path, toc=True, toc_depth=toc_depth)
13
+ entries.extend(file_entries)
14
+ return build_toc(entries)
15
+
16
+
17
+ def test_h1_with_subheadings_nests_under_it(tmp_path):
18
+ toc = _toc_for(tmp_path, "# One\n\n## Two\n\n### Three\n")
19
+ assert toc == (
20
+ '<ul>\n<li><a href="#one">One</a><ul>\n'
21
+ '<li><a href="#two">Two</a><ul>\n'
22
+ '<li><a href="#three">Three</a></li>\n</ul></li>\n'
23
+ "</ul></li>\n</ul>"
24
+ )
25
+
26
+
27
+ def test_file_without_h1_nests_its_h2_under_previous_h1(tmp_path):
28
+ toc = _toc_for(tmp_path, "# Intro\n", "## Detail\n")
29
+ assert toc == (
30
+ '<ul>\n<li><a href="#intro">Intro</a><ul>\n'
31
+ '<li><a href="#detail">Detail</a></li>\n'
32
+ "</ul></li>\n</ul>"
33
+ )
34
+
35
+
36
+ def test_leading_h2_is_not_promoted_to_top_level(tmp_path):
37
+ toc = _toc_for(tmp_path, "## Orphan\n\n# Real\n")
38
+ assert toc == (
39
+ '<ul>\n<li><ul>\n<li><a href="#orphan">Orphan</a></li>\n</ul></li>\n'
40
+ '<li><a href="#real">Real</a></li>\n</ul>'
41
+ )
42
+
43
+
44
+ def test_toc_depth_limits_entries(tmp_path):
45
+ toc = _toc_for(tmp_path, "# One\n\n## Two\n\n### Three\n", toc_depth=2)
46
+ assert "#two" in toc
47
+ assert "#three" not in toc
48
+
49
+
50
+ def test_heading_text_is_escaped(tmp_path):
51
+ toc = _toc_for(tmp_path, "# Fish & chips < 5\n")
52
+ assert "Fish &amp; chips &lt; 5" in toc
53
+
54
+
55
+ def test_skipped_level_gets_unlabelled_parent():
56
+ toc = build_toc([TocEntry(1, "a", "A"), TocEntry(3, "c", "C")])
57
+ assert toc == (
58
+ '<ul>\n<li><a href="#a">A</a><ul>\n'
59
+ '<li><ul>\n<li><a href="#c">C</a></li>\n</ul></li>\n'
60
+ "</ul></li>\n</ul>"
61
+ )
62
+
63
+
64
+ def test_no_entries_gives_empty_string():
65
+ assert build_toc([]) == ""
@@ -1 +0,0 @@
1
- __version__ = "2.2.1"
@@ -1,15 +0,0 @@
1
- import re
2
-
3
- _INNER_UL = re.compile(r"<ul>(.*)</ul>", re.DOTALL)
4
-
5
-
6
- def combine_toc(fragments: list[str]) -> str:
7
- """Merge per-file TOC HTML fragments into a single <ul> block."""
8
- items: list[str] = []
9
- for fragment in fragments:
10
- m = _INNER_UL.search(fragment)
11
- if m:
12
- items.append(m.group(1).strip())
13
- if not items:
14
- return ""
15
- return "<ul>\n" + "\n".join(items) + "\n</ul>"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes