Cosmological perturbations in generalised gravity theories: formulation