Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions llm_web_kit/main_html_parser/simplify_html/simplify_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
# '-header', '_header', # 有特例,可能自定义的header中有标题,先注释
}

# 自定义标签
tail_block_tag = 'cc-alg-uc-text'


def add_data_uids(dom: html.HtmlElement) -> None:
"""为DOM所有节点添加data-uid属性(递归所有子节点)"""
Expand Down Expand Up @@ -762,7 +765,7 @@ def process_paragraphs(paragraphs: List[Dict[str, str]], uid_map: Dict[str, html
# trailing_text = last_child.tail

# 创建wrapper元素
wrapper = etree.Element('cc-alg-uc-tex')
wrapper = etree.Element(tail_block_tag)
wrapper.set('_item_id', current_id)

# 设置前面的文本
Expand Down Expand Up @@ -798,7 +801,7 @@ def process_paragraphs(paragraphs: List[Dict[str, str]], uid_map: Dict[str, html
# 检查父节点的text
if original_parent.text and original_parent.text.strip() == root_for_xpath.text.strip():
# 创建wrapper
wrapper = etree.Element('cc-alg-uc-tex')
wrapper = etree.Element(tail_block_tag)
wrapper.set('_item_id', current_id)
wrapper.text = original_parent.text

Expand All @@ -818,7 +821,7 @@ def process_paragraphs(paragraphs: List[Dict[str, str]], uid_map: Dict[str, html
for child in original_parent.iterchildren():
if child.tail and child.tail.strip() == root_for_xpath.text.strip():
# 创建wrapper
wrapper = etree.Element('cc-alg-uc-tex')
wrapper = etree.Element(tail_block_tag)
wrapper.set('_item_id', current_id)
wrapper.text = child.tail

Expand All @@ -830,21 +833,8 @@ def process_paragraphs(paragraphs: List[Dict[str, str]], uid_map: Dict[str, html
index = parent.index(child)
parent.insert(index + 1, wrapper)

found = True
break

# 如果没有找到匹配的文本节点,使用父节点作为包裹对象
if not found:
wrapper = etree.Element('cc-alg-uc-tex')
wrapper.set('_item_id', current_id)
wrapper.text = root_for_xpath.text
# 将父节点的内容移动到wrapper中
for child in list(original_parent.iterchildren()):
wrapper.append(child)
original_parent.remove(child)

# 添加wrapper到父节点
original_parent.append(wrapper)
else:
# 块级元素直接设置属性
original_parent.set('_item_id', current_id)
Expand Down