import { escapeHtml as e } from "../../html.ts"; import { fmt, LANG_NAMES, LANGS, type Dict, type Lang } from "./i18n/index.ts"; import { flatten, label, MAX_DEPTH, type SectionNode } from "./tree.ts"; export const LIBRARY_CSS = `:root{--bg:#f7f6f2;--fg:#1d1d1b;--muted:#5d5d58;--card:#fff;--line:#e2e0d8;--accent:#2f5d50;--warn:#9a3412} @media (prefers-color-scheme:dark){:root{--bg:#141413;--fg:#ecebe6;--muted:#a3a29b;--card:#1e1e1c;--line:#33332f;--accent:#8cc1b0;--warn:#fdba74}} *{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--fg);font:16px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif} main{max-width:880px;margin:0 auto;padding:32px 16px}a{color:var(--accent)}h1{margin:8px 0}.lead,.meta,.note{color:var(--muted)} nav.top{display:flex;gap:16px;flex-wrap:wrap;font-size:.875rem;justify-content:space-between}nav.top span a{margin-left:8px} form.search{display:flex;gap:8px;margin:16px 0 24px}form.search input{flex:1} input,select,textarea,button{font:inherit;padding:6px 10px;border:1px solid var(--line);border-radius:6px;background:var(--card);color:var(--fg)} button{cursor:pointer;border-color:var(--accent);color:var(--accent)}textarea{width:100%;min-height:120px} details{margin:4px 0}details>div,ul.toc ul{margin-left:20px}summary{cursor:pointer;font-weight:600} ul{padding-left:20px}ul.toc{list-style:none;padding-left:0}.items{list-style:"πŸ“– ";padding-left:24px} .card{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:20px;margin:16px 0} .error{border:1px solid var(--warn);color:var(--warn);border-radius:8px;padding:8px 12px}.ok{color:var(--accent)} .tag{display:inline-block;border:1px solid var(--line);border-radius:999px;padding:0 8px;margin:0 4px 4px 0;font-size:.875rem} .btn{display:inline-block;margin:8px 8px 0 0;padding:8px 16px;border:1px solid var(--accent);border-radius:8px;text-decoration:none} .inline{display:inline-flex;gap:6px;margin:2px 8px 2px 0;align-items:center}.hidden{opacity:.55} label{display:block;margin:10px 0 2px}label.check{display:flex;gap:6px;margin:2px 0;font-weight:normal} .f input[type=text],.f input[type=url]{width:100%}table{border-collapse:collapse;width:100%}td,th{border-bottom:1px solid var(--line);padding:6px;text-align:left} .d1{padding-left:20px}.d2{padding-left:40px}.d3{padding-left:60px}.d4{padding-left:80px} .chapter h2{margin-top:40px;padding-top:16px;border-top:1px solid var(--line)}.chapter h3{margin-top:24px} .box{background:var(--card);border-left:4px solid var(--accent);border-radius:8px;padding:4px 16px;margin:16px 0} .table{overflow-x:auto;margin:12px 0}.table td,.table th{vertical-align:top}nav.chtoc ol{padding-left:24px}`; export interface ItemView { id: string; title: string; author: string; edition: string; language: string; annotation: string; tags: string[]; linkUrl: string | null; fileName: string | null; fileSize: number | null; fileAccess: "public" | "closed"; rightsBasis: "own" | "licensed" | "public_domain" | null; status: "draft" | "published"; sectionIds: string[]; } interface ShellOpts { lang: Lang; title: string; switchPath?: string; noindex?: boolean } function shell(t: Dict, o: ShellOpts, inner: string): string { const langs = o.switchPath ? `${e(t.languages)}:${(Object.keys(LANGS) as Lang[]) .map((l) => (l === o.lang ? ` ${e(LANG_NAMES[l])}` : ` ${e(LANG_NAMES[l])}`)) .join("")}` : ""; return ` ${e(o.title)}${o.noindex ? '' : ""}
${inner}
`; } const withLang = (path: string, lang: Lang): string => `${path}${path.includes("?") ? "&" : "?"}lang=${lang}`; const err = (msg: string | undefined) => (msg ? `` : ""); // ---------- публичная Ρ‡Π°ΡΡ‚ΡŒ ---------- function itemLink(i: ItemView): string { const by = i.author ? ` β€” ${e(i.author)}` : ""; return `
  • ${e(i.title)}${by}
  • `; } /** Π§Π΅Ρ€Π½ΠΎΠ²ΠΈΠΊ Π³Π»Π°Π²Ρ‹ для Π±Π»ΠΎΠΊΠ° Β«Π§Π΅Ρ€Π½ΠΎΠ²ΠΈΠΊΠΈ для Ρ€Π΅Π΄Π°ΠΊΡ†ΠΈΠΈΒ» Π² ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π΅ вСрсии для ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ (CLT-17). */ export interface CatalogDraft { slug: string; title: string; hasReview: boolean; } export function renderCatalog( t: Dict, lang: Lang, tree: SectionNode[], itemsBySection: Map, search?: { q: string; found: ItemView[] }, drafts: readonly CatalogDraft[] = [], ): string { const node = (n: SectionNode): string => { const items = itemsBySection.get(n.id) ?? []; const inner = `${items.length ? `` : ""}${n.children.map(node).join("")}`; const title = e(label(n)); if (!inner) return `
    ${title}
    `; return `${title}
    ${inner}
    `; }; const q = search?.q ?? ""; const form = ``; const body = search ? `

    ${e(fmt(t.searchResults, { q }))}

    ${search.found.length ? `` : `

    ${e(t.nothingFound)}

    `}

    ${e(t.allLibrary)}

    ` : `

    ${e(t.tocHeading)}

    ${tree.map(node).join("")}`; const switchPath = search ? `/library?q=${encodeURIComponent(q)}` : "/library"; const draftLink = (d: CatalogDraft) => `
  • ${e(d.title)}${ d.hasReview ? `
    ${e(t.chapterReviewLink)}` : "" }
  • `; const draftBlock = drafts.length ? `

    ${e(t.catalogDraftsHeading)}

    ${e(t.catalogDraftsLead)}

      ${drafts.map(draftLink).join("")}
    ` : ""; return shell(t, { lang, title: t.title, switchPath }, `

    ${e(t.title)}

    ${e(t.lead)}

    ${draftBlock}${form}${body}`); } export function renderItem(t: Dict, lang: Lang, i: ItemView, sections: SectionNode[], opts: { editor: boolean }): string { const row = (k: string, v: string) => (v ? `
    ${e(k)}: ${e(v)}
    ` : ""); const tags = i.tags.length ? `
    ${e(t.tags)}: ${i.tags.map((x) => `${e(x)}`).join("")}
    ` : ""; const secs = sections.length ? `
    ${e(t.sections)}:
      ${sections.map((s) => `
    • ${e(label(s))}
    • `).join("")}
    ` : ""; const ann = i.annotation ? `

    ${e(t.annotation)}

    ${i.annotation.split(/\n{2,}/).map((p) => `

    ${e(p.trim())}

    `).join("")}` : ""; const link = i.linkUrl ? `${e(t.openLink)}` : ""; const file = i.fileName ? `${e(t.download)}${i.fileAccess === "closed" ? `

    ${e(t.closedFileNote)}

    ` : ""}` : ""; const draft = i.status === "draft" ? `

    ${e(t.draftNote)}

    ` : ""; const back = opts.editor ? ` Β· ${e(t.backToEditor)}` : ""; return shell( t, { lang, title: `${i.title} β€” ${t.title}`, switchPath: `/library/items/${i.id}` }, `${draft}

    ${e(i.title)}

    ${row(t.author, i.author)}${row(t.edition, i.edition)}${row(t.language, i.language)}${tags}${secs}${ann}

    ${link}${file}

    ${e(t.allLibrary)}${back}

    `, ); } export interface ChapterView { slug: string; title: string; html: string; toc: { id: string; title: string }[]; status: "draft" | "published"; reviewPage?: { title: string; html: string; toc: { id: string; title: string }[] }; } /** Π‘Ρ‚Ρ€Π°Π½ΠΈΡ†Π° Π³Π»Π°Π²Ρ‹ ΠΊΠ½ΠΈΠ³ΠΈ (CLT-15). `ch.html` ΡƒΠΆΠ΅ экранирован Ρ€Π΅Π½Π΄Π΅Ρ€ΠΎΠΌ Π³Π»Π°Π²Ρ‹. */ export function renderChapter(t: Dict, lang: Lang, ch: ChapterView, section: SectionNode | undefined, opts: { editor: boolean }): string { const draft = ch.status === "draft" ? `

    ${e(t.chapterDraftNote)}

    ` : ""; const onlyRu = lang === "ru" ? "" : `

    ${e(t.chapterOnlyRu)}

    `; const sec = section ? `

    ${e(t.chapterSection)}: ${e(label(section))}

    ` : ""; const toc = ch.toc.length ? `` : ""; const back = opts.editor ? ` Β· ${e(t.backToEditor)}` : ""; const review = ch.reviewPage ? `

    ${e(t.chapterReviewLink)}

    ` : ""; return shell( t, { lang, title: `${ch.title} β€” ${t.title}`, switchPath: `/library/chapters/${ch.slug}`, noindex: ch.status === "draft" }, `${draft}${onlyRu}${sec}${review}${toc}
    ${ch.html}

    ${e(t.allLibrary)}${back}

    `, ); } /** Π˜Ρ‚ΠΎΠ³ рСдакторской ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ Π³Π»Π°Π²Ρ‹ (CLT-16). ВсСгда noindex: это Ρ€Π°Π±ΠΎΡ‡ΠΈΠΉ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚, Π½Π΅ ΠΊΠ½ΠΈΠ³Π°. */ export function renderChapterReview(t: Dict, lang: Lang, ch: ChapterView & { reviewPage: NonNullable }): string { const r = ch.reviewPage; const onlyRu = lang === "ru" ? "" : `

    ${e(t.chapterOnlyRu)}

    `; const toChapter = `${e(t.chapterReviewBack)}`; const toc = r.toc.length ? `` : ""; return shell( t, { lang, title: `${r.title} β€” ${t.title}`, switchPath: `/library/chapters/${ch.slug}/review`, noindex: true }, `

    ${e(t.chapterReviewNote)}

    ${onlyRu}

    ${toChapter}

    ${toc}
    ${r.html}

    ${toChapter} Β· ${e(t.allLibrary)}

    `, ); } export function renderMessage(t: Dict, lang: Lang, title: string, text: string): string { return shell(t, { lang, title }, `

    ${e(title)}

    ${e(text)}

    ${e(t.allLibrary)}

    `); } // ---------- Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΎΡ€ ---------- const csrfInput = (csrf: string) => ``; export function renderLogin(t: Dict, lang: Lang, opts: { error?: string; disabled?: boolean }): string { const form = opts.disabled ? "" : `

    `; return shell(t, { lang, title: t.loginTitle, switchPath: "/library/editor" }, `

    ${e(t.loginTitle)}

    ${e(opts.disabled ? t.editorOff : t.loginLead)}

    ${err(opts.error)}${form}`); } export function renderEditor( t: Dict, lang: Lang, csrf: string, tree: SectionNode[], items: ItemView[], opts: { error?: string; saved?: boolean; chapters?: { slug: string; title: string; status: "draft" | "published" }[] }, ): string { const act = (n: SectionNode, action: string, text: string) => `
    ${csrfInput(csrf)}
    `; const node = (n: SectionNode): string => { const addChild = n.depth < MAX_DEPTH - 1 ? `
    ${csrfInput(csrf)}
    ` : ""; const rename = `
    ${csrfInput(csrf)}
    `; const hide = n.selfHidden ? act(n, "restore", t.restore) : act(n, "hide", t.hide); return `