Skip to content

Commit 8b8e6ce

Browse files
committed
Move notice comment insertion to CSLPruner.save
Refactors the process of inserting the contribution notice comment so that it is handled in CSLPruner.save() based on a notice_comment attribute, rather than directly manipulating the XML in CSLBuilder. This centralizes comment insertion logic and simplifies the builder code.
1 parent fb5169b commit 8b8e6ce

File tree

2 files changed

+12
-36
lines changed

2 files changed

+12
-36
lines changed

src/style_variant_builder/build.py

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
from itertools import chain
1515
from pathlib import Path
1616

17-
from lxml import etree
18-
1917
from style_variant_builder.prune import CSLPruner
2018

2119
logging.basicConfig(level=logging.INFO, format="%(message)s")
@@ -140,42 +138,13 @@ def _prune_variant(self, patched_file: Path, output_variant: Path) -> None:
140138
# Flatten single-macro layouts before pruning so wrapper macros can be removed
141139
pruner.flatten_layout_macros()
142140
pruner.prune_macros()
143-
self._insert_notice_comment_xml(pruner)
144-
pruner.save()
145-
146-
def _insert_notice_comment_xml(self, pruner: CSLPruner) -> None:
147-
"""Insert a contribution notice comment as the first child of the <style> element."""
148-
if pruner.root is None:
149-
logging.error("Cannot insert notice comment: XML root is None.")
150-
return
151-
152-
comment_node = etree.Comment(
153-
" This file was generated by the Style Variant Builder "
141+
# Set notice comment to be inserted during save
142+
pruner.notice_comment = (
143+
"This file was generated by the Style Variant Builder "
154144
"<https://github.com/citation-style-language/style-variant-builder>. "
155-
"To contribute changes, modify the template and regenerate variants. "
145+
"To contribute changes, modify the template and regenerate variants."
156146
)
157-
# Insert as the first child of the root <style> element
158-
pruner.root.insert(0, comment_node)
159-
# Insert a tail newline and indentation after the comment to ensure separation and correct indentation for <info>
160-
# Detect the indentation used for children of <style>
161-
if len(pruner.root) > 1:
162-
# Try to infer indentation from the first real element (usually <info>)
163-
next_elem = pruner.root[1]
164-
# Find the preceding text (indentation) for the <info> element
165-
# Default to 2 spaces if not found
166-
indent = " "
167-
# Try to find the indentation from the previous tail or from the element itself
168-
if next_elem.tail and next_elem.tail.lstrip("\n") != next_elem.tail:
169-
# If tail starts with a newline, use the rest as indent
170-
indent = next_elem.tail.split("\n")[-1]
171-
elif (
172-
pruner.root.text
173-
and pruner.root.text.lstrip("\n") != pruner.root.text
174-
):
175-
indent = pruner.root.text.split("\n")[-1]
176-
pruner.root[0].tail = f"\n{indent}"
177-
else:
178-
pruner.root[0].tail = "\n "
147+
pruner.save()
179148

180149
def build_variants(self) -> tuple[int, int]:
181150
try:

src/style_variant_builder/prune.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CSLPruner:
3434
modified: bool = field(
3535
default=False, init=False
3636
) # Track whether changes have been made
37+
notice_comment: str | None = field(default=None, init=False)
3738

3839
def parse_xml(self) -> None:
3940
try:
@@ -280,6 +281,12 @@ def reindent_xml_bytes(self, xml_data: bytes) -> bytes:
280281
def save(self) -> None:
281282
if self.tree is not None:
282283
try:
284+
# Insert notice comment if set
285+
if self.notice_comment and self.root is not None:
286+
# Add spaces around comment text for proper XML comment formatting
287+
comment_text = f" {self.notice_comment.strip()} "
288+
self.root.insert(0, etree.Comment(comment_text))
289+
283290
# Serialize from the element root to avoid including any
284291
# document-level processing instructions (e.g., xml-model)
285292
xml_data = etree.tostring(

0 commit comments

Comments
 (0)