GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(save): 6.x가 만든, 구역 설정이 뒤 문단에 있는 문서를 다시 저장한다 (#822) · airmang/python-hwpx@ae6a695

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • CHANGELOG.md
  • docs
    • migration-7.0.md
  • src/hwpx/_document
    • persistence.py
  • tests
    • test_section_properties_first.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎CHANGELOG.md‎

Copy file name to clipboardExpand all lines: CHANGELOG.md
+9Lines changed: 9 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
## [Unreleased]
66

7+
### 고침
8+
9+
- 구역 설정(`hp:secPr`)이 구역 첫 문단이 아닌 뒤 문단에 있는 문서를 7.0.0·7.0.1은 저장하지 않았다
10+
(`section-properties-not-first`). 6.x의 `section.insert_paragraphs(0, ...)`가 그런 파일을 만들었으므로, 6.x로 만든 문서를
11+
7.0에서 열면 고치지 않고 저장해도 거부됐다. 이제 저장이 `hwpx-repair`처럼 구역 설정과 그 문단의 단·머리말·꼬리말·쪽 번호
12+
컨트롤을 첫 문단 앞으로 옮기고, 용지·여백이 바뀌므로 그 구역의 줄 배치 캐시를 모두 지운 뒤 쓰며, 어느 구역을 옮겼는지
13+
`UserWarning`으로 알린다. 한/글이 읽는 자리로 옮기므로 그 구역은 파일이 적어 둔 용지·여백으로 열린다(전에는 한/글이 기본
14+
용지·여백으로 열었다). 그대로 읽은 파일에 대한 열기 안전 검사의 보고는 전과 같다.
15+
716
## [7.0.1] - 2026-11-22
817

918
### 고침
Collapse file

‎docs/migration-7.0.md‎

Copy file name to clipboardExpand all lines: docs/migration-7.0.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ equation = title.add_equation(latex_to_eqedit(r"\frac{a}{b}"), font_size=11.0)
393393
|---|---|
394394
| `add_equation` 이 적는 수식 글꼴이 `HYhwpEQ` 에서 `HancomEQN`(한/글 수식 편집기의 글꼴)으로 바뀌었다. 획이 조금 굵어지고 일부 글자 폭이 달라진다 | `doc.shapes.add_equation(script, font="HYhwpEQ")` |
395395
| 수식 상자 폭을 한/글 2024(Windows)가 실제로 필요로 한 폭에 맞춰 다시 잡는다. 같은 수식의 `hp:sz` 폭이 6.x 와 다르다(맥 한/글이 저장한 상자보다 평균 10% 넓다) | 없음(6.x 폭은 수식 뒤 글자와 겹쳤다) |
396-
| `section.insert_paragraphs(index, paragraphs)`(`doc.oxml.insert_paragraphs` 도)가 넣는 사본에서 구역 설정(`hp:secPr` 와 단·머리말·꼬리말·쪽 번호 컨트롤)을 뺀다. `index=0` 이면 구역 설정이 옛 첫 문단에서 첫 사본으로 옮겨 간다. 6.x 는 사본에 구역 설정을 남기고 맨 앞에 넣을 때 옛 첫 문단에 두어, 한/글이 그 구역을 기본 용지·여백으로 열었다. 구역 설정이 뒤 문단에 있는 문서는 저장이 `section-properties-not-first` 로 거부된다 | 없음. 이미 그런 파일은 `hwpx-repair` 로 고친다 |
396+
| `section.insert_paragraphs(index, paragraphs)`(`doc.oxml.insert_paragraphs` 도)가 넣는 사본에서 구역 설정(`hp:secPr` 와 단·머리말·꼬리말·쪽 번호 컨트롤)을 뺀다. `index=0` 이면 구역 설정이 옛 첫 문단에서 첫 사본으로 옮겨 간다. 6.x 는 사본에 구역 설정을 남기고 맨 앞에 넣을 때 옛 첫 문단에 두어, 한/글이 그 구역을 기본 용지·여백으로 열었다. 구역 설정이 뒤 문단에 있는 문서는 7.0.0·7.0.1 에서 저장이 `section-properties-not-first` 로 거부됐고, 그 뒤 판은 저장할 때 구역 설정을 첫 문단으로 옮기고 그 구역의 줄 배치 캐시를 지운 뒤 `UserWarning` 으로 알린다 | 없음. 6.x 가 만든 그런 파일은 다시 저장하면 고쳐진다(`hwpx-repair` 도 같다) |
397397
| `paragraph.column_break` 를 바꾸면 `page_break` 처럼 그 문단의 줄 배치 캐시(`hp:linesegarray`)가 지워진다. 6.x 는 남겼다. 한/글이 열 때 그 문단을 다시 배치한다 | 없음(옛 줄 배치를 남기지 않는다) |
398398
| `add_heading` 뒤에 `add_paragraph` 로 더한 본문이 제목의 문단 모양·스타일(개요)을 물려받지 않는다. 6.x 에서는 한/글이 그 본문에 다음 개요 번호("나." …)를 붙였다 | `doc.add_paragraph(text, inherit_style=True)` |
399399
| 정렬을 준 머리말·꼬리말·쪽 번호(`set_header`/`set_footer` 의 `align`, `set_page_number`)가 본문 문단의 테두리·간격을 물려받지 않는다 | 없음(6.x 결과는 쪽 번호 밑에 줄이 그어지던 오류다) |
Collapse file

‎src/hwpx/_document/persistence.py‎

Copy file name to clipboardExpand all lines: src/hwpx/_document/persistence.py
+25-19Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,24 @@ def _refuse_unanchored_memos(doc: "HwpxDocument") -> None:
261261
)
262262

