Electronic structure changes during the surface-assisted formation of a graphene nanoribbon