I'd use leanpub markdown. They generate those formats plus you get a page for the book, can charge for it, and advertise it. (Some of those tasks cost money).
Won't work if you want to keep everything local, though.
Won't work if you want to keep everything local, though.
If you want to look down the path of implementing book layout yourself, here are two breadcrumbs from my bookmarks:
https://journal.stuffwithstuff.com/2014/11/03/bringing-my-we...
https://iangmcdowell.com/blog/posts/laying-out-a-book-with-c...
That's one thing I learned, having written a couple of books. Even though writing is tough, it is easier in many wyas than the marketing.