Spaces:
Running
Running
R3.2.8.6-R1.3C-RU5 Four-Language DOCX References Heading Unnumbered
#32
by GuXiaopeng - opened
- utils/docx_builder.py +18 -1
utils/docx_builder.py
CHANGED
|
@@ -11,6 +11,21 @@ class DocxBuildError(RuntimeError):
|
|
| 11 |
"""Raised when markdown cannot be rendered into a valid DOCX document."""
|
| 12 |
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
class DocxRenderer(mistune.HTMLRenderer):
|
| 15 |
def __init__(self):
|
| 16 |
super().__init__()
|
|
@@ -46,9 +61,11 @@ class DocxRenderer(mistune.HTMLRenderer):
|
|
| 46 |
def heading(self, text, level):
|
| 47 |
clean_text = BeautifulSoup(text, 'html.parser').get_text().strip()
|
| 48 |
lower_text = clean_text.lower()
|
|
|
|
| 49 |
skip_keywords = APP_CONFIG["blacklists"]["meta_sections"]
|
|
|
|
| 50 |
|
| 51 |
-
if any(kw in lower_text for kw in skip_keywords) or (level == 1 and self.is_first_h1):
|
| 52 |
if level == 1:
|
| 53 |
self.is_first_h1 = False
|
| 54 |
self.doc.add_heading(clean_text, level=1)
|
|
|
|
| 11 |
"""Raised when markdown cannot be rendered into a valid DOCX document."""
|
| 12 |
|
| 13 |
|
| 14 |
+
_UNNUMBERED_REFERENCE_HEADINGS = frozenset({
|
| 15 |
+
"references",
|
| 16 |
+
"参考文献",
|
| 17 |
+
"литература",
|
| 18 |
+
"список литературы",
|
| 19 |
+
"пайдаланылған әдебиеттер",
|
| 20 |
+
})
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _normalize_heading_label(text):
|
| 24 |
+
"""Normalize a heading for exact front/back-matter label matching."""
|
| 25 |
+
value = " ".join(str(text or "").replace("\u00a0", " ").split()).casefold()
|
| 26 |
+
return value.rstrip(" ::")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
class DocxRenderer(mistune.HTMLRenderer):
|
| 30 |
def __init__(self):
|
| 31 |
super().__init__()
|
|
|
|
| 61 |
def heading(self, text, level):
|
| 62 |
clean_text = BeautifulSoup(text, 'html.parser').get_text().strip()
|
| 63 |
lower_text = clean_text.lower()
|
| 64 |
+
normalized_text = _normalize_heading_label(clean_text)
|
| 65 |
skip_keywords = APP_CONFIG["blacklists"]["meta_sections"]
|
| 66 |
+
exact_reference_heading = normalized_text in _UNNUMBERED_REFERENCE_HEADINGS
|
| 67 |
|
| 68 |
+
if exact_reference_heading or any(kw in lower_text for kw in skip_keywords) or (level == 1 and self.is_first_h1):
|
| 69 |
if level == 1:
|
| 70 |
self.is_first_h1 = False
|
| 71 |
self.doc.add_heading(clean_text, level=1)
|