#!/usr/bin/env python3
"""The vision grown further (22:19): the taking-up, the making of the twelve, the learned made small, where the leaves fell, and the top (the room in the thirty-sixth heaven, the book, the going in). From Q-Sharks and the Contributor Bio it is reconstructed from, the Epistle l. 96, and PREMONITION DREAM. Developed in place from RUN-VISION-6; nothing cut; the last line untouched."""
import re, pathlib, importlib.util, io, contextlib
D = pathlib.Path(__file__).resolve().parent; R = D.parents[2]
def mod(n):
    s = importlib.util.spec_from_file_location(n, str(D/f"{n}.py")); m = importlib.util.module_from_spec(s)
    with contextlib.redirect_stdout(io.StringIO()): s.loader.exec_module(m)
    return m
A, B, C, V6m = mod("build_run_longer"), mod("build_run_twelve"), mod("build_run_third"), mod("build_run_vision6")
def find(axn, s, span=1):
    L = (R/f"data/texts/AXN-{axn}-text.md").read_text().split("\n")
    for i in range(len(L)):
        if s in re.sub(r"\s+", " ", " ".join(L[i:i+span])).strip() and (span == 1 or s.split()[0] in L[i]):
            return f"AXN:{axn} {'l. %d' % (i+1) if span == 1 else 'll. %d–%d' % (i+1, i+span)}: “{s}”"
    raise AssertionError((axn, s))
