forked from rachelos/we-mp-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_template_parser.py
More file actions
55 lines (50 loc) · 1.57 KB
/
Copy pathdebug_template_parser.py
File metadata and controls
55 lines (50 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from core.lax.template_parser import TemplateParser
import logging
# 设置详细日志
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# 测试数据
articles = [
{"title": "文章1", "publish_time": "2023-01-01","content": "测试内容"},
{"title": "文章2", "publish_time": "2023-01-02","content": "测试内容"},
{"title": "文章3", "publish_time": "2023-01-03","content": "测试内容"}
]
# 问题模板
template = """
{
"articles":[{% for article in articles %}
{{article}}
{% if not loop.last %},{% endif %}{% endfor %}]
}
"""
template2 = """{
"feed": "测试feed",
"id": "测试feed",
"articles": [
{"title": "文章1", "pub_date": "2023-01-01"},
{"title": "文章2", "pub_date": "2023-01-02"},
{"title": "文章3", "pub_date": "2023-01-03"}
]
}"""
def main():
# 测试模板1
parser = TemplateParser(template)
result = parser.render({'articles': articles, 'feed': {'mp_name': '测试feed'}})
print(result)
last_comma_pos = result.rfind(',]')
if last_comma_pos>0:
print("❌ 错误: 最后一个元素后仍有逗号")
else:
print("✅ 正确: 最后一个元素后没有逗号")
parser2 = TemplateParser(template2)
result2 = parser2.render({'articles': articles, 'feed': {'mp_name': '测试feed'}})
print(result2)
last_comma_pos = result2.rfind(',]')
if last_comma_pos>0:
print("❌ 错误: 最后一个元素后仍有逗号")
else:
print("✅ 正确: 最后一个元素后没有逗号")
if __name__ == "__main__":
main()