update exportlegends for DF40.x

Several fixes to the code following feedback from Lethosor.  Nicer structure in a few places, cleaner output, less dependant on a single version of DF or dfhack, easier to update in future.  Exports the new detailed maps.  

I'd love it to also export site maps, but I've got no idea how to do that.