Heralded generation of high-purity ultrashort single photons in arbitrary temporal shapes