# -*- coding: utf-8 -*- # # Copyright 2025 The InfiniFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from rag.nlp import find_codec, rag_tokenizer import uuid import chardet from bs4 import BeautifulSoup, NavigableString, Tag, Comment import html def get_encoding(file): with open(file,'rb') as f: tmp = chardet.detect(f.read()) return tmp['encoding'] BLOCK_TAGS = [ "h1", "h2", "h3", "h4", "h5", "h6", "p", "div", "article", "section", "aside", "ul", "ol", "li", "table", "pre", "code", "blockquote", "figure", "figcaption" ] TITLE_TAGS = {"h1": "#", "h2": "##", "h3": "###", "h4": "#####", "h5": "#####", "h6": "######"} class RAGFlowHtmlParser: def __call__(self, fnm, binary=None, chunk_token_num=None): if binary: encoding = find_codec(binary) txt = binary.decode(encoding, errors="ignore") else: with open(fnm, "r",encoding=get_encoding(fnm)) as f: txt = f.read() return self.parser_txt(txt, chunk_token_num) @classmethod def parser_txt(cls, txt, chunk_token_num): if not isinstance(txt, str): raise TypeError("txt type should be string!") temp_sections = [] soup = BeautifulSoup(txt, "html5lib") # delete