sphinx: refactoring and cleanup sphinx pre/post