Sometime around the year 1314, a retired Egyptian bureaucrat named Shihab al-Din al-Nuwayri began writing a compendium of all knowledge, under the appealingly reckless title The Ultimate Ambition in the Arts of Erudition. It would eventually total more than 9,000 pages in thirty volumes, covering all of human history from Adam onward, all known plants and animals, geography, law, the arts of government and war, poetry, recipes, jokes, and of course, the revelations of Islam.

At one point, Nuwayri tackles a subject that may seem familiar to the modern audience: the Islamic punishments for adultery, sodomy, and fornication. He cites authorities who declare that such sinners must be stoned to death or severely flogged, in language that conjures up the gruesomely “medieval” execution videotapes posted seven hundred years later by ISIS: “Whosoever engages in the act of the people of Lot—both the active and passive participant—must be put to death.”

Yet this authentically medieval author then continues unblinkingly with a long, celebratory chapter about erotic poetry, much of it homosexual and wine-fueled. A sample:

That sly and brilliant one

Who grows girlish in his impudence

He appears manly at first

But after a drink is suddenly a woman

When you tell him: “Baby, say Moses,”

He lisps moistly: “Motheth”

He embraces me until morning

Trading stories with me in the dark.

The juxtaposition is one of many in this bizarre, fascinating book that illustrate the sprawlingly heterodox reality of the early centuries of Islam, so different from the crude puritanical myths purveyed by modern-day jihadis. The Ultimate Ambition, a canonical work for scholars in the Islamic world for centuries, has been translated into English for the first time and radically condensed (to about three hundred pages) by Elias Muhanna, a professor of comparative literature at Brown University. Reading it is like stumbling into a cavernous attic full of unimaginably strange artifacts, some of them unforgettable, some merely dross. From the alleged self-fellation of monkeys to the many lovely Bedouin words for the night sky (“the Encrusted, because of its abundance of stars, and the Forehead, because of its smoothness”) to the court rituals of Egypt’s then-overlords, the Mamluks, nothing seems to escape Nuwayri’s taxonomic ambitions.

Nuwayri draws heavily on earlier Islamic sources, and his respect for tradition usually prevents him from passing judgment, even when the claims he is citing are hilariously implausible. In one section, for instance, he passes on a story about a sexually voracious she-bear who captures a man so that she can slake her lust on him again and again, licking his feet raw to prevent him from leaving the cave. Yet at a few points Nuwayri permits himself a brief editorial comment, as in one section about happiness: “Imru’ al-Qays was asked, ‘What is happiness?’ and he replied: ‘A delicate maiden burning with fragrance, burdened by her ample curves.’ He was infatuated by women.” At another point Nuwayri relays a story from “a trustworthy person among the Abyssinians” about how to escape the charge of a wild rhino: “If the man urinates on the rhinoceros’s ear, it will run away and not return to him. That way, the man will escape from it. God knows best.” One has to wonder if the pious addendum is slightly tongue-in-cheek—a rhetorical shrug of the shoulders.

At times Nuwayri allows his sources to compete with each other, citing different juristic opinions on wine-drinking, music, and the punishments for illicit sex. At least once, he even dramatizes such a disagreement:

The caliph al-Ma’mūn asked (the judge) Yaḥyā ibn Aktham about the meaning of desire, and he replied: “It is the auspicious thoughts that a man’s heart falls in love with and his soul esteems.” Then (the theologian) Thumāma spoke up and said: “Shut up, Yaḥyā! You should stick to answering questions about divorce or whether a pilgrim violates his ritual purity by hunting a gazelle or killing an ant.”

Mostly, the heterodoxy creeps in sideways, in the book’s unapologetic references to supposedly illicit pleasures. The section on the human body includes the sub-heading “On Poetic Descriptions of the Down on the Young Male Cheek.” The section titled “On the Buttocks” includes this poetic snippet:

The eyes of his onlookers gathered around

His haunches, like a second belt

