Cantiga 45 of the Cantigas de Santa Maria of Alfonso X

The Wicked Knight who Built a Monastery
A knight led a dissolute life. He offended his neighbours, he robbed people, and he destroyed churches and monasteries.
Eventually, he determined to make amends for his sins. While eating one day, he decided to build on his estate a monastery, complete with cloister, church, cemetery and infirmary. He planned to live there as a friar alongside fifty or one hundred monks.
He found a perfect place to build, but was suddenly stricken with a fatal illness. He died and devils came to take his soul. Angels also arrived and contested the devils’ possession of the soul.
An angel brought the case before the Virgin and she, in turn, asked Christ to give her the soul.
God granted her wish and gave to the angel a white cloth like a penitent’s habit. The angel, bearing this cloth, frightened the devils away. Yelping, the devils retreated to hell.
The angels, singing Surgat Deus, restored the soul to the knight’s body. He built a monastery and led a chaste life there.

List of Cantigas de Santa Maria