Skip to content

Commit fadf0ea

Browse files
committed
config: repo and mkdocs config update
1 parent 0d9954c commit fadf0ea

6 files changed

Lines changed: 243 additions & 3 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
.DS_Store
2+
3+
.obsidian
4+
5+
*.png

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v3.2.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-added-large-files

convert_img.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import os
2+
import re
3+
from pathlib import Path
4+
from PIL import Image
5+
import argparse
6+
7+
def convert_image_to_webp(input_path, output_path):
8+
"""
9+
将图片转换为WebP格式(无损压缩)
10+
"""
11+
try:
12+
with Image.open(input_path) as img:
13+
# 如果是PNG且有透明通道,保持RGBA模式
14+
if img.format == 'PNG' and img.mode in ('RGBA', 'LA'):
15+
img.save(output_path, 'WEBP', lossless=True, quality=100)
16+
else:
17+
# 转换为RGB模式(适用于JPG等格式)
18+
if img.mode != 'RGB':
19+
img = img.convert('RGB')
20+
img.save(output_path, 'WEBP', lossless=True, quality=100)
21+
return True
22+
except Exception as e:
23+
print(f"转换失败 {input_path}: {e}")
24+
return False
25+
26+
def scan_and_convert_images(folder_path, keep_original=False):
27+
"""
28+
扫描img文件夹并转换图片
29+
"""
30+
img_folder = Path(folder_path) / 'img'
31+
if not img_folder.exists():
32+
print(f"未找到img文件夹: {img_folder}")
33+
return []
34+
35+
converted_files = []
36+
supported_formats = {'.png', '.jpg', '.jpeg'}
37+
38+
for img_file in img_folder.iterdir():
39+
if img_file.suffix.lower() in supported_formats:
40+
webp_file = img_file.with_suffix('.webp')
41+
42+
print(f"正在转换: {img_file.name} -> {webp_file.name}")
43+
44+
if convert_image_to_webp(img_file, webp_file):
45+
converted_files.append({
46+
'original': img_file.name,
47+
'converted': webp_file.name,
48+
'original_suffix': img_file.suffix.lower()
49+
})
50+
# 根据选项决定是否删除原文件
51+
if not keep_original:
52+
try:
53+
img_file.unlink()
54+
print(f"已删除原文件: {img_file.name}")
55+
except Exception as e:
56+
print(f"删除原文件失败 {img_file.name}: {e}")
57+
else:
58+
print(f"已保留原文件: {img_file.name}")
59+
60+
return converted_files
61+
62+
def update_markdown_references(folder_path, converted_files):
63+
"""
64+
更新Markdown文件中的图片引用
65+
"""
66+
folder = Path(folder_path)
67+
md_files = list(folder.glob('*.md'))
68+
69+
if not md_files:
70+
print("未找到Markdown文件")
71+
return
72+
73+
# 创建替换映射
74+
replacement_map = {}
75+
for file_info in converted_files:
76+
original_name = file_info['original']
77+
converted_name = file_info['converted']
78+
# 移除扩展名的原始名称
79+
base_name = Path(original_name).stem
80+
original_suffix = file_info['original_suffix']
81+
82+
# 创建多种可能的引用模式
83+
patterns = [
84+
f"img/{original_name}",
85+
f"./img/{original_name}",
86+
f"img/{base_name}{original_suffix}",
87+
f"./img/{base_name}{original_suffix}"
88+
]
89+
90+
for pattern in patterns:
91+
replacement_map[pattern] = f"img/{converted_name}"
92+
93+
# 更新每个Markdown文件
94+
for md_file in md_files:
95+
try:
96+
with open(md_file, 'r', encoding='utf-8') as f:
97+
content = f.read()
98+
99+
original_content = content
100+
updated = False
101+
102+
# 替换图片引用
103+
for old_ref, new_ref in replacement_map.items():
104+
# 匹配Markdown图片语法: ![alt](path) 和 <img src="path">
105+
patterns = [
106+
(rf'(\!\[[^\]]*\]\()\s*{re.escape(old_ref)}\s*(\))', rf'\1{new_ref}\2'),
107+
(rf'(<img[^>]+src=["\'\'"])\s*{re.escape(old_ref)}\s*(["\'\'"][^>]*>)', rf'\1{new_ref}\2')
108+
]
109+
110+
for pattern, replacement in patterns:
111+
if re.search(pattern, content):
112+
content = re.sub(pattern, replacement, content)
113+
updated = True
114+
115+
# 如果有更新,写回文件
116+
if updated:
117+
with open(md_file, 'w', encoding='utf-8') as f:
118+
f.write(content)
119+
print(f"已更新Markdown文件: {md_file.name}")
120+
121+
except Exception as e:
122+
print(f"更新Markdown文件失败 {md_file}: {e}")
123+
124+
def main():
125+
parser = argparse.ArgumentParser(description='将PNG/JPG图片转换为WebP格式并更新Markdown引用')
126+
parser.add_argument('--folder', required=True, help='包含img文件夹和Markdown文件的目标文件夹路径')
127+
parser.add_argument('--keep-original', action='store_true', help='保留原始图片文件,不删除')
128+
129+
args = parser.parse_args()
130+
folder_path = Path(args.folder)
131+
132+
if not folder_path.exists():
133+
print(f"错误: 文件夹不存在 - {folder_path}")
134+
return
135+
136+
if not folder_path.is_dir():
137+
print(f"错误: 路径不是文件夹 - {folder_path}")
138+
return
139+
140+
print(f"开始处理文件夹: {folder_path}")
141+
print("=" * 50)
142+
143+
# 步骤1: 转换图片
144+
print("1. 扫描并转换图片...")
145+
converted_files = scan_and_convert_images(folder_path, args.keep_original)
146+
147+
if not converted_files:
148+
print("未找到需要转换的图片文件")
149+
return
150+
151+
print(f"\n成功转换 {len(converted_files)} 个文件:")
152+
for file_info in converted_files:
153+
print(f" {file_info['original']} -> {file_info['converted']}")
154+
155+
# 步骤2: 更新Markdown文件
156+
print("\n2. 更新Markdown文件中的图片引用...")
157+
update_markdown_references(folder_path, converted_files)
158+
159+
print("\n" + "=" * 50)
160+
print("转换完成!")
161+
162+
if __name__ == "__main__":
163+
main()

