Measurement of quarkonium production in proton–lead and proton–proton collisions at \(5.02~\mathrm {TeV}\) with the ATLAS detector