英文:
Rendered Liquid template to Python dict
问题
我可以帮你翻译你提供的Liquid模板以及期望的字典格式,但不会回答关于翻译的问题。以下是翻译好的部分:
{
"articleHeadline": "This is the article of the headline",
"intro": "\nIntroduction to the document\n",
"articleHighlights": "\nStep 1: Get up\n\nStep 2: Go to kitchen\n\nStep 3: Get coffee\n",
"articleSummary": "\nSummary of the document\n"
}
如果你有其他需要,请随时告诉我。
英文:
I have the following Liquid template:
{%- capture vars -%}
{%- capture articleHeadline -%}This is the article of the headline{%- endcapture -%}
{%- capture intro -%}
Introduction to the document
{%- endcapture -%}
{%- capture articleHighlights -%}
Step 1: Get up
Step 2: Go to kitchen
Step 3: Get coffee
{%- endcapture -%}
{%- capture articleSummary -%}
Summary of the document
{%- endcapture -%}
{%- endcapture -%}
I'd like to parse this text and end up with a dict
of the form:
{
"articleHeadline": "This is the article of the headline",
"intro": "\nIntroduction to the document\n"
"articleHighlights": "\nStep 1: Get up\n\nStep 2: Go to kitchen\n\nStep 3: Get coffee\n",
"articleSummary": "\nSummary of the document\n"
}
I tried Python Liquid but I'm confident it does not reverse engineer templates like this. I also checked out BeautifulSoup for this purpose but it will only parse HTML tags.
Is there a library or method that I can use to parse this (hopefully avoiding regex)?
答案1
得分: 1
以下是您要翻译的部分:
我从未使用过这个库,但它似乎公开了一个返回模板的抽象语法树(AST)的解析器API;从那里开始,只需要递归函数来查找所有`capture`块并返回它们的内容。
```python
from pprint import pprint
from liquid.builtin.tags.capture_tag import CaptureNode
from liquid.environment import Environment
template = """
{%- capture vars -%}
{%- capture articleHeadline -%}This is the article of the headline{%- endcapture -%}
{%- capture intro -%}
Introduction to the document
{%- endcapture -%}
{%- capture articleHighlights -%}
Step 1: Get up
Step 2: Go to kitchen
Step 3: Get coffee
{%- endcapture -%}
{%- capture articleSummary -%}
Summary of the document
{%- endcapture -%}
{%- endcapture -%}
"""
def get_capture_contents(node):
if isinstance(node, CaptureNode):
yield (node.name, node.block)
if hasattr(node, "children"):
for child in node.children():
yield from get_capture_contents(child.node)
env = Environment()
ast = env.parse(template)
blocks = {}
for stmt in ast.statements:
for cap_name, content in get_capture_contents(stmt):
blocks[cap_name] = str(content)
pprint(blocks)
英文:
How are you that confident that Python Liquid doesn't do this?
I've never used the library before, but it seems to expose a parser API that returns the AST (abstract syntax tree) of the template; from there on out it's just a recursive function away to find all capture
blocks and return their contents.
from pprint import pprint
from liquid.builtin.tags.capture_tag import CaptureNode
from liquid.environment import Environment
template = """
{%- capture vars -%}
{%- capture articleHeadline -%}This is the article of the headline{%- endcapture -%}
{%- capture intro -%}
Introduction to the document
{%- endcapture -%}
{%- capture articleHighlights -%}
Step 1: Get up
Step 2: Go to kitchen
Step 3: Get coffee
{%- endcapture -%}
{%- capture articleSummary -%}
Summary of the document
{%- endcapture -%}
{%- endcapture -%}
"""
def get_capture_contents(node):
if isinstance(node, CaptureNode):
yield (node.name, node.block)
if hasattr(node, "children"):
for child in node.children():
yield from get_capture_contents(child.node)
env = Environment()
ast = env.parse(template)
blocks = {}
for stmt in ast.statements:
for cap_name, content in get_capture_contents(stmt):
blocks[cap_name] = str(content)
pprint(blocks)
prints out
{'articleHeadline': 'This is the article of the headline',
'articleHighlights': 'Step 1: Get up\n'
'\n'
'Step 2: Go to kitchen\n'
'\n'
'Step 3: Get coffee',
'articleSummary': 'Summary of the document',
'intro': 'Introduction to the document',
'vars': 'var articleHeadline = { This is the article of the headline }var '
'intro = { Introduction to the document }var articleHighlights = { '
'Step 1: Get up\n'
'\n'
'Step 2: Go to kitchen\n'
'\n'
'Step 3: Get coffee }var articleSummary = { Summary of the document }'}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论