improve gen_gallery.py

- add captions, contents and section titles
fix sidebar links
move _gallery_template contents to new doc/gallery_template.html

- clean up