|
| 1 | +# Post-Processing Solution Links 检查报告 |
| 2 | + |
| 3 | +## 处理流程 |
| 4 | + |
| 5 | +后处理按以下顺序执行(`PostProcessor.process()` 方法): |
| 6 | + |
| 7 | +### Step 1: 文本替换 |
| 8 | +- `LC 11` → `LeetCode 11` |
| 9 | +- `LC-11` → `LeetCode 11` |
| 10 | + |
| 11 | +### Step 2: 转换纯文本为链接 (`_convert_plain_leetcode_to_links`) |
| 12 | +- 处理现有链接:`[LeetCode 79 - Word Search](wrong_url)` → `[LeetCode 79](correct_url)` |
| 13 | +- 处理纯文本:`LeetCode 79` → `[LeetCode 79](url)` |
| 14 | +- 使用 `meta/problems/*.toml` 中的 URL 数据 |
| 15 | + |
| 16 | +### Step 3: 规范化 LeetCode 链接 (`_normalize_leetcode_links`) |
| 17 | +- 修复 URL 格式,确保以 `/description/` 结尾 |
| 18 | +- 例如:`https://leetcode.com/problems/word-search/` → `https://leetcode.com/problems/word-search/description/` |
| 19 | + |
| 20 | +### Step 4: 添加 Solution 链接 (`_add_github_solution_links`) |
| 21 | +- 查找所有 `[LeetCode {id}](url)` 格式的链接 |
| 22 | +- 如果该问题在 `meta/problems/*.toml` 中有 `[files].solution` 字段 |
| 23 | +- 添加 Solution 链接:`[LeetCode 79](url) | [Solution](github_url)` |
| 24 | + |
| 25 | +## Solution 链接处理逻辑 |
| 26 | + |
| 27 | +### 1. 问题查找 (`_add_github_solution_links`) |
| 28 | + |
| 29 | +```python |
| 30 | +# 从链接文本中提取问题ID |
| 31 | +id_match = re.search(r'LeetCode\s+(\d+)', link_text) |
| 32 | +problem_id = id_match.group(1) # 例如: "79" |
| 33 | + |
| 34 | +# 尝试多种ID格式查找 |
| 35 | +lookup_keys = [ |
| 36 | + problem_id.zfill(4), # "0079" |
| 37 | + problem_id, # "79" |
| 38 | + str(int(problem_id)).zfill(4), # "0079" (normalized) |
| 39 | + str(int(problem_id)) # "79" (normalized) |
| 40 | +] |
| 41 | + |
| 42 | +for key in lookup_keys: |
| 43 | + problem = self.problems_lookup.get(key) |
| 44 | + if problem: |
| 45 | + break |
| 46 | +``` |
| 47 | + |
| 48 | +### 2. Solution 文件检查 |
| 49 | + |
| 50 | +```python |
| 51 | +# 检查是否有solution文件 |
| 52 | +files = problem.get("files", {}) |
| 53 | +solution_file = files.get("solution", "") |
| 54 | + |
| 55 | +if solution_file: |
| 56 | + # 生成GitHub URL |
| 57 | + github_url = self.github_template.format(solution_file=solution_file) |
| 58 | + # 添加链接 |
| 59 | + return f"{full_text} | [Solution]({github_url})" |
| 60 | +``` |
| 61 | + |
| 62 | +### 3. 问题数据查找表构建 (`_build_problems_lookup`) |
| 63 | + |
| 64 | +从 `meta/problems/*.toml` 文件加载数据: |
| 65 | + |
| 66 | +```toml |
| 67 | +# 0079_word_search.toml |
| 68 | +id = "0079" |
| 69 | +leetcode_id = 79 |
| 70 | +[files] |
| 71 | +solution = "solutions/0079_word_search.py" |
| 72 | +``` |
| 73 | + |
| 74 | +查找表会存储多个key格式: |
| 75 | +- `"0079"` → problem data |
| 76 | +- `"79"` → problem data |
| 77 | +- `str(int("0079"))` → problem data (如果不同) |
| 78 | + |
| 79 | +## 数据流 |
| 80 | + |
| 81 | +``` |
| 82 | +state.get("problems", {}) # 从DataSourcesLoader加载 |
| 83 | + ↓ |
| 84 | +PostProcessor(config, problems=problems) |
| 85 | + ↓ |
| 86 | +merge_leetcode_api_data(problems) # 合并API缓存数据 |
| 87 | + ↓ |
| 88 | +_build_problems_lookup(problems) # 构建ID查找表 |
| 89 | + ↓ |
| 90 | +_add_github_solution_links(content) # 添加Solution链接 |
| 91 | +``` |
| 92 | + |
| 93 | +## 验证检查点 |
| 94 | + |
| 95 | +### ✅ 已检查的项目 |
| 96 | + |
| 97 | +1. **Problems数据传递** |
| 98 | + - `graph.py:1053`: `PostProcessor(config, problems=state.get("problems", {}))` |
| 99 | + - ✅ 正确传递 |
| 100 | + |
| 101 | +2. **查找表构建** |
| 102 | + - `_build_problems_lookup`: 支持多种ID格式 |
| 103 | + - ✅ 逻辑正确 |
| 104 | + |
| 105 | +3. **Solution链接添加** |
| 106 | + - `_add_github_solution_links`: 检查`files.solution`字段 |
| 107 | + - ✅ 逻辑正确 |
| 108 | + |
| 109 | +4. **正则表达式匹配** |
| 110 | + - Pattern: `r'\[(LeetCode\s+\d+[^\]]*)\]\(([^)]+)\)'` |
| 111 | + - ✅ 能匹配 `[LeetCode 79](url)` 和 `[LeetCode 79 - Title](url)` |
| 112 | + |
| 113 | +### 🔍 需要验证的项目 |
| 114 | + |
| 115 | +1. **Problems数据是否正确加载到state** |
| 116 | + - 检查 `graph.py:1188`: `"problems": data.get("problems", {})` |
| 117 | + - 检查 `main.py:293`: `data = loader.load_all()` |
| 118 | + |
| 119 | +2. **TOML文件中的files字段格式** |
| 120 | + - 确认格式为:`[files] solution = "solutions/0079_word_search.py"` |
| 121 | + - 不是:`files.solution` 或 `files["solution"]` |
| 122 | + |
| 123 | +3. **实际运行时的数据流** |
| 124 | + - 可能需要添加调试输出来验证 |
| 125 | + |
| 126 | +## 示例 |
| 127 | + |
| 128 | +### 输入 |
| 129 | +```markdown |
| 130 | +[LeetCode 79](https://leetcode.com/problems/word-search/) |
| 131 | +``` |
| 132 | + |
| 133 | +### 处理过程 |
| 134 | +1. Step 2: 规范化URL → `[LeetCode 79](https://leetcode.com/problems/word-search/description/)` |
| 135 | +2. Step 4: 查找问题ID "79" → 找到 `0079_word_search.toml` |
| 136 | +3. Step 4: 检查 `files.solution` → 找到 `"solutions/0079_word_search.py"` |
| 137 | +4. Step 4: 生成GitHub URL → `https://github.com/lufftw/neetcode/blob/main/solutions/0079_word_search.py` |
| 138 | + |
| 139 | +### 输出 |
| 140 | +```markdown |
| 141 | +[LeetCode 79](https://leetcode.com/problems/word-search/description/) | [Solution](https://github.com/lufftw/neetcode/blob/main/solutions/0079_word_search.py) |
| 142 | +``` |
| 143 | + |
| 144 | +## 潜在问题 |
| 145 | + |
| 146 | +1. **如果Solution链接没有添加,可能的原因:** |
| 147 | + - Problems数据未正确加载到state |
| 148 | + - TOML文件中缺少 `[files].solution` 字段 |
| 149 | + - 问题ID匹配失败(已改进查找逻辑) |
| 150 | + - 正则表达式匹配失败(已验证应该能匹配) |
| 151 | + |
| 152 | +2. **调试建议:** |
| 153 | + - 在 `_add_github_solution_links` 中添加调试输出 |
| 154 | + - 检查 `self.problems_lookup` 的内容 |
| 155 | + - 验证 `problem.get("files", {})` 的结构 |
| 156 | + |
| 157 | +## 改进建议 |
| 158 | + |
| 159 | +1. ✅ 已改进:问题ID查找逻辑,支持多种格式 |
| 160 | +2. ✅ 已改进:更清晰的查找键列表 |
| 161 | +3. 🔄 可选:添加调试模式,输出查找过程 |
| 162 | + |
0 commit comments