263263

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.
267266
268267
Hancom reads a section's settings only from its first paragraph: Hangul 2024 opened such a
269268
section with default paper and margins and saved a default ``hp:secPr`` in the first paragraph
270269
(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.
273277
"""
274278

279+
import warnings
280+
281+
from ..oxml._paragraph_text_edit import move_section_settings
275282
from ..oxml.namespaces import HP as _HP_NS
276283

277284
for index, section in enumerate(doc.oxml.sections):
@@ -282,18 +289,17 @@ def _refuse_section_properties_not_first(doc: "HwpxDocument") -> None:
282289
if paragraph.find(f"{_HP_NS}run/{_HP_NS}secPr") is not None), None)
283290
if holder is None:
284291
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
297303
)
298304

299305

@@ -629,7 +635,7 @@ def save(
629635
_refuse_lossy_hwp5_save(doc, path)
630636
_refuse_unlinked_master_pages(doc)
631637
_refuse_unanchored_memos(doc)
632-
_refuse_section_properties_not_first(doc)
638+
_move_section_properties_to_first(doc)
633639
_run_pre_save_validation(doc)
634640
archive_bytes, measurement, open_safety = _build_measured(doc, reset_dirty=False)
635641
actual_mode, fallback_used = _resolve_grade(mode, fallback, measurement)
@@ -778,7 +784,7 @@ def to_bytes(
778784
_refuse_lossy_hwp5_save(doc)
779785
_refuse_unlinked_master_pages(doc)
780786
_refuse_unanchored_memos(doc)
781-
_refuse_section_properties_not_first(doc)
787+
_move_section_properties_to_first(doc)
782788
_run_pre_save_validation(doc)
783789
archive_bytes, measurement, _open_safety = _build_measured(doc, reset_dirty=False)
784790
_resolve_grade(mode, fallback, measurement)
Collapse file

‎tests/test_section_properties_first.py‎

Copy file name to clipboardExpand all lines: tests/test_section_properties_first.py
+44-6Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import pytest
1717

1818
from hwpx import HwpxDocument
19-
from hwpx.errors import HwpxError
2019
from hwpx.tools.document_merge import append_document, insert_document
2120
from hwpx.tools.package_validator import validate_editor_open_safety, validate_package
2221
from hwpx.tools.repair import repair_repack
@@ -133,12 +132,51 @@ def _second_paragraph_shape() -> bytes:
133132
return doc
134133

135134

136-
def test_the_save_refuses_settings_in_a_later_paragraph() -> None:
135+
def test_the_save_moves_settings_from_a_later_paragraph_and_warns() -> None:
137136
doc = _second_paragraph_shape()
138-
with pytest.raises(HwpxError) as refused:
139-
doc.to_bytes()
140-
assert refused.value.code == "section-properties-not-first"
141-
assert refused.value.context == {"section": 0, "paragraph": 1}
137+
with pytest.warns(UserWarning, match="section-properties-not-first"):
138+
data = doc.to_bytes()
139+
again = HwpxDocument.open(data)
140+
_settings_in_first_run(again)
141+
assert again.sections[0].properties.page_margins.left == LEFT
142+
assert again.sections[0].properties.page_margins.top == TOP
143+
assert again.sections[0].element.find(f".//{_HP}linesegarray") is None
144+
assert [paragraph.text for paragraph in again.paragraphs[:3]] == ["하나", "둘", "셋"]
145+
146+
147+
def test_a_file_6x_made_with_insert_paragraphs_at_the_front_saves_again(tmp_path) -> None:
148+
"""6.x's ``insert_paragraphs(0, ...)`` left the settings in the old first paragraph; 7.0.0
149+
refused to save such a file even unchanged."""
150+
151+
from lxml import etree
152+
153+
good = _doc().to_bytes()
154+
with zipfile.ZipFile(io.BytesIO(good)) as archive:
155+
parts = {name: archive.read(name) for name in archive.namelist()}
156+
infos = {info.filename: info for info in archive.infolist()}
157+
root = etree.fromstring(parts["Contents/section0.xml"])
158+
first = root.find(f"{_HP}p")
159+
copy = etree.fromstring(etree.tostring(first))
160+
for run in copy.findall(f"{_HP}run"):
161+
for child in list(run):
162+
if etree.QName(child).localname in {"secPr", "ctrl"}:
163+
run.remove(child)
164+
copy.set("id", "123")
165+
root.insert(0, copy)
166+
parts["Contents/section0.xml"] = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone=True)
167+
made = tmp_path / "made-by-6x.hwpx"
168+
with zipfile.ZipFile(made, "w") as archive:
169+
for name, payload in parts.items():
170+
archive.writestr(infos[name], payload)
171+
172+
doc = HwpxDocument.open(made)
173+
out = tmp_path / "saved.hwpx"
174+
with pytest.warns(UserWarning, match="section-properties-not-first"):
175+
doc.save(out)
176+
assert validate_editor_open_safety(out).ok
177+
again = HwpxDocument.open(out)
178+
_settings_in_first_run(again)
179+
assert again.sections[0].properties.page_margins.left == LEFT
142180

143181

144182
def test_the_open_safety_check_reports_the_shape(tmp_path) -> None:

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.