Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: add LaTeX escaping and preparation functions for Gradio preview
myhlolicc9fea8148ab
diff --git a/mineru/cli/gradio_app.py b/mineru/cli/gradio_app.pyindex 1a5f660b..d347f733 100644--- a/mineru/cli/gradio_app.py+++ b/mineru/cli/gradio_app.py@@ -486,6 +486,67 @@ def replace_image_with_base64(markdown_text, image_dir_path):return result+def _escape_latex_html_chars_for_gradio(content):+ """转义公式内部会被 Gradio HTML 解析链路误判的字符。"""+ return content.replace("&", "&").replace("<", "<").replace(">", ">")+++def escape_latex_blocks_for_gradio_preview(markdown_text, latex_delimiters):+ """根据当前 LaTeX 分隔符,仅转义公式内容,避免影响公式外 Markdown/HTML。"""+ if not markdown_text or not latex_delimiters:+ return markdown_text++ delimiter_pairs = []+ for delimiter in latex_delimiters:+ left = delimiter.get("left")+ right = delimiter.get("right")+ if left and right:+ delimiter_pairs.append((left, right))+ delimiter_pairs.sort(key=lambda pair: len(pair[0]), reverse=True)+ if not delimiter_pairs:+ return markdown_text++ result = []+ position = 0+ text_length = len(markdown_text)+ while position < text_length:+ matched_pair = None+ for left, right in delimiter_pairs:+ if markdown_text.startswith(left, position):+ matched_pair = (left, right)+ break++ if matched_pair is None:+ result.append(markdown_text[position])+ position += 1+ continue++ left, right = matched_pair+ content_start = position + len(left)+ content_end = markdown_text.find(right, content_start)+ if content_end == -1:+ # 未闭合的分隔符保持原样,并继续扫描后续可能闭合的公式块。+ result.append(markdown_text[position])+ position += 1+ continue++ result.append(left)+ result.append(+ _escape_latex_html_chars_for_gradio(markdown_text[content_start:content_end])+ )+ result.append(right)+ position = content_end + len(right)++ return "".join(result)+++def prepare_markdown_for_gradio_preview(markdown_text, latex_delimiters):+ """准备传给 gr.Markdown 的预览文本;原始 Markdown 文件内容不在这里改写。"""+ if not isinstance(markdown_text, str):+ return markdown_text+ return escape_latex_blocks_for_gradio_preview(markdown_text, latex_delimiters)++def normalize_language(language):if '(' in language and ')' in language:return language.split('(')[0].strip()@@ -1254,6 +1315,12 @@ def main(ctx,url=url,api_url=api_url,):+ update = (+ update[0],+ update[1],+ prepare_markdown_for_gradio_preview(update[2], latex_delimiters),+ *update[3:],+ )yield updatesuffixes = [f".{suffix}" for suffix in pdf_suffixes + image_suffixes + office_suffixes]diff --git a/mineru/model/docx/tools/math/omml.py b/mineru/model/docx/tools/math/omml.pyindex a1687d41..c92eeb44 100644--- a/mineru/model/docx/tools/math/omml.py+++ b/mineru/model/docx/tools/math/omml.py@@ -95,6 +95,13 @@ def _normalize_latex_delimiter(delimiter):return delimiter+def _ensure_latex_command_boundary(latex_text):+ """为裸 LaTeX 字母控制词补终止空格,避免后续变量被拼成未知命令。"""+ if re.fullmatch(r"\\[A-Za-z]+", latex_text):+ return f"{latex_text} "+ return latex_text++class Tag2Method:def call_method(self, elm, stag=None):getmethod = self.tag2meth.get@@ -485,8 +492,10 @@ class oMath2Latex(Tag2Method):# 不能删除内部LaTeX命令的闭合花括号。if out_latex_str.startswith(r"{\ensuremath{") and out_latex_str.endswith("}}"):out_latex_str = out_latex_str[len(r"{\ensuremath{") : -2]+ out_latex_str = _ensure_latex_command_boundary(out_latex_str)elif out_latex_str.startswith(r"\ensuremath{") and out_latex_str.endswith("}"):out_latex_str = out_latex_str[len(r"\ensuremath{") : -1]+ out_latex_str = _ensure_latex_command_boundary(out_latex_str)elif (s.startswith("{") is Falseand out_latex_str.startswith("{")