Improve doctest stripping, closes #1678