export to doctest format

I wrote this because I needed it. I think it is very useful.

I'm not sure my code is that good - especially in the template I don't care much about handling non-plaintext output so I probably got it horribly wrong.

Still - I propose adding some version of this to the examples because that's where I looked and didn't find one.