docs/stylesheets/counter.css

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
h1 {
2+
counter-reset: h2;
3+
}
4+
h2 {
5+
counter-reset: h3;
6+
}
7+
h3 {
8+
counter-reset: h4;
9+
}
10+
h4 {
11+
counter-reset: h5;
12+
}
13+
h5 {
14+
counter-reset: h6;
15+
}
16+
h2:before {
17+
counter-increment: h2;
18+
content: counter(h2);
19+
margin-right: 0.8rem;
20+
}
21+
h3:before {
22+
counter-increment: h3;
23+
content: counter(h2) "." counter(h3);
24+
margin-right: 0.8rem;
25+
}
26+
h4:before {
27+
counter-increment: h4;
28+
content: counter(h2) "." counter(h3) "." counter(h4);
29+
margin-right: 0.8rem;
30+
}
31+
h5:before {
32+
counter-increment: h5;
33+
content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5);
34+
margin-right: 0.8rem;
35+
}
36+
h6:before {
37+
counter-increment: h6;
38+
content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6);
39+
margin-right: 0.8rem;
40+
}

docs/stylesheets/extra.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
/* 隐藏纵向公式滚动条 */
3+
.md-typeset div.arithmatex {
4+
overflow-y: hidden;
5+
overflow-x: auto;
6+
}

mkdocs.yml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ nav:
55
- system/index.md
66
- MLSys:
77
- mlsys/index.md
8+
- AI:
9+
- ai/index.md
10+
- Diffusion:
11+
- Introduction: ai/diffusion/index.md
12+
- "Lec1: Flow and Diffusion Models": ai/diffusion/lec1.md
13+
- "Lec2: Constructing a Training Target": ai/diffusion/lec2.md
14+
- "Lec3: Training Flow and Diffusion Models": ai/diffusion/lec3.md
15+
- "Lec4: Building an Image Generator": ai/diffusion/lec4.md
16+
- RL:
17+
- ai/rl/index.md
18+
- 杂项:
19+
- misc/index.md
820

921
site_name: HarryZ の Notebook
1022
site_url: https://note.uqbarz.com/
1123
repo_url: https://github.com/hharryz/hharryz.github.io
1224
repo_name: hharryz.github.io
1325
site_author: HarryZ
14-
copyright: HarryZ
26+
copyright: Copyright © HarryZ
1527

1628
theme:
1729
name: material
@@ -44,11 +56,12 @@ theme:
4456
- navigation.path
4557
- navigation.top
4658
- toc.follow
59+
- content.action.view
4760
- search.suggest
4861
- search.highlight
4962
- search.share
5063

51-
markdown_extensions:
64+
markdown_extensions:
5265
- admonition
5366
- pymdownx.details
5467
- pymdownx.superfences:
@@ -63,6 +76,9 @@ markdown_extensions:
6376
- pymdownx.inlinehilite
6477
- pymdownx.snippets
6578
- pymdownx.superfences
79+
- pymdownx.caret
80+
- pymdownx.mark
81+
- pymdownx.tilde
6682
- pymdownx.tabbed:
6783
alternate_style: true
6884
- attr_list
@@ -94,8 +110,10 @@ extra_javascript:
94110
extra_css:
95111
- stylesheets/fonts.css
96112
- stylesheets/theme.css
113+
- stylesheets/counter.css
114+
- stylesheets/extra.css
97115

98-
plugins:
116+
plugins:
99117
- search
100118
- git-revision-date-localized:
101119
enable_creation_date: true

0 commit comments

Comments
 (0)