-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
每次 Push 或 PR 到 main 分支都触发。详情见README.md
- Loading branch information
Showing
4 changed files
with
207 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Deploy MkDocs | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
build-and-deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out the repo | ||
uses: actions/checkout@main | ||
|
||
- name: Setup Python | ||
uses: actions/setup-python@main | ||
with: | ||
python-version: '3.x' | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install mkdocs mkdocs-material | ||
- name: Update dir | ||
run: python ./update_dir.py | ||
|
||
- name: Deploy docs to GitHub Pages | ||
uses: mhausenblas/mkdocs-deploy-gh-pages@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
site_name: 重庆理工大学课程攻略共享计划 | ||
site_author: H·Sofie, Royfor12 | ||
site_description: 使用MKDocs构建的静态网页 | ||
site_url: https://Royfor12.github.io/CQUT-Course-Guide-Sharing-Scheme | ||
repo_url: https://github.com/Royfor12/CQUT-Course-Guide-Sharing-Scheme | ||
|
||
theme: | ||
name: material | ||
language: zh | ||
font: | ||
text: Noto Sans Simplified Chinese | ||
features: | ||
- navigation.instant # 即时加载 | ||
- navigation.instant.prefetch # 即时预取 | ||
- navigation.instant.progress # 加载进度指示器 | ||
- navigation.instant.preview # 即时预览 | ||
- navigation.sections # 导航栏分组 | ||
- navigation.path # 标题导航路径 | ||
- navigation.top # 返回顶部按钮 | ||
- toc.follow # 目录锚点跟随 | ||
- search.highlight # 搜索高亮显示 | ||
- content.footnote.tooltips # 脚注内联 | ||
|
||
plugins: | ||
- search: | ||
lang: zh | ||
|
||
markdown_extensions: | ||
- abbr | ||
- admonition | ||
- pymdownx.details | ||
- attr_list | ||
- def_list | ||
- footnotes | ||
- md_in_html | ||
- tables | ||
- pymdownx.betterem | ||
- pymdownx.caret | ||
- pymdownx.mark | ||
- pymdownx.tilde | ||
- pymdownx.tasklist: | ||
custom_checkbox: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import shutil | ||
from urllib.parse import quote | ||
|
||
# 从 GitHub Actions 环境变量里获取仓库所有者和仓库名、分支名 | ||
repo_full = os.environ.get("GITHUB_REPOSITORY", "defaultOwner/defaultRepo") | ||
branch = os.environ.get("GITHUB_REF_NAME", "main") | ||
|
||
# 文本文件走 blob 链接(在线查看),二进制文件走 cdn 链接(便于直接下载) | ||
BLOB_URL_PREFIX = f"https://github.com/{repo_full}/blob/{branch}/" | ||
BIN_URL_PREFIX = f"https://cdn.jsdmirror.com/gh/{repo_full}/" | ||
|
||
# 顶层排除目录 | ||
EXCLUDE_TOP_DIRS = {'.git', 'docs', '.vscode', '.circleci', 'site', 'image'} | ||
|
||
# 视为 README 的文件名 | ||
README_CANDIDATES = {'README.md', 'readme.md', 'index.md'} | ||
|
||
# 生成 blob 链接 | ||
BLOB_EXTS = {'md', 'txt', 'c', 'cpp', 'py'} | ||
|
||
def process_directory(base_dir: str, rel_path: str): | ||
""" | ||
将 base_dir(绝对路径) 对应的目录内容,递归生成到 docs/rel_path 目录下的 index.md 中。 | ||
1. 若有 README_CANDIDATES 中任意文件,则将其内容原样写入。 | ||
2. 新起一行后,输出大标题 "# 文件列表"。 | ||
3. 列出当前目录所有文件的链接(md/txt -> blob,其它 -> cdn)。 | ||
4. 对各子文件夹,递归调用 process_directory,生成各自的 docs/rel_path/subfolder/index.md。 | ||
""" | ||
|
||
# 在 docs/ 下创建与原目录相同层级的文件夹 | ||
docs_folder = os.path.join("docs", rel_path) | ||
if not os.path.exists(docs_folder): | ||
os.makedirs(docs_folder) | ||
|
||
items = sorted(os.listdir(base_dir)) | ||
|
||
# 尝试找到任意一个 README | ||
readme_content = "" | ||
for candidate in README_CANDIDATES: | ||
if candidate in items: | ||
readme_path = os.path.join(base_dir, candidate) | ||
with open(readme_path, "r", encoding="utf-8") as rf: | ||
readme_content = rf.read() | ||
break # 找到一个就可以退出 | ||
|
||
# 收集文件链接列表 | ||
file_links = [] | ||
subdirs = [] | ||
for item in items: | ||
full_item_path = os.path.join(base_dir, item) | ||
if os.path.isfile(full_item_path): | ||
# 若是 README,则已处理过;否则纳入链接 | ||
if item not in README_CANDIDATES: | ||
ext = item.split(".")[-1].lower() if "." in item else "" | ||
# 判断用 blob 还是 cdn 加速 | ||
if ext in BLOB_EXTS: | ||
file_url = BLOB_URL_PREFIX + quote(f"{rel_path}/{item}") | ||
else: | ||
file_url = BIN_URL_PREFIX + quote(f"{rel_path}/{item}") | ||
file_links.append((item, file_url)) | ||
else: | ||
# 是子目录 | ||
subdirs.append(item) | ||
|
||
# 写出当前目录的 index.md | ||
index_md_path = os.path.join(docs_folder, "index.md") | ||
with open(index_md_path, "w", encoding="utf-8") as wf: | ||
# 1) 写 README 内容(若有) | ||
if readme_content.strip(): | ||
wf.write(readme_content.strip()) | ||
wf.write("\n\n---\n\n") # 加点空行,避免直接跟标题混在一起 | ||
|
||
# 2) 输出大标题“# 文件列表” | ||
wf.write("# 文件列表\n") | ||
|
||
# 3) 列出当前目录内的文件链接 | ||
for fname, url in file_links: | ||
wf.write(f"- [{fname}]({url})\n") | ||
wf.write("\n") | ||
|
||
# 4) 递归处理子目录 | ||
for subdir in subdirs: | ||
sub_rel_path = os.path.join(rel_path, subdir) | ||
full_subdir_path = os.path.join(base_dir, subdir) | ||
process_directory(full_subdir_path, sub_rel_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
# 确保 docs 目录存在 | ||
if not os.path.exists("docs"): | ||
os.mkdir("docs") | ||
|
||
# 排除 EXCLUDE_TOP_DIRS | ||
top_dirs = [] | ||
for d in sorted(os.listdir(".")): | ||
if os.path.isdir(d) and d not in EXCLUDE_TOP_DIRS: | ||
top_dirs.append(d) | ||
|
||
for d in top_dirs: | ||
# 绝对路径 | ||
abs_path = os.path.abspath(d) | ||
# rel_path 就是顶层目录的名字 d | ||
process_directory(abs_path, d) | ||
|
||
# 若仓库根目录下存在 README.md,则复制到 docs/index.md 当主页 | ||
if os.path.exists("README.md"): | ||
shutil.copyfile("README.md", "docs/index.md") |