Skip to content

Commit 63fb256

Browse files
committed
feat: update plugin
1 parent da0745a commit 63fb256

File tree

4 files changed

+65
-79
lines changed

4 files changed

+65
-79
lines changed

hooks/edit_url.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# https://www.mkdocs.org/dev-guide/plugins/#events
55

66

7-
@plugins.event_priority(100)
7+
@plugins.event_priority(80)
88
def on_page_markdown(markdown, page, config, files):
99
page_edit_url = page.meta.get("edit_url")
1010
page.edit_url = str(page_edit_url) if page_edit_url else None

hooks/ext_info.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,31 @@
55

66
# https://www.mkdocs.org/dev-guide/plugins/#events
77

8+
code_dict = {
9+
"py": ("Python3", "python"),
10+
"java": ("Java", "java"),
11+
"cpp": ("C++", "cpp"),
12+
"go": ("Go", "go"),
13+
"ts": ("TypeScript", "ts"),
14+
"rs": ("Rust", "rust"),
15+
"js": ("JavaScript", "js"),
16+
"cs": ("C#", "cs"),
17+
"php": ("PHP", "php"),
18+
"c": ("C", "c"),
19+
"scala": ("Scala", "scala"),
20+
"swift": ("Swift", "swift"),
21+
"rb": ("Ruby", "rb"),
22+
"kt": ("Kotlin", "kotlin"),
23+
"dart": ("Dart", "dart"),
24+
"nim": ("Nim", "nim"),
25+
"sql": ("MySQL", "sql"),
26+
"sh": ("Shell", "bash"),
27+
}
828

9-
@plugins.event_priority(100)
10-
def on_page_markdown(markdown, page, config, files):
29+
mapping = {lang: name for name, lang in code_dict.values()}
30+
31+
32+
def add_difficulty_info(markdown, page):
1133
difficulty = page.meta.get("difficulty")
1234
rating = page.meta.get("rating")
1335
if not difficulty and not rating:
@@ -45,3 +67,41 @@ def on_page_markdown(markdown, page, config, files):
4567
)
4668

4769
return markdown
70+
71+
def modify_code_block(content):
72+
# 修改代码块
73+
while True:
74+
start = "<!-- tabs:start -->"
75+
end = "<!-- tabs:end -->"
76+
i = content.find(start)
77+
j = content.find(end)
78+
if i == -1 or j == -1:
79+
break
80+
j = content.find(end)
81+
codes = content[i + len(start) : j].strip()
82+
res = re.findall(r"```(.*?)\n(.*?)\n```", codes, re.S)
83+
result = []
84+
if res:
85+
for lang, code in res:
86+
name = mapping.get(lang)
87+
code = code or ""
88+
# 需要将 code 缩进 4 个空格
89+
code = code.replace("\n", "\n ")
90+
code_snippet = f'=== "{name}"\n\n ```{lang} linenums="1"\n {code}\n ```\n'
91+
result.append(code_snippet)
92+
content = content[:i] + "\n".join(result) + content[j + len(end) :]
93+
return content
94+
95+
def remove_version_switch(content):
96+
content = re.sub(r"\[中文文档]\((.*?)\)", "", content)
97+
content = re.sub(r"\[English Version]\((.*?)\)", "", content)
98+
return content
99+
100+
101+
@plugins.event_priority(100)
102+
def on_page_markdown(markdown, page, config, files):
103+
markdown = remove_version_switch(markdown)
104+
markdown = add_difficulty_info(markdown, page)
105+
markdown = modify_code_block(markdown)
106+
return markdown
107+

main.py

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
import os
22
import re
3-
import yaml
43
from collections import defaultdict
54

65

7-
def extract_metadata(content: str):
8-
# 检查是否包含 YAML front matter
9-
if content.startswith("---\n"):
10-
# 使用正则表达式匹配 YAML front matter
11-
match = re.match(r"^---\n(.*?)\n---\n", content, re.DOTALL)
12-
if match:
13-
yaml_content = match.group(1)
14-
# 解析 YAML 内容
15-
metadata = yaml.safe_load(yaml_content)
16-
# 获取剩余的 Markdown 内容
17-
remaining_content = content[match.end() :]
18-
return metadata, remaining_content
19-
# 如果没有 metadata,返回空字典和原始内容
20-
return {}, content
21-
22-
236
for contest_file in ["docs/contest.md", "docs-en/contest.md"]:
247
with open(contest_file, "r", encoding="utf-8") as f:
258
content = f.read()
@@ -40,30 +23,6 @@ def extract_metadata(content: str):
4023
f.write(content)
4124

