noahmorrison / chevron

A Python implementation of mustache
MIT License
505 stars 55 forks source link

Feature Request: Chevron support of parsing template #58

Closed spra85 closed 5 years ago

spra85 commented 5 years ago

We use Pystache's parse method so we know what variables/conditionals/etc. are in a given template. I'm curious if there's been any thought to supporting something like that in Chevon? Through perusing the source code, I didn't see anything like that available but not mentioned in the README but I may have missed it.

>>> parsed = pystache.parse(u"Hey {{#who}}{{.}}!{{/who}}")
>>> print parsed
[u'Hey ', _SectionNode(key=u'who', index_begin=12, index_end=18, parsed=[_EscapeNode(key=u'.'), u'!'])]
johnfraney commented 5 years ago

@spra85 I just happened to be looking for this, and there is a tokenizer function you can use:

from chevron.tokenizer import tokenize

It returns a generator of (token_type, content) tuples.

spra85 commented 5 years ago

@johnfraney, somehow I missed this when you initially responded, but thanks for sending. Will definitely take a look.

spra85 commented 5 years ago

@johnfraney, I actually ended up just writing my own custom regex for this. Basically,

import re
sorted({k for k in re.findall(r"{{\^?#?\/?{?(\w+)}}}?", template_content)})