Skip to content

Commit 0180782

Browse files
committed
fix(document_iterator): implement a hack for preserving node numbers
1 parent c6a743b commit 0180782

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

strictdoc/core/document_iterator.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ def all_node_content(
8888
print_fragments: bool = False,
8989
level_stack: Tuple[int, ...] = (),
9090
custom_level: bool = False,
91+
update_levels: bool = True,
9192
) -> Iterator[Tuple[SDocIteratedElementIF, DocumentIterationContext]]:
93+
"""
94+
FIXME: update_levels is hack. Change the design so that the node state
95+
is not patched during the iteration.
96+
"""
97+
9298
if isinstance(node, SDocSection):
9399
# If node is not whitelisted, we ignore it. Also, note that due to
94100
# this early return, all child nodes of this node are ignored
@@ -100,8 +106,10 @@ def all_node_content(
100106
context = DocumentIterationContext(
101107
node, level_stack, custom_level=custom_level
102108
)
103-
node.context.title_number_string = context.get_level_string()
104-
node.ng_level = context.get_level()
109+
110+
if update_levels:
111+
node.context.title_number_string = context.get_level_string()
112+
node.ng_level = context.get_level()
105113

106114
yield node, context
107115

@@ -128,6 +136,7 @@ def all_node_content(
128136
level_stack=level_stack + (current_number,),
129137
custom_level=custom_level
130138
or subnode_.ng_resolved_custom_level is not None,
139+
update_levels=update_levels,
131140
)
132141

133142
elif isinstance(node, SDocNode):
@@ -141,8 +150,9 @@ def all_node_content(
141150
context = DocumentIterationContext(
142151
node, level_stack, custom_level=custom_level
143152
)
144-
node.context.title_number_string = context.get_level_string()
145-
node.ng_level = context.get_level()
153+
if update_levels:
154+
node.context.title_number_string = context.get_level_string()
155+
node.ng_level = context.get_level()
146156

147157
yield node, context
148158

@@ -161,6 +171,7 @@ def all_node_content(
161171
level_stack=level_stack + (current_number,),
162172
custom_level=custom_level
163173
or subnode_.ng_resolved_custom_level is not None,
174+
update_levels=update_levels,
164175
)
165176

166177
elif isinstance(node, SDocDocument):
@@ -172,8 +183,11 @@ def all_node_content(
172183
context = DocumentIterationContext(
173184
node, level_stack, custom_level=custom_level
174185
)
175-
node.context.title_number_string = context.get_level_string()
176-
node.ng_level = context.get_level()
186+
if update_levels:
187+
node.context.title_number_string = (
188+
context.get_level_string()
189+
)
190+
node.ng_level = context.get_level()
177191

178192
yield node, context
179193

@@ -198,6 +212,7 @@ def all_node_content(
198212
level_stack=level_stack + (current_number,),
199213
custom_level=custom_level
200214
or subnode_.ng_resolved_custom_level is not None,
215+
update_levels=update_levels,
201216
)
202217

203218
elif isinstance(node, DocumentFromFile):
@@ -210,6 +225,7 @@ def all_node_content(
210225
node.resolved_document,
211226
print_fragments=print_fragments,
212227
level_stack=level_stack,
228+
update_levels=update_levels,
213229
)
214230

215231
else:

strictdoc/core/transforms/delete_requirement.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def validate(self) -> None:
3434
for document_node_, _ in document_iterator.all_node_content(
3535
self.requirement,
3636
print_fragments=True,
37+
# FIXME: update_levels is a hack. See all_node_content().
38+
update_levels=False,
3739
):
3840
if not isinstance(document_node_, SDocNode):
3941
continue

tests/end2end/screens/document/delete_node/_validation/delete_node_must_prevent_deletion_when_children_have_incoming_parent_RELATIONs/test_case.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ def test(self):
3030
requirement.do_delete_node(proceed_with_confirm=False)
3131

3232
screen_document.assert_text(
33-
"Cannot remove node 'Nesting section' with incoming LINKs from: "
34-
"'Requirement title #2' -> 'SECTION_NESTING'."
33+
"Cannot remove node '1. Nesting section' with incoming LINKs from: "
34+
"'2. Requirement title #2' -> 'SECTION_NESTING'."
3535
)
3636
screen_document.assert_text(
37-
"Cannot remove node 'Sub-nesting section' with incoming LINKs from: "
38-
"'Requirement title #3' -> 'SECTION_SUBNESTING'."
37+
"Cannot remove node '1.1. Sub-nesting section' with incoming LINKs from: "
38+
"'3. Requirement title #3' -> 'SECTION_SUBNESTING'."
3939
)
4040
screen_document.assert_text(
41-
"Cannot remove node 'Requirement title #1' with incoming relations from: "
42-
"'Requirement title #2', 'Requirement title #3'."
41+
"Cannot remove node '1.1.1. Requirement title #1' with incoming relations from: "
42+
"'2. Requirement title #2', '3. Requirement title #3'."
4343
)
4444

4545
assert test_setup.compare_sandbox_and_expected_output()

0 commit comments

Comments
 (0)