It's all at a very approachable level, too. I had to write an epub-maker as a necessary component for a project, and it turned out to be ~150 lines of python. You have to make a few indexes in XML and stick them into a zip file, basically.
https://github.com/phuff/epub_builder
I built it because I wanted to have something that made a daily brief news paper that was personalized and sent to my kindle. It makes an epub and uses kindlegen to convert it to a .mobi. There's a lot of fun epub formatting stuff you can do.
Here's the system that makes the daily newspaper, but it's been so long I'm not sure it's actually functional code outside of my production version: