YAML on Textile Parsing
I think this is my first question here. I've been lurking around on StackOverflow for quite some time now it has been a great resource to me.
I'm writing a small static site generator that works like jekyll (The end-user writes his page content in textile and the header(meta-info) in YAML - a script mashes it up with the template places everything in a directory which the server renders) in python. I'm doing this for the sake learning python.
I'm able to parse a yaml file into a dictionary; as well as convert textile into html all in python - thanks to the python libraries. The question is: how do I parse the yaml in a textile file and the textile within the same file?
Right now, the idea is to put a line separator between the yaml and the textile content: ex.
---
someyaml: someyamlcont开发者_运维知识库ent
anothervariable: somevalue
andsoon:
- something
- somestuff
---
_all the textile content goes here_ as well as all the **osm** here.
My sort of idea was to read the lines manually and ask if the current line is == '---' then after that the python-yaml comes in; ones the '---' is back, it's python-textile turn to work.
Are there any other ways of doing this?
Thanks in advance. :)
Assuming all your file will have the same format and use YAML, one possibility would be to read the file into a single string and then do a string split on "---" to end up up with an array.
I'm not a python guy, but I think this should create an array with the following:
- Index 0 - Empty
- Index 1 - Your YAML Headers
- Index 2-N - Your main body content.
From here, you could just process Index 1 for your YAML and then deal with your body content separately. Note that your body content would be split into multiple array elements if you have any other "---" strings. So, you'd want to join index 2 with any others that are after it (adding the "---" strings back in while you do the join).
精彩评论