Skip to content

Commit

Permalink
update wiki docs (#5)
Browse files Browse the repository at this point in the history
* update wiki docs and action
  • Loading branch information
SAKURA-CAT authored Jun 17, 2024
1 parent 38f3e06 commit 8c176f7
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 2 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/update-wiki.yml
Original file line number Diff line number Diff line change
@@ -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: [email protected]
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://[email protected]/$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://[email protected]/$OWNER/$REPOSITORY_NAME.wiki.git master
1 change: 0 additions & 1 deletion docs/Home.md

This file was deleted.

1 change: 0 additions & 1 deletion docs/unit-test.md

This file was deleted.

11 changes: 11 additions & 0 deletions docs/wiki/Home.md
Original file line number Diff line number Diff line change
@@ -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)



63 changes: 63 additions & 0 deletions scripts/parse_docs.py
Original file line number Diff line number Diff line change
@@ -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()
73 changes: 73 additions & 0 deletions test/unit/_/scirpts/test_parse_docs.py
Original file line number Diff line number Diff line change
@@ -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)."

0 comments on commit 8c176f7

Please sign in to comment.