If you have time to invest, go for a TeX variant (LaTeX or ConTeXt). That gives full control over any typesetting detail.
I’d recommend ConTeXt [0] over LaTeX. A minimal document is as simple as
\starttext
Start writing here…
\stoptext
and you can go a
long way from there, including making ebooks. Much better than LaTeX for layout control, language support, graphics integration, table generation, etc… It’s also intimately integrated with Lua for your scripting needs.
The documentation is possibly its weak point: it’s rich and comprehensive, although somewhat sparse and at times heavily technical. But it’s worth the effort, IMHO.
You may get inspired by the TeX showcase [1] (not only ConTeXt, but also LaTeX and plain TeX).
[0] http://wiki.contextgarden.net/Main_Page
[1] https://www.tug.org/texshowcase/