2023-12-14 10:09:40 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import mistune
|
|
|
|
|
2023-12-14 10:13:51 +08:00
|
|
|
from qtoml.encoder import dumps
|
|
|
|
from qtoml.decoder import loads
|
|
|
|
from lib_not_dr import loggers
|
|
|
|
from lib_not_dr.types.options import Options
|
2023-12-14 10:09:40 +08:00
|
|
|
|
|
|
|
|
2023-12-14 10:13:51 +08:00
|
|
|
ast_markdown = mistune.create_markdown(renderer='ast')
|
2023-12-14 10:09:40 +08:00
|
|
|
|
|
|
|
|
2023-12-14 10:13:51 +08:00
|
|
|
class TagParser(Options):
|
|
|
|
module_root: Path
|
|
|
|
tags: dict[str, list[str]] = {}
|
|
|
|
logger: loggers.logger.Logger = None # noqa
|
2023-12-14 10:09:40 +08:00
|
|
|
|
2023-12-14 10:13:51 +08:00
|
|
|
def load_module(self, **kwargs):
|
|
|
|
for readme in self.module_root.rglob('readme.md'):
|
|
|
|
self.logger.debug(readme.absolute(), tag="load file")
|
|
|
|
self.get_module_data(readme)
|
|
|
|
|
|
|
|
tag_toml = dumps(self.tags)
|
|
|
|
tag_path = self.module_root / ".." / "build" / "tags.toml"
|
|
|
|
tag_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
tag_path.touch(exist_ok=True)
|
|
|
|
with open(tag_path, 'w', encoding='utf-8') as file:
|
|
|
|
file.write(tag_toml)
|
|
|
|
|
2023-12-14 10:09:40 +08:00
|
|
|
|
2023-12-14 10:13:51 +08:00
|
|
|
def get_module_data(self, module_path: Path):
|
|
|
|
with open(module_path, 'r', encoding='utf-8') as f:
|
|
|
|
file = f.read()
|
|
|
|
|
|
|
|
ast = ast_markdown(file)
|
|
|
|
if len(ast) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if ast[0] != {'type': 'thematic_break'}:
|
|
|
|
# 排除开头不是注释块的
|
|
|
|
return
|
|
|
|
|
|
|
|
self.logger.info(f"开始解析 {ast[1]}")
|
|
|
|
config_code = ast[1].get("raw", "")
|
|
|
|
config_dict = loads(config_code)
|
|
|
|
self.logger.trace(config_dict)
|
|
|
|
|
|
|
|
if not (tag_list := config_dict.get('tags')):
|
|
|
|
self.logger.warn("未找到 tags", tag=str(module_path))
|
|
|
|
return
|
|
|
|
|
|
|
|
for tag in tag_list:
|
|
|
|
if tag not in self.tags:
|
|
|
|
self.tags[tag] = [module_path.__str__()]
|
|
|
|
else:
|
|
|
|
self.tags[tag].append(module_path.__str__())
|
|
|
|
|
|
|
|
def init(self, **kwargs) -> bool:
|
|
|
|
self.logger = loggers.get_logger()
|
|
|
|
self.logger.global_level = 0
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = TagParser(module_root = Path("modules"))
|
|
|
|
parser.load_module()
|
|
|
|
parser.logger.info(parser.tags)
|