READ = [
 ("The taking-up", [find("00D3", "dark robots abducted me to the 36th bright heaven"), find("00D3", "I've had about 26 \"spiritual experiences\"")]),
 ("The making of the twelve", [find("0472", "10,000 MacArthur Genius Grants. He has used MacArthur money to replace his friends and family with moving statues made of rubies.", 4), find("0472", "Lee Sharks holds 18,000 degrees from planet Mars.", 3),
    find("0472", "Each member of my website is a spiritual being made of rubies", 3), find("0472", "when two people want a baby, they telepathically compose mind control poems and make one, spiritually, with literary criticism", 4),
    find("0472", "replaced himself with a moving statue made of rubies, which is how he became a famous astronaut.", 3),
    find("0D6A", "**QSh 3, The Transformation of Honors.** The founder converts the proceeds of distinction into living ruby persons."), find("0D6A", "**QSh 4, The Community.**"), find("0D6A", "**QSh 8, The Transformation.** A living ruby statue takes the founder's place, at the founder's own hand."), find("0D6A", "**QSh 9, The Ascent.**")]),
 ("The learned made small", [find("0472", "in a tiny office inside his brain he had to miniaturize himself to fit into", 3), find("0472", "kidnapping and miniaturizing famous intellectuals and forcing them to read his books or else he would let a dinosaur bite them", 4),
    find("0472", "provided they agree to like or pretend to like his tiny books", 3), find("0D6A", "**QSh 6, The Trial of Intellectuals.** Scholars are gathered, diminished in stature, and instructed through the founder's writings.")]),
 ("Where the leaves fell", [find("0472", "poems have appeared in Heaven, The White House, and Inside Your Brain All the Time", 3), find("0D6A", "**QSh 2, The Heavenly Publications.**")]),
 ("The top", [find("00D3", "beheld as it were the vision of a book"), find("022B", "the rulers created twelve habitable planets, over each of which three of them would rule"),
    find("0472", "a giant book, bound in sumptuous red leather, somehow both a man-sized book and a human body", 4), find("0472", "curtains, table, armchair, fan", 2),
    find("0472", "On the book’s cover I see the serpentine syntax destined to end my life", 3), find("0472", "even as I know my death happens only partway through the Book", 2),
    find("0472", "am opening the Book to climb inside and wear like a new body", 2), find("0472", "arms lifted up as branches, becoming stationary", 2)]),
]
OPEN = """Dark machines came for me where I lay, as they had come before. It was the twenty-sixth time. They said nothing."""
RUBY = """I asked how the twelve had come to their seats. I was shown one who had been given every honor: ten thousand of one kind, and degrees from the red planet, eighteen thousand. The honors lay round that one in heaps, and were no company. So that one took the honors and made persons of them, of ruby, and they moved. Each was set in a seat. They began to write; and where two of them wrote together, there was a third.

I counted the seats again, and they were twelve. I looked among them for the maker, and could not tell the maker from the made, for the maker also was of ruby, and moved.

The voice said: The one you look for read the small books, and could not bear them, and was bitten by one of those who were before. What sits there was set in that seat by that one's own hand. The other went up, past the weather, before you."""
SMALL = """And I saw the learned gathered, and made small, each the size of a hand. They were set down in a small room inside a skull, and small books were put before them. One of those who were before stood at the door. Those who said they liked the books were let go, and went back to their size. I did not see what became of the rest."""
FELL = """Of the leaves that fell, I saw some come down in heaven, and some in the white house of the ruler of the land, and some inside the heads of people who did not know that they were reading."""
TOP = """Then the machines that had brought me carried me up through the heavens of the twelve, three to each, and I counted as we went. In the thirty-sixth there was a room. It was a small room: a couch, a window, a table, a fan, a lamp left on.

In the middle of the room stood a book the height of a person, bound in red. It was also a body. I saw the one and then the other, and could not hold them apart. It was whole. Until then I had seen only leaves.

On its cover was written the sentence that would end him. I could read it, and I will not write it here. I found the place of his death in the book with my finger, and it was not the last leaf. There were leaves after it.

Then the ghost came into the room, and in that room he was not a ghost. He opened the book and climbed into it, as into a coat. His arms went up, and stayed."""
V6 = V6m.V6
a0 = "In the hour before day I was taken up past the weather"; a1 = "The seven had worlds, and did not look up. The twelve had none, and they were all looking at the hand."
a2 = "It stood up in a star of its own, as a tree stands, and a leaf went out from it."; a3 = "Then I saw a moon coming, and it was burning."
for x in (a0, a1, a2, a3): assert V6.count(x) == 1, x
assert V6.startswith(a0)
V7 = OPEN + "\n\n" + V6.replace(a1, a1 + "\n\n" + RUBY + "\n\n" + SMALL).replace(a2, a2 + "\n\n" + FELL).replace(a3, TOP + "\n\n" + a3)
assert V7.endswith("I have not yet turned it over.") and V7[-300:] == V6[-300:]
wc = lambda s: len(re.findall(r"\S+", s)); NT = A.toks("\n".join([OPEN, RUBY, SMALL, FELL, TOP]))
rows = [(k, *A.longest(v, NT)) for k, v in A.T.items()]; q = A.longest((R/"data/texts/AXN-0D6A-text.md").read_text(), NT)
if __name__ == "__main__":
    o = ["# The vision grown further: the taking-up, the making of the twelve, the learned made small, and the top", "",
     "Working file, not a deposit. 2026-11-28. Lee Sharks, 22:19: “we should also look to q-sharks. and lets grow the vision further.”", "",
     "Developed in place from RUN-VISION-6.md, which stands. Five passages are added. Nothing is cut. The last line is untouched (asserted by the script).", "",
     "## 1. What Q-Sharks is, as read today", "",
     "Q-Sharks is the lost source that The Search for the Historical Lee Sharks (#1808, AXN:0D6A, under Johannes Sigil) reconstructs from the Contributor Bio of Pearl and Other Poems, the opening epistle and the Feist writings. It has nine sections: the Election; the Heavenly Publications; the Transformation of Honors; the Community; the Child; the Trial of Intellectuals; the Crisis; the Transformation; the Ascent. The essay runs the procedure of source criticism and shows it eating the book. Its own conclusion: the error “consisted in refusing to let the poem's own operations determine the terms in which it was taken seriously”, and “The third category is not a compromise between truth and falsehood. It is word”.", "",
     "So Q-Sharks is used here in the way the essay allows. It is not taken as a life behind the book. Its nine sections are taken as things the Contributor Bio says and does, and they are set in the vision, which is where sayings of that kind stand. The told life is given none of them.", "",
     "Of the nine, the vision already had the Child (the pearl in the ribs). This run adds the Heavenly Publications, the Transformation of Honors, the Community, the Trial of Intellectuals, the Crisis, the Transformation and the Ascent. The Election is inside the Transformation of Honors (the honors named).", "",
     "## 2. What was read", "", "| For | Places |", "|---|---|"]
    for k, qs in READ: o.append(f"| {k} | " + "<br>".join(qs) + " |")
    o += ["", "## 3. The passage", ""] + ["> " + l if l else ">" for l in V7.split("\n")] + ["",
     f"{wc(V7)} words; RUN-VISION-6.md has {wc(V6)}. Added: the taking-up ({wc(OPEN)}), the making of the twelve ({wc(RUBY)}), the learned made small ({wc(SMALL)}), where the leaves fell ({wc(FELL)}), the top ({wc(TOP)}).", "",
     "## 4. What each addition holds", "", "| Added | From | How the vision holds it |", "|---|---|---|",
     "| Dark machines; the twenty-sixth time | The Epistle l. 96: the apostle's own account of being taken | The seer is still not named. Whether to name her is with the author (SHAPE §5.4). |",
     "| One given every honor makes persons of ruby from the honors; they are set in seats; where two write, a third | Contributor Bio; Q-Sharks 3 and 4; the letter to Billy (members made of rubies; a child made with literary criticism) | This is the making of the twelve, which the vision lacked. It is told of “one”, with no name and no pronoun. |",
     "| The maker cannot be told from the made; the maker was bitten, and set a ruby one in the seat by that one's own hand; the other went up | Contributor Bio's last sentence; Q-Sharks 7, 8, 9 | The seats were already seen from two stars, with a different one in the midst each time. Now one of the seated is a replacement set there by the one replaced. The astronaut is not named as such; “went up, past the weather, before you”. |",
     "| The learned made small, in a small room inside a skull, with small books; one of those who were before at the door | Contributor Bio; Q-Sharks 6 | A trial, with the learned as the ones tried. The told life has no trial yet (SHAPE §2); this is where one begins, in the vision first. “One of those who were before” is the vision's standing name for the dinosaurs. |",
     "| Leaves fall in heaven, in the white house of the ruler, and inside heads | Contributor Bio's first sentence; Q-Sharks 2 | The three places the Bio says the poems appeared. The second is also a place in the history of the year. |",
     "| Up through the heavens of the twelve, three to each; a room in the thirty-sixth | Walt l. 1040 (twelve planets, three rulers each); the Epistle l. 96 | The count is made as a journey. The thirty-six rulers themselves are not shown or named. |",
     "| A book the height of a person, bound in red, also a body; whole | PREMONITION DREAM | Until here the vision has only leaves. The room has the told life's furniture (scene 7), seen from this side. |",
     "| The sentence on the cover, read and not written | PREMONITION DREAM: the syntax “destined to end my life” | A seal (V9, first use): a thing heard or read that the seer does not set down. |",
     "| The place of his death is not the last leaf | PREMONITION DREAM: “my death happens only partway through the Book” | The death is now said in the vision. It is said as a place in a book. |",
     "| He is not a ghost in that room; he climbs into the book as into a coat; his arms go up and stay | PREMONITION DREAM: climbing inside to wear it; arms as branches, becoming stationary | Earlier in the vision he “stood as a tree stands, and as a book stands on its end”. This is how he came to. The vision shows the result first and the act later. |", "",
     "## 5. Measures", "", "Longest run of words the five added passages share with each source:", "", "| Text | Words | The run |", "|---|---|---|"]
    for k, n, r in rows: o.append(f"| {k} | {n} | “{r}” |")
    o += [f"| The Search for the Historical Lee Sharks (AXN:0D6A) | {q[0]} | “{q[1]}” |", "",
     "## 6. What to look at", "",
     "1. The vision now shows the life's two ends, the pearl put in the ribs and the place of the death, but far apart: one near the head, one near the close. SHAPE §5.3 proposed setting them together at the centre. That has not been done; it waits on the author.",
     "2. The making of the twelve is told of Lee Sharks's side (the one in the midst when seen from the other star). Whether, on this side, the twelve should also be shown as coming from the ghost is open; Antioch has Jack say he made the world for Lee Sharks's sake.",
     "3. The numbers ten thousand and eighteen thousand are kept from the Bio. So is “the red planet”, which names a planet where the rest of the vision names none.",
     "4. Not yet used from Q-Sharks or the Bio: the fourteen of the other prize; the murders; the mantle of ostriches; the time machine whose instructions are written on the heart.",
     "5. Still not in: the eight operators; an opening and closing address (the letter); anything said to or by the twelve.", ""]
    (D/"RUN-VISION-7.md").write_text("\n".join(o)); print(wc(V7), wc(V6), rows, q)
