|
1 |
| -import json |
2 | 1 | import os
|
3 | 2 | import re
|
4 |
| -import requests |
| 3 | +import yaml |
5 | 4 | from collections import defaultdict
|
6 | 5 |
|
7 | 6 |
|
8 |
| -def load_ratings(): |
9 |
| - res = {} |
10 |
| - if os.path.exists("rating.json"): |
11 |
| - with open("rating.json", "r", encoding="utf-8") as f: |
12 |
| - ratings = json.loads(f.read()) |
13 |
| - for item in ratings: |
14 |
| - res[str(item["ID"])] = item |
15 |
| - |
16 |
| - url = "https://zerotrac.github.io/leetcode_problem_rating/data.json" |
17 |
| - try: |
18 |
| - resp = requests.get(url) |
19 |
| - if resp.status_code == 200: |
20 |
| - ratings = resp.json() |
21 |
| - for item in ratings: |
22 |
| - res[str(item["ID"])] = item |
23 |
| - except Exception as e: |
24 |
| - print(f"Failed to fetch ratings: {e}") |
25 |
| - return res |
26 |
| - |
27 |
| - |
28 |
| -rating_dict = load_ratings() |
| 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 |
29 | 21 |
|
30 | 22 |
|
31 | 23 | for contest_file in ["docs/contest.md", "docs-en/contest.md"]:
|
@@ -108,10 +100,9 @@ def get_paths(dirs: str, m: int):
|
108 | 100 | for p in sorted(get_paths(dir, m)):
|
109 | 101 | # example:
|
110 | 102 | # p = 'solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md'
|
111 |
| - edit_url = f"https://github.com/doocs/leetcode/edit/main/{p}" |
112 | 103 | with open(p, "r", encoding="utf-8") as f:
|
113 | 104 | content = f.read()
|
114 |
| - |
| 105 | + metadata, content = extract_metadata(content) |
115 | 106 | # [中文文档](/lcci/01.01.Is%20Unique/README.md)
|
116 | 107 | # 正则匹配 [中文文档](xxx) 并且移除
|
117 | 108 | content = re.sub(r"\[中文文档]\((.*?)\)", "", content)
|
@@ -144,10 +135,6 @@ def get_paths(dirs: str, m: int):
|
144 | 135 | elif num.endswith("- I"):
|
145 | 136 | num = num[:-3] + ".1"
|
146 | 137 | num = ".".join([x.strip(" ").lstrip("0") for x in num.split(".")])
|
147 |
| - rat = -1 |
148 |
| - if target_dir == "lc" and num in rating_dict: |
149 |
| - rat = int(rating_dict[num]["Rating"]) |
150 |
| - print(f"Rating: {num} {rat}") |
151 | 138 | is_en = "README_EN" in p
|
152 | 139 | if is_en:
|
153 | 140 | navdata_en[dir].append(f" - {num}. {name}: {target_dir}/{num}.md")
|
@@ -179,26 +166,12 @@ def get_paths(dirs: str, m: int):
|
179 | 166 | os.makedirs(docs_dir)
|
180 | 167 | new_path = os.path.join(docs_dir, f"{num}.md")
|
181 | 168 |
|
182 |
| - # 获取 tags |
183 |
| - match = re.search(r"<!-- tags:(.*?) -->", content) |
184 |
| - tag_headers = "" |
185 |
| - if match: |
186 |
| - tags = match.group(1).split(",") |
187 |
| - if tags and tags != [""]: |
188 |
| - tag_headers = "tags:\n" |
189 |
| - tag_headers += "".join([f" - {tag}\n" for tag in tags]) |
190 |
| - tag_headers += "\n" |
191 |
| - |
192 |
| - # 开启评论 |
193 |
| - """ |
194 |
| - --- |
195 |
| - comments: true |
196 |
| - --- |
197 |
| - """ |
198 |
| - content = ( |
199 |
| - f"---\ncomments: true\nedit_url: {edit_url}\n{tag_headers}---\n\n" |
200 |
| - + content |
| 169 | + yaml_metadata = yaml.dump( |
| 170 | + metadata, default_flow_style=False, allow_unicode=True |
201 | 171 | )
|
| 172 | + print(metadata) |
| 173 | + metadata_section = f"---\n{yaml_metadata}---\n\n" |
| 174 | + content = metadata_section + content |
202 | 175 | with open(new_path, "w", encoding="utf-8") as f:
|
203 | 176 | f.write(content)
|
204 | 177 |
|
|
0 commit comments