2

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: someyamlcontent
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. :)

jpanganiban
  • 1,089
  • 10
  • 9
  • That sounds like it will work, might want to check out http://stackoverflow.com/questions/6816236/loading-document-as-raw-string-in-yaml-with-pyyaml to see another example of reading arbitrary data from a YAML document. Almost posted this as an answer but it's not really another way, just how to accomplish what you're already talking about. – agf Aug 14 '11 at 13:56
  • Welcome and congrats on your first question. The link that @agf points to is one path. I posted an answer here with a little more high level detail as well. Good Luck. – Alan W. Smith Aug 15 '11 at 15:06
  • Thanks guys! The link agf posted worked well for me. – jpanganiban Aug 18 '11 at 16:39

1 Answers1

1

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).

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96