package org.dromara.knowledge.data;

import org.dromara.common.core.exception.ServiceException;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

/**
 * HTML 文档解析器
 * 提取 HTML 中的纯文本内容，移除标签和脚本
 *
 * @author pengles
 */
@Component
public class HtmlDocumentParser implements DocumentParser {

    // 匹配 HTML 标签的正则表达式
    private static final Pattern HTML_TAG_PATTERN = Pattern.compile("<[^>]+>");
    // 匹配脚本和样式标签及其内容
    private static final Pattern SCRIPT_STYLE_PATTERN = Pattern.compile(
            "(?i)<(script|style)[^>]*>.*?</\\1>", Pattern.DOTALL);
    // 匹配 HTML 实体
    private static final Pattern HTML_ENTITY_PATTERN = Pattern.compile("&[a-zA-Z]+;|&#\\d+;");

    @Override
    public String parse(byte[] bytes, String filename) {
        try {
            String html = new String(bytes, StandardCharsets.UTF_8);

            // 移除脚本和样式标签及其内容
            html = SCRIPT_STYLE_PATTERN.matcher(html).replaceAll("");

            // 移除 HTML 标签
            html = HTML_TAG_PATTERN.matcher(html).replaceAll(" ");

            // 简单处理 HTML 实体（可以扩展更完整的实体映射）
            html = html.replace("&nbsp;", " ")
                    .replace("&lt;", "<")
                    .replace("&gt;", ">")
                    .replace("&amp;", "&")
                    .replace("&quot;", "\"")
                    .replace("&#39;", "'");

            // 清理多余的空白字符
            html = html.replaceAll("\\s+", " ").trim();

            return html;
        } catch (Exception e) {
            throw new ServiceException("HTML解析失败: " + e.getMessage());
        }
    }
}
