Add strict mode to substitute.py

In strict mode the script aborts if the text can't be successfully
substituted.
Fix a bug with the %% token.
Improve error handling for IO in substitute.