feat(render_test): 补全 Gridea Pro 自定义 filter stub 与数字参数 / ifchanged 兼容#2
Merged
Conversation
## 问题
`render_test.py` 用 Python Jinja2 模拟 Pongo2 渲染,但缺三类支持:
1. **Gridea Pro 自定义 filter 没注册**:`excerpt` / `reading_time` / `word_count` /
`strip_html` / `relative` / `to_json` / `group_by` 等 → 主题用了就 FAIL
2. **未加引号的数字参数**:如 `excerpt:160` → `excerpt(160)` 转换缺失(Pongo2→Python Jinja2 转换器只识别带引号字符串) → 主题用了就 FAIL
3. **`ifchanged` Django 标签**:Pongo2 支持但 Python Jinja2 不识别 → 渲染异常
历史上 `bitcron-pro` / `mango` / `printer` / `kehua` / `liushen` / `jasmine` 等多个主题被迫绕过这些 filter(主题代码变丑或假阳性 PASS)。
## 修复
`scripts/render_test.py` 三处增量改动(仅 +71 行,不删除任何旧逻辑):
### 1. Pongo2→Jinja2 转换器:新增数字参数支持
原只把 `|filter:"arg"` 转成 `|filter("arg")`,现在也把 `|filter:123` 转成 `|filter(123)`。
### 2. `ifchanged` 兼容
`{% ifchanged %}` / `{% endifchanged %}` 替换为 `{% if true %} / {% endif %}` —— 视觉上每次迭代都会输出,但渲染不报错。
### 3. 13 个自定义 filter stub
仅保证模板渲染通过(不要求和 Gridea Pro 实际行为像素级一致):
- `excerpt(value, length=140)` —— 去 HTML + 截 N 字符
- `word_count(value)` —— 去 HTML 后字符数
- `reading_time(value)` —— word_count // 400 ≥ 1
- `strip_html(value)` —— 去 HTML
- `relative(value)` / `timeago(value)` —— 直接返回字符串
- `to_json(value)` —— `json.dumps(ensure_ascii=False)`
- `group_by(value, key="year")` —— 按字段分组返回 `[SimpleNamespace(key, items)]`
- `striptags`、`urlencode`、`truncatechars`、`split`、`join`、`first`、`last`、`upper`、`lower` —— 标准 Pongo2 filter 的 Python 等价实现
## 验证
- ✅ `bitcron-pro`、`liushen`、`jasmine` 三个主题 render_test 通过(之前部分模板因 filter 缺失 FAIL)
- ✅ 不影响仅用基础 filter 的旧主题
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
9 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
`render_test.py` 用 Python Jinja2 模拟 Pongo2 渲染,但缺三类支持,导致主题作者必须绕过才能让测试 PASS,主题代码因此变丑或出现假阳性:
影响过的主题
`bitcron-pro` / `mango` / `printer` / `kehua` / `liushen` / `jasmine` 都被迫绕路:
修复
`scripts/render_test.py` 纯增量 +71 行,不删任何旧逻辑:
1. Pongo2→Jinja2 转换器:新增数字参数支持
```python
处理未加引号的数字参数: |filter:123 → |filter(123)
def replace_numeric_filter(m):
return f"|{m.group(1)}({m.group(2)})"
converted = re.sub(
r'\|\s*(\w+)\s*:\s*(-?\d+(?:\.\d+)?)(?=\s|\||$|\})',
replace_numeric_filter,
converted,
)
```
2. `ifchanged` 兼容
`{% ifchanged %}` / `{% endifchanged %}` 在渲染前替换为 `{% if true %}` / `{% endif %}` —— 视觉上每次迭代都会输出,但渲染不报错。
3. 13 个自定义 filter stub(测试桩)
验证
与其他 PR 的关系
Test plan
🤖 Generated with Claude Code