@@ -261,17 +261,24 @@ def _refuse_unanchored_memos(doc: "HwpxDocument") -> None: |
261 | 261 | ) |
262 | 262 |
|
263 | 263 |
|
264 | | -def _refuse_section_properties_not_first(doc: "HwpxDocument") -> None: |
265 | | - """Raise before writing anything when a section's ``hp:secPr`` is in a later paragraph than its |
266 | | - first one. |
| 264 | +def _move_section_properties_to_first(doc: "HwpxDocument") -> None: |
| 265 | + """Before writing, move a section's ``hp:secPr`` from a later paragraph into its first one. |
267 | 266 |
|
268 | 267 | Hancom reads a section's settings only from its first paragraph: Hangul 2024 opened such a |
269 | 268 | section with default paper and margins and saved a default ``hp:secPr`` in the first paragraph |
270 | 269 | (out/move8/sync/check ``secpr_second_paragraph``). The engine's own edits keep the settings in |
271 | | - the first paragraph; this catches XML edits (``.element``) and files read that way. The |
272 | | - open-safety check reports the same as an error. |
| 270 | + the first paragraph; this shape comes from XML edits (``.element``) and from files made that |
| 271 | + way, among them 6.x's ``section.insert_paragraphs(0, ...)``. 7.0.0 refused to save those, so a |
| 272 | + file 6.x made could not be saved again even unchanged. The settings now move as ``hwpx-repair`` |
| 273 | + moves them (with the page-level controls of their paragraph, to the front of the first |
| 274 | + paragraph), every line layout cache of the section is dropped because its paper and margins |
| 275 | + change, and a :class:`UserWarning` naming ``section-properties-not-first`` says which section. |
| 276 | + The open-safety check still reports the shape as an error in a file read as it is. |
273 | 277 | """ |
274 | 278 |
|
| 279 | + import warnings |
| 280 | + |
| 281 | + from ..oxml._paragraph_text_edit import move_section_settings |
275 | 282 | from ..oxml.namespaces import HP as _HP_NS |
276 | 283 |
|
277 | 284 | for index, section in enumerate(doc.oxml.sections): |
@@ -282,18 +289,17 @@ def _refuse_section_properties_not_first(doc: "HwpxDocument") -> None: |
282 | 289 | if paragraph.find(f"{_HP_NS}run/{_HP_NS}secPr") is not None), None) |
283 | 290 | if holder is None: |
284 | 291 | continue |
285 | | - from ..errors import HwpxValueError |
286 | | - |
287 | | - raise HwpxValueError( |
288 | | - f"section {index}'s settings (hp:secPr) are in its paragraph {holder}, not its first; Hancom " |
289 | | - "reads them only from the first paragraph and gives the section default paper and margins", |
290 | | - code="section-properties-not-first", |
291 | | - context={"section": index, "paragraph": holder}, |
292 | | - suggestion=( |
293 | | - "Put new paragraphs in front with doc.paragraphs[0].insert_before(...) or " |
294 | | - "paragraph.move_before(...), which move the settings along; a file already in this " |
295 | | - "shape is fixed by `hwpx-repair` (python -m hwpx.tools.repair)." |
296 | | - ), |
| 292 | + move_section_settings(paragraphs[holder], paragraphs[0]) |
| 293 | + for paragraph in section.element.iter(f"{_HP_NS}p"): |
| 294 | + for cache in paragraph.findall(f"{_HP_NS}linesegarray"): |
| 295 | + paragraph.remove(cache) |
| 296 | + section.mark_dirty() |
| 297 | + warnings.warn( |
| 298 | + f"section {index}'s settings (hp:secPr) were in its paragraph {holder}, where Hancom does not " |
| 299 | + "read them (it gives the section default paper and margins); the save moved them to the " |
| 300 | + "first paragraph and dropped the section's line layout caches (section-properties-not-first)", |
| 301 | + UserWarning, |
| 302 | + stacklevel=5, # this, save/to_bytes, the archive-read wrapper, HwpxDocument.save |
297 | 303 | ) |
298 | 304 |
|
299 | 305 |
|
@@ -629,7 +635,7 @@ def save( |
629 | 635 | _refuse_lossy_hwp5_save(doc, path) |
630 | 636 | _refuse_unlinked_master_pages(doc) |
631 | 637 | _refuse_unanchored_memos(doc) |
632 | | - _refuse_section_properties_not_first(doc) |
| 638 | + _move_section_properties_to_first(doc) |
633 | 639 | _run_pre_save_validation(doc) |
634 | 640 | archive_bytes, measurement, open_safety = _build_measured(doc, reset_dirty=False) |
635 | 641 | actual_mode, fallback_used = _resolve_grade(mode, fallback, measurement) |
@@ -778,7 +784,7 @@ def to_bytes( |
778 | 784 | _refuse_lossy_hwp5_save(doc) |
779 | 785 | _refuse_unlinked_master_pages(doc) |
780 | 786 | _refuse_unanchored_memos(doc) |
781 | | - _refuse_section_properties_not_first(doc) |
| 787 | + _move_section_properties_to_first(doc) |
782 | 788 | _run_pre_save_validation(doc) |
783 | 789 | archive_bytes, measurement, _open_safety = _build_measured(doc, reset_dirty=False) |
784 | 790 | _resolve_grade(mode, fallback, measurement) |
|