Skip to content

Commit 96c80db

Browse files
authored
👌 IMPROVE: Changed document identification (#51)
Move the comparison of sitemaps and identification of changed documents to `SiteMap.get_changed`, and add tests for this method.
1 parent 091d23a commit 96c80db

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

sphinx_external_toc/api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,21 @@ def as_json(self) -> Dict[str, Any]:
155155
if self.file_format:
156156
data["file_format"] = self.file_format
157157
return data
158+
159+
def get_changed(self, previous: "SiteMap") -> Set[str]:
160+
"""Compare this sitemap to another and return a list of changed documents.
161+
162+
Note: for sphinx, file extensions should be removed to get docnames
163+
"""
164+
changed_docs = set()
165+
# check if the root document has changed
166+
if self.root.docname != previous.root.docname:
167+
changed_docs.add(self.root.docname)
168+
for name, doc in self._docs.items():
169+
if name not in previous:
170+
changed_docs.add(name)
171+
continue
172+
prev_doc = previous[name]
173+
if prev_doc != doc:
174+
changed_docs.add(name)
175+
return changed_docs

sphinx_external_toc/events.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,10 @@ def add_changed_toctrees(
133133
# move external_site_map from config to env
134134
app.env.external_site_map = site_map = app.config.external_site_map
135135
# Compare to previous map, to record docnames with new or changed toctrees
136-
changed_docs = set()
137-
if previous_map:
138-
for docname in site_map:
139-
if (
140-
docname not in previous_map
141-
or site_map[docname] != previous_map[docname]
142-
):
143-
changed_docs.add(docname)
144-
return changed_docs
136+
if not previous_map:
137+
return set()
138+
filenames = site_map.get_changed(previous_map)
139+
return {remove_suffix(name, app.config.source_suffix) for name in filenames}
145140

146141

147142
class TableOfContentsNode(nodes.Element):

tests/test_api.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from sphinx_external_toc.api import Document, FileItem, SiteMap, TocTree
2+
3+
4+
def test_sitemap_get_changed_identical():
5+
"""Test for identical sitemaps."""
6+
root1 = Document("root")
7+
root1.subtrees = [TocTree([])]
8+
sitemap1 = SiteMap(root1)
9+
root2 = Document("root")
10+
root2.subtrees = [TocTree([])]
11+
sitemap2 = SiteMap(root2)
12+
assert sitemap1.get_changed(sitemap2) == set()
13+
14+
15+
def test_sitemap_get_changed_root():
16+
"""Test for sitemaps with changed root."""
17+
doc1 = Document("doc1")
18+
doc2 = Document("doc2")
19+
sitemap1 = SiteMap(doc1)
20+
sitemap1["doc2"] = doc2
21+
sitemap2 = SiteMap(doc2)
22+
sitemap1["doc1"] = doc1
23+
assert sitemap1.get_changed(sitemap2) == {"doc1"}
24+
25+
26+
def test_sitemap_get_changed_title():
27+
"""Test for sitemaps with changed title."""
28+
root1 = Document("root")
29+
root1.title = "a"
30+
sitemap1 = SiteMap(root1)
31+
root2 = Document("root")
32+
root2.title = "b"
33+
sitemap2 = SiteMap(root2)
34+
assert sitemap1.get_changed(sitemap2) == {"root"}
35+
36+
37+
def test_sitemap_get_changed_subtrees():
38+
"""Test for sitemaps with changed subtrees."""
39+
root1 = Document("root")
40+
root1.subtrees = [TocTree([])]
41+
sitemap1 = SiteMap(root1)
42+
root2 = Document("root")
43+
root2.subtrees = [TocTree([FileItem("a")])]
44+
sitemap2 = SiteMap(root2)
45+
assert sitemap1.get_changed(sitemap2) == {"root"}
46+
47+
48+
def test_sitemap_get_changed_subtrees_numbered():
49+
"""Test for sitemaps with changed numbered option."""
50+
root1 = Document("root")
51+
root1.subtrees = [TocTree([], numbered=False)]
52+
sitemap1 = SiteMap(root1)
53+
root2 = Document("root")
54+
root2.subtrees = [TocTree([], numbered=True)]
55+
sitemap2 = SiteMap(root2)
56+
assert sitemap1.get_changed(sitemap2) == {"root"}

0 commit comments

Comments
 (0)