将Liquid模板渲染为Python字典。

huangapple go评论102阅读模式
英文:

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? 将Liquid模板渲染为Python字典。

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 }'}

huangapple
  • 本文由 发表于 2023年5月25日 20:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定