Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: implement HTML rendering for algorithm blocks in markdown processing

myhlolif8db3557cbb7
diff --git a/mineru/backend/utils/markdown_utils.py b/mineru/backend/utils/markdown_utils.py
index f5de61f2..dd8bc7d1 100644
--- a/mineru/backend/utils/markdown_utils.py
+++ b/mineru/backend/utils/markdown_utils.py
@@ -1,6 +1,7 @@
# Copyright (c) Opendatalab. All rights reserved.
import re
+from html import escape
CONSERVATIVE_MARKDOWN_SPECIAL_CHARS = ("*", "_", "`", "~", "$")
TEXT_BLOCK_MARKDOWN_PREFIX_RE = re.compile(
@@ -45,3 +46,51 @@ def escape_text_block_markdown_prefix(content: str) -> str:
marker_start = match.start("marker")
return f"{content[:marker_start]}\\{content[marker_start:]}"
+
+
+def render_algorithm_html_from_lines(
+ lines: list[dict],
+ inline_left_delimiter: str,
+ inline_right_delimiter: str,
+ text_normalizer=None,
+) -> str:
+ """将 algorithm 的行内 span 渲染为 HTML,以同时保留缩进和公式渲染能力。"""
+ html_parts = []
+ previous_span_type = None
+ for line in lines or []:
+ for span in line.get("spans", []):
+ span_type = span.get("type")
+ content = span.get("content", "")
+ if content is None:
+ content = ""
+
+ if span_type == "text":
+ if text_normalizer is not None:
+ content = text_normalizer(content)
+ html_parts.append(escape(str(content), quote=False))
+ if content:
+ previous_span_type = span_type
+ elif span_type == "inline_equation":
+ if str(content).strip():
+ if (
+ previous_span_type == "inline_equation"
+ and html_parts
+ and not html_parts[-1].endswith((" ", "\n", "\t"))
+ ):
+ html_parts.append(" ")
+ html_parts.append(
+ f"{inline_left_delimiter}"
+ f"{escape(str(content), quote=False)}"
+ f"{inline_right_delimiter}"
+ )
+ previous_span_type = span_type
+
+ html_body = "".join(html_parts)
+ if not html_body.strip():
+ return ""
+
+ return (
+ '<div class="mineru-algorithm" style="white-space: pre-wrap; font-family:monospace;">\n'
+ f"{html_body}\n"
+ "</div>"
+ )
diff --git a/mineru/backend/vlm/vlm_middle_json_mkcontent.py b/mineru/backend/vlm/vlm_middle_json_mkcontent.py
index eeab1c57..ecf28af0 100644
--- a/mineru/backend/vlm/vlm_middle_json_mkcontent.py
+++ b/mineru/backend/vlm/vlm_middle_json_mkcontent.py
@@ -1,7 +1,7 @@
# Copyright (c) Opendatalab. All rights reserved.
import os
import re
-from html import escape, unescape
+from html import unescape
from loguru import logger
@@ -12,6 +12,7 @@ from mineru.utils.language import detect_lang
from mineru.backend.utils.markdown_utils import (
escape_conservative_markdown_text,
escape_text_block_markdown_prefix,
+ render_algorithm_html_from_lines,
)
latex_delimiters_config = get_latex_delimiter_config()
@@ -143,11 +144,19 @@ def _get_blocks_in_index_order(blocks):
def _render_code_block_markdown(block, para_block):
- code_text = merge_para_with_text(block)
- if para_block.get('sub_type') == BlockType.CODE:
+ if para_block.get('sub_type') == BlockType.ALGORITHM:
+ return render_algorithm_html_from_lines(
+ block.get('lines', []),
+ inline_left_delimiter,
+ inline_right_delimiter,
+ text_normalizer=_normalize_text_content,
+ )
+ elif para_block.get('sub_type') == BlockType.CODE:
+ code_text = merge_para_with_text(block)
guess_lang = para_block.get('guess_lang', 'txt')
return f"```{guess_lang}\n{code_text}\n```"
- return code_text
+ else:
+ raise ValueError(f"Unknown code block sub_type: {para_block.get('sub_type')}")
def _render_visual_block_segments(block, para_block, img_buket_path='', table_enable=True):
@@ -223,7 +232,12 @@ def _render_visual_block_segments(block, para_block, img_buket_path='', table_en
if block_type == BlockType.CODE_BODY:
block_text = _render_code_block_markdown(block, para_block)
if block_text.strip():
- return [(block_text, 'markdown_line')]
+ segment_kind = (
+ 'html_block'
+ if para_block.get('sub_type') == BlockType.ALGORITHM
+ else 'markdown_line'
+ )
+ return [(block_text, segment_kind)]
return []
return []