From 8c176f7ddf4dc78954e3593cb3d55ad80f5ccbe1 Mon Sep 17 00:00:00 2001 From: KAAANG <79990647+SAKURA-CAT@users.noreply.github.com> Date: Mon, 17 Jun 2024 17:05:06 +0800 Subject: [PATCH] update wiki docs (#5) * update wiki docs and action --- .github/workflows/update-wiki.yml | 53 +++++++++++++++++++ docs/Home.md | 1 - docs/unit-test.md | 1 - docs/wiki/Home.md | 11 ++++ scripts/parse_docs.py | 63 ++++++++++++++++++++++ test/unit/_/scirpts/test_parse_docs.py | 73 ++++++++++++++++++++++++++ 6 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/update-wiki.yml delete mode 100644 docs/Home.md delete mode 100644 docs/unit-test.md create mode 100644 docs/wiki/Home.md create mode 100644 scripts/parse_docs.py create mode 100644 test/unit/_/scirpts/test_parse_docs.py diff --git a/.github/workflows/update-wiki.yml b/.github/workflows/update-wiki.yml new file mode 100644 index 0000000..a241a6e --- /dev/null +++ b/.github/workflows/update-wiki.yml @@ -0,0 +1,53 @@ +name: Update Wiki + +on: + push: + paths: + - docs/** + branches: + - main +env: + USER_TOKEN: ${{ secrets.WIKI_ACTION_TOKEN }} # This is the repository secret + USER_NAME: wiki-bot + USER_EMAIL: team@swanhub.co + OWNER: SwanHubX + REPOSITORY_NAME: SwanLab-Toolkit + +jobs: + publish_docs_to_wiki: + name: Publish docs to Wiki + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set Python + uses: actions/setup-python@v5 + with: + python-version: "3.8" + + - name: Run parser + run: | + python3 scripts/parse_docs.py docs/wiki/ + + # 1. Create folder named `tmp_wiki` + # 2. Initialize Git + # 3. Pull old Wiki content + - name: Pull content from wiki + run: | + mkdir tmp_wiki + cd tmp_wiki + git init + git config user.name $USER_NAME + git config user.email $USER_EMAIL + git pull https://$USER_TOKEN@github.com/$OWNER/$REPOSITORY_NAME.wiki.git + + # 4. Synchronize differences between `docs` & `tmp_wiki` + # 5. Push new Wiki content + - name: Push content to wiki + run: | + rsync -av --delete docs/ tmp_wiki/ --exclude .git + cd tmp_wiki + git add . + git commit -m "Update Wiki content" + git push -f --set-upstream https://$USER_TOKEN@github.com/$OWNER/$REPOSITORY_NAME.wiki.git master \ No newline at end of file diff --git a/docs/Home.md b/docs/Home.md deleted file mode 100644 index c7814c6..0000000 --- a/docs/Home.md +++ /dev/null @@ -1 +0,0 @@ -Welcome to the SwanLab-Toolkit wiki! diff --git a/docs/unit-test.md b/docs/unit-test.md deleted file mode 100644 index 5884e2a..0000000 --- a/docs/unit-test.md +++ /dev/null @@ -1 +0,0 @@ -# 单元测试 \ No newline at end of file diff --git a/docs/wiki/Home.md b/docs/wiki/Home.md new file mode 100644 index 0000000..1ab4f8f --- /dev/null +++ b/docs/wiki/Home.md @@ -0,0 +1,11 @@ +欢迎来到SwanLab-Toolkit wiki!SwanLab-Toolkit是为[SwanLab](https://github.com/SwanHubX/SwanLab) +定制的工具软件包,初衷是为了更好的维护和管理SwanLab。 + +在本wiki,你可以查阅到当前本仓库提供的一些API功能,帮助开发和维护SwanLab。 +如果你希望更好的了解本仓库,可以查看swankit的[源码](https://github.com/SwanHubX/SwanLab-Toolkit) + +> +出于一些考虑,本wiki只收录API的使用方法,如果需要查看更详细的文档,建议移步[docs](https://github.com/SwanHubX/SwanLab-Toolkit/tree/main/docs) + + + diff --git a/scripts/parse_docs.py b/scripts/parse_docs.py new file mode 100644 index 0000000..139367f --- /dev/null +++ b/scripts/parse_docs.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +r""" +@DATE: 2024/6/17 16:18 +@File: parse-docs.py +@IDE: pycharm +@Description: + 主要是为了与wiki的路径保持一致,创建此解析脚本 +""" +import re +import os +import argparse + + +def parse(raw): + # 定义正则表达式来匹配以 /docs 开头的链接 + pattern = re.compile(r'\[(.*?)]\((/docs/.*?\.md)\)') + + # 使用正则表达式替换匹配的链接 + def replace_link(match): + text = match.group(1) + link = match.group(2) + # 删除 /docs 和 .md 后缀 + new_link = link.replace('/docs', '').replace('.md', '') + return f'[{text}]({new_link})' + + # 替换所有匹配的链接 + result = pattern.sub(replace_link, raw) + return result + + +def main(): + # 创建一个解析器对象 + parser = argparse.ArgumentParser(description='Parse links in a markdown file.') + + # 添加一个位置参数 + parser.add_argument('path', type=str, help='The path to the folder') + + # 解析命令行参数 + args = parser.parse_args() + + # 遍历文件夹中的所有文件 + for root, dirs, files in os.walk(args.path): + for file in files: + if not file.endswith('.md'): + continue + # 拼接文件的完整路径 + file_path = os.path.join(root, file) + + # 读取文件内容 + with open(file_path, 'r') as f: + raw = f.read() + + # 解析文件内容 + parsed = parse(raw) + + # 写入解析后的内容 + with open(file_path, 'w') as f: + f.write(parsed) + + +if __name__ == '__main__': + main() diff --git a/test/unit/_/scirpts/test_parse_docs.py b/test/unit/_/scirpts/test_parse_docs.py new file mode 100644 index 0000000..294b628 --- /dev/null +++ b/test/unit/_/scirpts/test_parse_docs.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +r""" +@DATE: 2024/6/17 16:32 +@File: test_parse_docs.py +@IDE: pycharm +@Description: + 测试parse_docs.py +""" +import os +from scripts.parse_docs import parse, main +from tutils import TEMP_DIR +from unittest.mock import patch +import argparse +import pytest + + +@pytest.mark.parametrize("raw, expected", [ + ( + "Here is a link to [some document](/docs/some/path/to/document.md).", + "Here is a link to [some document](/some/path/to/document)." + ), + ( + "And another link to [another document](/docs/another/path/to/document.md).", + "And another link to [another document](/another/path/to/document)." + ), + ( + "No change to [normal link](https://example.com).", + "No change to [normal link](https://example.com)." + ), + ( + "Multiple links [doc1](/docs/doc1.md) and [doc2](/docs/doc2.md).", + "Multiple links [doc1](/doc1) and [doc2](/doc2)." + ), + ( + "Empty link []().", + "Empty link []()." + ), + ( + "Link with no extension [doc](/docs/doc).", + "Link with no extension [doc](/docs/doc)." + ), + ( + "Link with different extension [doc](/docs/doc.txt).", + "Link with different extension [doc](/docs/doc.txt)." + ), +]) +def test_parse(raw, expected): + """ + 测试parse函数 + """ + assert parse(raw) == expected + + +def test_main(): + """ + 测试main函数 + """ + path = os.path.join(TEMP_DIR, "test_parse_docs") + os.makedirs(path) + # ---------------------------------- 写入一些文件 ---------------------------------- + with open(os.path.join(path, "test1.md"), "w") as f: + f.write("Here is a link to [some document](/docs/some/path/to/document.md).") + with open(os.path.join(path, "test2.md"), "w") as f: + f.write("And another link to [another document](/docs/another/path/to/document.md).") + + with patch("argparse.ArgumentParser.parse_args", return_value=argparse.Namespace(path=path)): + main() + + with open(os.path.join(path, "test1.md"), "r") as f: + assert f.read() == "Here is a link to [some document](/some/path/to/document)." + with open(os.path.join(path, "test2.md"), "r") as f: + assert f.read() == "And another link to [another document](/another/path/to/document)."