Update app.py
Browse files
app.py
CHANGED
|
@@ -257,17 +257,16 @@ def merge_hwpx_pages(base_hwpx: bytes, additional_hwpx: bytes) -> bytes:
|
|
| 257 |
zi.flag_bits = 0
|
| 258 |
out_zip.writestr(zi, base_zip.read("mimetype"))
|
| 259 |
|
| 260 |
-
#
|
| 261 |
-
|
| 262 |
-
add_sections = {}
|
| 263 |
|
|
|
|
| 264 |
for filename in base_zip.namelist():
|
| 265 |
if filename == "mimetype":
|
| 266 |
continue
|
| 267 |
if filename.startswith("Contents/section") and filename.endswith(".xml"):
|
| 268 |
-
|
| 269 |
else:
|
| 270 |
-
# ๋ค๋ฅธ ํ์ผ๋ค์ ๊ทธ๋๋ก ๋ณต์ฌ
|
| 271 |
zi = zipfile.ZipInfo(filename)
|
| 272 |
zi.compress_type = zipfile.ZIP_DEFLATED
|
| 273 |
zi.external_attr = 0o100666 << 16
|
|
@@ -276,23 +275,29 @@ def merge_hwpx_pages(base_hwpx: bytes, additional_hwpx: bytes) -> bytes:
|
|
| 276 |
zi.flag_bits = 0
|
| 277 |
out_zip.writestr(zi, base_zip.read(filename))
|
| 278 |
|
| 279 |
-
# ์ถ๊ฐ
|
| 280 |
for filename in add_zip.namelist():
|
| 281 |
if filename.startswith("Contents/section") and filename.endswith(".xml"):
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
| 286 |
|
| 287 |
-
#
|
| 288 |
-
for filename,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
zi = zipfile.ZipInfo(filename)
|
| 290 |
zi.compress_type = zipfile.ZIP_DEFLATED
|
| 291 |
zi.external_attr = 0o100666 << 16
|
| 292 |
zi.create_system = 0
|
| 293 |
zi.date_time = now[:6]
|
| 294 |
zi.flag_bits = 0
|
| 295 |
-
out_zip.writestr(zi,
|
| 296 |
|
| 297 |
base_zip.close()
|
| 298 |
add_zip.close()
|
|
@@ -301,6 +306,36 @@ def merge_hwpx_pages(base_hwpx: bytes, additional_hwpx: bytes) -> bytes:
|
|
| 301 |
|
| 302 |
return out_buf.getvalue()
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
def merge_sections(base_sections: dict, add_sections: dict) -> dict:
|
| 305 |
"""์น์
XML๋ค์ ๋ณํฉ"""
|
| 306 |
merged = base_sections.copy()
|
|
@@ -317,22 +352,25 @@ def merge_sections(base_sections: dict, add_sections: dict) -> dict:
|
|
| 317 |
|
| 318 |
def merge_section_content(base_xml: str, add_xml: str) -> str:
|
| 319 |
"""๋จ์ผ ์น์
XML ๋ด์ฉ์ ๋ณํฉ"""
|
| 320 |
-
# ์ถ๊ฐํ XML์์ ํ์ด์ง๋ค ์ถ์ถ
|
| 321 |
-
page_pattern = re.compile(
|
| 322 |
-
|
|
|
|
|
|
|
| 323 |
|
| 324 |
-
|
|
|
|
|
|
|
| 325 |
return base_xml
|
| 326 |
|
| 327 |
-
#
|
| 328 |
-
|
| 329 |
-
pages_end_pattern = re.compile(r'(</[a-zA-Z0-9_]+:pages>)')
|
| 330 |
|
| 331 |
-
#
|
| 332 |
-
|
|
|
|
| 333 |
|
| 334 |
-
|
| 335 |
-
merged_xml = pages_end_pattern.sub(pages_to_add + r'\1', base_xml)
|
| 336 |
|
| 337 |
return merged_xml
|
| 338 |
|
|
|
|
| 257 |
zi.flag_bits = 0
|
| 258 |
out_zip.writestr(zi, base_zip.read("mimetype"))
|
| 259 |
|
| 260 |
+
# ๋ชจ๋ ํ์ผ์ ์ฒ๋ฆฌ - ์น์
ํ์ผ๋ค์ ๋ณ๋ ๋ณํฉ
|
| 261 |
+
section_files = {}
|
|
|
|
| 262 |
|
| 263 |
+
# ๋ฒ ์ด์ค์์ ๋ชจ๋ ํ์ผ ์์ง
|
| 264 |
for filename in base_zip.namelist():
|
| 265 |
if filename == "mimetype":
|
| 266 |
continue
|
| 267 |
if filename.startswith("Contents/section") and filename.endswith(".xml"):
|
| 268 |
+
section_files[filename] = [base_zip.read(filename).decode("utf-8", errors="ignore")]
|
| 269 |
else:
|
|
|
|
| 270 |
zi = zipfile.ZipInfo(filename)
|
| 271 |
zi.compress_type = zipfile.ZIP_DEFLATED
|
| 272 |
zi.external_attr = 0o100666 << 16
|
|
|
|
| 275 |
zi.flag_bits = 0
|
| 276 |
out_zip.writestr(zi, base_zip.read(filename))
|
| 277 |
|
| 278 |
+
# ์ถ๊ฐ ํ์ผ์์ ์น์
๋ง ์์ง
|
| 279 |
for filename in add_zip.namelist():
|
| 280 |
if filename.startswith("Contents/section") and filename.endswith(".xml"):
|
| 281 |
+
content = add_zip.read(filename).decode("utf-8", errors="ignore")
|
| 282 |
+
if filename in section_files:
|
| 283 |
+
section_files[filename].append(content)
|
| 284 |
+
else:
|
| 285 |
+
section_files[filename] = [content]
|
| 286 |
|
| 287 |
+
# ์น์
ํ์ผ๋ค ๋ณํฉํด์ ์ฐ๊ธฐ
|
| 288 |
+
for filename, contents in section_files.items():
|
| 289 |
+
if len(contents) == 1:
|
| 290 |
+
merged_content = contents[0]
|
| 291 |
+
else:
|
| 292 |
+
merged_content = merge_section_xml_list(contents)
|
| 293 |
+
|
| 294 |
zi = zipfile.ZipInfo(filename)
|
| 295 |
zi.compress_type = zipfile.ZIP_DEFLATED
|
| 296 |
zi.external_attr = 0o100666 << 16
|
| 297 |
zi.create_system = 0
|
| 298 |
zi.date_time = now[:6]
|
| 299 |
zi.flag_bits = 0
|
| 300 |
+
out_zip.writestr(zi, merged_content.encode("utf-8"))
|
| 301 |
|
| 302 |
base_zip.close()
|
| 303 |
add_zip.close()
|
|
|
|
| 306 |
|
| 307 |
return out_buf.getvalue()
|
| 308 |
|
| 309 |
+
def merge_section_xml_list(xml_list: list) -> str:
|
| 310 |
+
"""์ฌ๋ฌ ์น์
XML์ ํ๋๋ก ๋ณํฉ"""
|
| 311 |
+
if len(xml_list) <= 1:
|
| 312 |
+
return xml_list[0] if xml_list else ""
|
| 313 |
+
|
| 314 |
+
base_xml = xml_list[0]
|
| 315 |
+
|
| 316 |
+
for additional_xml in xml_list[1:]:
|
| 317 |
+
# ๊ฐ XML์์ ํ์ด์ง ์ถ์ถ
|
| 318 |
+
page_pattern = re.compile(
|
| 319 |
+
r'<(?P<prefix>[a-zA-Z0-9_]+):page\b[^>]*>.*?</(?P=prefix):page>',
|
| 320 |
+
re.DOTALL
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
add_pages = page_pattern.findall(additional_xml)
|
| 324 |
+
if not add_pages:
|
| 325 |
+
continue
|
| 326 |
+
|
| 327 |
+
# ํ์ด์ง ์ ์ฒด ๋งค์น
|
| 328 |
+
page_matches = page_pattern.finditer(additional_xml)
|
| 329 |
+
pages_to_add = [match.group(0) for match in page_matches]
|
| 330 |
+
|
| 331 |
+
if pages_to_add:
|
| 332 |
+
# ๋ฒ ์ด์ค XML์ </prefix:pages> ํ๊ทธ ์ฐพ์์ ๊ทธ ์์ ์ฝ์
|
| 333 |
+
pages_close_pattern = re.compile(r'(</[a-zA-Z0-9_]+:pages>)')
|
| 334 |
+
pages_str = ''.join(pages_to_add)
|
| 335 |
+
base_xml = pages_close_pattern.sub(pages_str + r'\1', base_xml)
|
| 336 |
+
|
| 337 |
+
return base_xml
|
| 338 |
+
|
| 339 |
def merge_sections(base_sections: dict, add_sections: dict) -> dict:
|
| 340 |
"""์น์
XML๋ค์ ๋ณํฉ"""
|
| 341 |
merged = base_sections.copy()
|
|
|
|
| 352 |
|
| 353 |
def merge_section_content(base_xml: str, add_xml: str) -> str:
|
| 354 |
"""๋จ์ผ ์น์
XML ๋ด์ฉ์ ๋ณํฉ"""
|
| 355 |
+
# ์ถ๊ฐํ XML์์ ํ์ด์ง๋ค ์ถ์ถ - ๋ ์ ํํ ํจํด
|
| 356 |
+
page_pattern = re.compile(
|
| 357 |
+
r'<(?P<prefix>[a-zA-Z0-9_]+):page\b[^>]*>.*?</(?P=prefix):page>',
|
| 358 |
+
re.DOTALL
|
| 359 |
+
)
|
| 360 |
|
| 361 |
+
# ํ์ด์ง ๋งค์นญ
|
| 362 |
+
page_matches = list(page_pattern.finditer(add_xml))
|
| 363 |
+
if not page_matches:
|
| 364 |
return base_xml
|
| 365 |
|
| 366 |
+
# ์ถ๊ฐํ ํ์ด์ง๋ค
|
| 367 |
+
pages_to_add = [match.group(0) for match in page_matches]
|
|
|
|
| 368 |
|
| 369 |
+
# ๋ฒ ์ด์ค XML์ </hp:pages> ๋๋ </hml:pages> ํ๊ทธ ์์ ์ฝ์
|
| 370 |
+
pages_end_pattern = re.compile(r'(</[a-zA-Z0-9_]+:pages>)')
|
| 371 |
+
pages_str = ''.join(pages_to_add)
|
| 372 |
|
| 373 |
+
merged_xml = pages_end_pattern.sub(pages_str + r'\1', base_xml)
|
|
|
|
| 374 |
|
| 375 |
return merged_xml
|
| 376 |
|