dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

Incorrect behavior if text notstartswith '\n' and template with '_end_' indicates #104

Open matrix273 opened 1 year ago

matrix273 commented 1 year ago

Data notstartswith '\n':

data = """interface Port-Channel12
 ip address 1.1.1.124 255.255.255.255
    !
"""

Template with '_end_' indicates:

<group name="interfaces" >
interface {{ interface }}
{{line|_line_}}
{{ endline|ORPHRASE|_end_}} 
</group>

code below

parser = ttp(data=data, template=template)
parser.parse()

# print result in JSON format
results = parser.result(format='json')[0]
print(results)

I got result like below,missing 'line'

[
    {
        "interfaces": {
            "interface": "Port-Channel12"
        }
    }
]

If I remove the '_end_' indicates,Template like this

<group name="interfaces" >
interface {{ interface }}
{{line|_line_}}
{{ endline|ORPHRASE}} 
</group>

the result will like below,include 'line'

[
    {
        "interfaces": {
            "interface": "Port-Channel12",
            "line": " ip address 1.1.1.124 255.255.255.255\n    !"
        }
    }
]

And If I did't change Template,Only make Data startswith '\n',Data like this

data = """
interface Port-Channel12
 ip address 1.1.1.124 255.255.255.255
    !
"""

the result will like below,include 'line' too

[
    {
        "interfaces": {
            "interface": "Port-Channel12",
            "line": " ip address 1.1.1.124 255.255.255.255\n    !"
        }
    }
]