4225

43-
code_dict = {
44-
"py": ("Python3", "python"),
45-
"java": ("Java", "java"),
46-
"cpp": ("C++", "cpp"),
47-
"go": ("Go", "go"),
48-
"ts": ("TypeScript", "ts"),
49-
"rs": ("Rust", "rust"),
50-
"js": ("JavaScript", "js"),
51-
"cs": ("C#", "cs"),
52-
"php": ("PHP", "php"),
53-
"c": ("C", "c"),
54-
"scala": ("Scala", "scala"),
55-
"swift": ("Swift", "swift"),
56-
"rb": ("Ruby", "rb"),
57-
"kt": ("Kotlin", "kotlin"),
58-
"dart": ("Dart", "dart"),
59-
"nim": ("Nim", "nim"),
60-
"sql": ("MySQL", "sql"),
61-
"sh": ("Shell", "bash"),
62-
}
63-
64-
mapping = {lang: name for name, lang in code_dict.values()}
65-
66-
6726
def get_paths(dirs: str, m: int):
6827
paths = []
6928
for root, _, files in os.walk(dirs):
@@ -102,12 +61,6 @@ def get_paths(dirs: str, m: int):
10261
# p = 'solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md'
10362
with open(p, "r", encoding="utf-8") as f:
10463
content = f.read()
105-
metadata, content = extract_metadata(content)
106-
# [中文文档](/lcci/01.01.Is%20Unique/README.md)
107-
# 正则匹配 [中文文档](xxx) 并且移除
108-
content = re.sub(r"\[中文文档]\((.*?)\)", "", content)
109-
content = re.sub(r"\[English Version]\((.*?)\)", "", content)
110-
11164
title = content[content.find("[") + 1 : content.find("]")]
11265
dot = title.find(".") if dir != "lcci" else title.rfind(".")
11366
num = (
@@ -140,38 +93,10 @@ def get_paths(dirs: str, m: int):
14093
navdata_en[dir].append(f" - {num}. {name}: {target_dir}/{num}.md")
14194
else:
14295
navdata_cn[dir].append(f" - {num}. {name}: {target_dir}/{num}.md")
143-
# 修改代码块
144-
while True:
145-
start = "<!-- tabs:start -->"
146-
end = "<!-- tabs:end -->"
147-
i = content.find(start)
148-
j = content.find(end)
149-
if i == -1 or j == -1:
150-
break
151-
j = content.find(end)
152-
codes = content[i + len(start) : j].strip()
153-
res = re.findall(r"```(.*?)\n(.*?)\n```", codes, re.S)
154-
result = []
155-
if res:
156-
for lang, code in res:
157-
name = mapping.get(lang)
158-
code = code or ""
159-
# 需要将 code 缩进 4 个空格
160-
code = code.replace("\n", "\n ")
161-
code_snippet = f'=== "{name}"\n\n ```{lang} linenums="1"\n {code}\n ```\n'
162-
result.append(code_snippet)
163-
content = content[:i] + "\n".join(result) + content[j + len(end) :]
16496
docs_dir = ("docs-en" if is_en else "docs") + os.sep + target_dir
16597
if not os.path.exists(docs_dir):
16698
os.makedirs(docs_dir)
16799
new_path = os.path.join(docs_dir, f"{num}.md")
168-
169-
yaml_metadata = yaml.dump(
170-
metadata, default_flow_style=False, allow_unicode=True
171-
)
172-
print(metadata)
173-
metadata_section = f"---\n{yaml_metadata}---\n\n"
174-
content = metadata_section + content
175100
with open(new_path, "w", encoding="utf-8") as f:
176101
f.write(content)
177102

mkdocs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,5 @@ nav:
447447
- 17.25. 单词矩阵: lcci/17.25.md
448448
- 17.26. 稀疏相似度: lcci/17.26.md
449449

450-
- 专项训练: tags.md
450+
- 专项训练: tags.md
451+
- 竞赛: contest.md

0 commit comments

Comments
 (0)