But Nuwayri is not deliberately sabotaging Muslim orthodoxy. He is merely reflecting a world in which moral prescriptions existed alongside a much messier reality, and some degree of dissonance between the two was accepted and forgiven. This loose fit between life and text applied throughout the pre-modern Middle East, but perhaps especially in the turbulent, plague-ridden years of the fourteenth century. Egypt’s rulers, the Mamluks, were a caste of military slave-soldiers who had seized power from their owners in 1250, three decades before Nuwayri’s birth, and remained in power until the Ottomans conquered them in 1517. They were mostly Turkic people from the Eurasian steppe whose forefathers had been kidnapped and trained (too well) in the arts of war. Culturally, it was a time when Sufi mysticism was gaining adherents, and rowdy religious festivals packed Cairo’s streets, encouraging promiscuous minglings of sect and ritual.

This kind of dissonance is still visible in much of the Middle East, despite the dramatic encroachment in recent decades of more literalistic and intolerant strains of religion. I was always struck, while living in Iraq and Lebanon, by the way Muslims could claim they accepted brutally categorical edicts on hellfire, Jews, and unbelief while living in a far more elastic and accepting way. This, I think, is what the late scholar Shahab Ahmed meant when he wrote in his posthumous book What Is Islam? that a true understanding of Islam must “come to terms with—indeed, be coherent with—the capaciousness, complexity, and, often, outright contradiction that obtains within” the religion’s lived history.

Religion aside, the book is full of strange myths and nostrums that hint at what mattered to people in the fourteenth century: sex, money, power, perfume. Nuwayri retails directions for incense and fragrance that are so elaborate it is hard to believe anyone really followed them. (One begins, “Take one hundred mithqāls of rare Tibetan musk and pound it after cleaning it of organ matter and hair.”) Then again, people and cities must have smelled awful, and olfactory relief made a difference. There are also many formulae for enlarging the penis, tightening the vagina, enemas, suppositories, contraceptives, and other sexual aids, with titles like “A Recipe for Another Medicine that Produces Indescribable Pleasure.”

Some of the most memorable moments in The Ultimate Ambition come when Nuwayri stops being so deferential to earlier Arab sources and offers up his own experience. In fact, it is a shame there are not more of these, because—as Muhanna recounts in an introduction to his translation—Nuwayri led an interesting life, working his way up through the Mamluk administration as a clerk and supervisor for the Mamluk sultan in Egypt, Syria, and what is now Lebanon. Nuwayri appears to have fought in an important battle with a Mongol army in Syria. Later, he was given the job of overseeing the sultan’s properties back in Cairo, an important position that required him to manage and coordinate the activities of officers, traders, judges, clerics, and teachers.

This distinguished resumé accounts for Nuwayri’s self-assured tone as he lists the duties of a medieval scribe, with notes on how he should carry himself, what he should wear, and how he should speak. Nuwayri proudly retails what he has learned about the proper management of the sultan’s properties, including subsections on “The Sultan’s Larder” and “The Sultan’s Buttery” (“The administrator also prepares the ṭāri’ al-ṭāri’, which is the third meal, served after the second is removed and reserved for the sultan and his closest companions”). He also narrates the signal events of his lifetime, including a sudden bout of inflation near the close of the thirteenth century (he lists the prices of wheat, chickens, and quince) and a terrible plague that led to victims being buried in mass graves. “The corpses that were not well buried were eaten by the dogs,” he writes. “Meanwhile, the people who lived ate the dogs.”

One of the book’s most memorable moments, for me, comes in the section on lions, which were still indigenous to the Middle East in Nuwayri’s time. After citing various authorities on the lion’s bravery, he adds that he has seen this quality with his own eyes, during a night journey from a town in the Jordan Valley to another one near what is today Ramallah, in the West Bank:

I was with about twenty-five horsemen and a group of men carrying bows and quivers. It was a moonlit night, and a lion joined us as we were walking. He kept pace with us along the right side of the road; he was not far at all, in fact closer than a stone’s throw away, and he stayed that way for a quarter of the night. Then, when he despaired of catching any one of us, due to our vigilance, he lagged behind us and then left us in a different direction.

This passage, so persuasively real, is offered by Nuwayri as a kind of aside, a dip into the merely empirical. For him, the primary task was weaving together the accounts of his predecessors into a vast and unified body of knowledge. He could scarcely have imagined that The Ultimate Ambition, written with such confidence from the heart of a mighty empire, would one day appear to us like that lion: a vanished, near-mythical creature at the desert’s edge.

Shihab al-Din al-Nuwayri’s The Ultimate Ambition in the Arts of Erudition, edited by Elias Muhanna, will be published August 30 by Penguin Random House.