# -*- coding: utf-8 -*- import io import mistune from bs4 import BeautifulSoup from docx import Document from docx.shared import Pt, Inches from docx.enum.section import WD_SECTION, WD_ORIENT from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT from docx.oxml import OxmlElement from docx.oxml.ns import qn from config import APP_CONFIG class DocxBuildError(RuntimeError): """Raised when markdown cannot be rendered into a valid DOCX document.""" _UNNUMBERED_REFERENCE_HEADINGS = frozenset({ "references", "参考文献", "литература", "список литературы", "пайдаланылған әдебиеттер", }) def _normalize_heading_label(text): """Normalize a heading for exact front/back-matter label matching.""" value = " ".join(str(text or "").replace("\u00a0", " ").split()).casefold() return value.rstrip(" ::") class DocxRenderer(mistune.HTMLRenderer): def __init__(self): super().__init__() self.doc = Document() self.h_counters = [0] * 6 self.is_first_h1 = True try: style = self.doc.styles['Normal'] style.font.name = APP_CONFIG["docx_styles"]["font_name"] style.font.size = Pt(APP_CONFIG["docx_styles"]["font_size_pt"]) except: pass def _render_rich_text(self, p, html_text): if not html_text.strip(): return soup = BeautifulSoup(html_text, 'html.parser') for element in soup.children: if isinstance(element, str): p.add_run(element) elif element.name in ['strong', 'b']: p.add_run(element.get_text()).bold = True elif element.name in ['em', 'i']: p.add_run(element.get_text()).italic = True elif element.name == 'code': run = p.add_run(element.get_text()) run.font.name = APP_CONFIG["docx_styles"]["code_font"] run.font.size = Pt(APP_CONFIG["docx_styles"]["code_size_pt"]) else: p.add_run(element.get_text()) def heading(self, text, level): clean_text = BeautifulSoup(text, 'html.parser').get_text().strip() lower_text = clean_text.lower() normalized_text = _normalize_heading_label(clean_text) skip_keywords = APP_CONFIG["blacklists"]["meta_sections"] exact_reference_heading = normalized_text in _UNNUMBERED_REFERENCE_HEADINGS if exact_reference_heading or any(kw in lower_text for kw in skip_keywords) or (level == 1 and self.is_first_h1): if level == 1: self.is_first_h1 = False self.doc.add_heading(clean_text, level=1) return '' if level == 1: self.is_first_h1 = False idx = level - 1 if 0 <= idx < 6: for j in range(idx): if self.h_counters[j] == 0: self.h_counters[j] = 1 self.h_counters[idx] += 1 for i in range(idx + 1, 6): self.h_counters[i] = 0 num_str = ".".join(map(str, self.h_counters[0:idx+1])) self.doc.add_heading(f"{num_str} {clean_text}", level=level) else: self.doc.add_heading(clean_text, level=level) return '' def paragraph(self, text): if text.strip(): self._render_rich_text(self.doc.add_paragraph(), text) return '' def block_code(self, code, info=None): run = self.doc.add_paragraph().add_run(code) run.font.name = APP_CONFIG["docx_styles"]["code_font"] run.font.size = Pt(APP_CONFIG["docx_styles"]["code_size_pt"]) return '' def block_quote(self, text): p = self.doc.add_paragraph() self._render_rich_text(p, text) try: p.style = 'Intense Quote' if 'Intense Quote' in self.doc.styles else 'Normal' except: pass return '' def list(self, text, ordered, **attrs): return text def list_item(self, text): if text.strip(): p = self.doc.add_paragraph() p.style = 'List Bullet' self._render_rich_text(p, text) return '' def table(self, text): soup = BeautifulSoup(text, 'html.parser') rows = soup.find_all('tr') if not rows: return '' max_cols = max(len(row.find_all(['td', 'th'])) for row in rows) if max_cols == 0: return '' header_labels = [cell.get_text(strip=True) for cell in rows[0].find_all(['td', 'th'])] qualitative_headers = [ 'Higher-order Theme', 'Empirical Concept', 'Process Stage', 'Case/Actor', 'Evidence Role', 'Representative Quote', ] qualitative_evidence_table = header_labels == qualitative_headers if qualitative_evidence_table: previous = self.doc.sections[-1] landscape = self.doc.add_section(WD_SECTION.NEW_PAGE) landscape.orientation = WD_ORIENT.LANDSCAPE landscape.page_width, landscape.page_height = previous.page_height, previous.page_width table = self.doc.add_table(rows=len(rows), cols=max_cols) try: table.style = 'Table Grid' except: pass if qualitative_evidence_table: table.autofit = False widths = [1.15, 1.10, 0.85, 0.85, 0.75, 3.80] if qualitative_evidence_table else [] if qualitative_evidence_table: for grid_col, width in zip(table._tbl.tblGrid.gridCol_lst, widths): grid_col.set(qn('w:w'), str(int(width * 1440))) for i, row in enumerate(rows): for j, cell in enumerate(row.find_all(['td', 'th'])): if j >= max_cols: continue target = table.cell(i, j) target.text = cell.get_text(strip=True) if qualitative_evidence_table: target.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.TOP target.width = Inches(widths[j]) tc_pr = target._tc.get_or_add_tcPr() tc_w = tc_pr.first_child_found_in('w:tcW') if tc_w is not None: tc_w.set(qn('w:type'), 'dxa') tc_w.set(qn('w:w'), str(int(widths[j] * 1440))) for paragraph in target.paragraphs: paragraph.paragraph_format.space_before = Pt(0) paragraph.paragraph_format.space_after = Pt(0) for run in paragraph.runs: run.font.size = Pt(9) if qualitative_evidence_table and table.rows: header_pr = table.rows[0]._tr.get_or_add_trPr() header = OxmlElement('w:tblHeader') header.set(qn('w:val'), 'true') header_pr.append(header) for row in table.rows: tr_pr = row._tr.get_or_add_trPr() cant_split = OxmlElement('w:cantSplit') tr_pr.append(cant_split) self.doc.add_paragraph() if qualitative_evidence_table: current = self.doc.sections[-1] portrait = self.doc.add_section(WD_SECTION.NEW_PAGE) portrait.orientation = WD_ORIENT.PORTRAIT portrait.page_width, portrait.page_height = current.page_height, current.page_width return '' def thematic_break(self): self.doc.add_paragraph('_' * 50) return '' def image(self, src, alt="", title=None): self.doc.add_paragraph(f"[Image: {alt or src}]") return '' def text_to_docx(markdown_content): """Render markdown strictly; never disguise renderer failures as success.""" try: renderer = DocxRenderer() mistune.create_markdown(renderer=renderer, plugins=['table'])(str(markdown_content or '')) doc = renderer.doc if not doc.paragraphs and not doc.tables: raise DocxBuildError("DOCX renderer produced no document content.") stream = io.BytesIO() doc.save(stream) stream.seek(0) return stream except DocxBuildError: raise except Exception as exc: raise DocxBuildError(f"DOCX rendering failed: {exc}") from exc