A surgery formula for knot Floer homology