# /*
#  * Copyright 2024 KylinSoft Co., Ltd.
#  *
#  * This program is free software: you can redistribute it and/or modify it under
#  * the terms of the GNU General Public License as published by the Free Software
#  * Foundation, either version 3 of the License, or (at your option) any later
#  * version.
#  *
#  * This program is distributed in the hope that it will be useful, but WITHOUT
#  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#  * details.
#  *
#  * You should have received a copy of the GNU General Public License along with
#  * this program. If not, see <https://www.gnu.org/licenses/>.
#  */

from bs4 import BeautifulSoup
from .base_parser import BaseParser

class HTMLParser(BaseParser):

    def parser(self, file_path) -> list:
        """
        :param file_path: 文件路径
        :return: 按行保存的文本内容列表
        """
        with open(file_path, 'r', encoding='utf-8') as file:
            html_content = file.read()

        soup = BeautifulSoup(html_content, 'html.parser')
        text = soup.get_text(separator=' ')
        text = ''.join(text.split())

        return [text]
    def parser_tag(self, file_path, tag_name, attrs=None):
        with open(file_path, 'r', encoding='utf-8') as file:
            html_content = file.read()
        soup = BeautifulSoup(html_content, 'html.parser')
        tags = soup.find_all(tag_name, attrs=attrs)
        return [tag.get_text(strip=True) for tag in tags]

if __name__ == '__main__':
    file_path = '/home/kylin/wangyan/test-search/touchgesture.xml'
    html_parser = HTMLParser()
    html_content = html_parser.parser(file_path)
    # print(html_parser.parser_tag(file_path, 'keys'))
    print(html_content)