Public: anyone can read this; the crew can change it.
A .bib file shows its references as a readable list: authors, title, venue, year #14632
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14562-a-bib-file-shows-its-references-as-a-rea → main 4 commits fork 2 threads · 2 unresolved
11 files
changed +548
−2
docs/examples/refs.bib added +16
| @@ -0,0 +1,16 @@ | ||
| 1 | @article{knuth84, | |
| 2 | author = {Knuth, Donald E.}, | |
| 3 | title = {Literate Programming}, | |
| 4 | journal = {The Computer Journal}, | |
| 5 | year = 1984 | |
| 6 | } | |
| 7 | ||
| 8 | @inproceedings{lamport78, | |
| 9 | author = {Lamport, Leslie and Shostak, Robert and Pease, Marshall and Garcia-Molina, Hector and Ullman, Jeffrey D. and Widom, Jennifer}, | |
| 10 | title = {Time, Clocks, and the Ordering of Events in a Distributed System, with a Deliberately Long Title to Wrap on a Phone}, | |
| 11 | booktitle = {Communications of the ACM}, | |
| 12 | year = 1978 | |
| 13 | } | |
| 14 | ||
| 15 | @article{broken, | |
| 16 | title = {never closed |
docs/features/file-kinds.md +1
| @@ -64,6 +64,7 @@ The forge draws a file on its page when its kind is below and it is within the s | ||
| 64 | 64 | - **SQL**, drawn up to 1 MB: `.sql` |
| 65 | 65 | - **GraphQL**, drawn up to 1 MB: `.graphql`, `.gql` |
| 66 | 66 | - **Mailbox**, drawn up to 5 MB: `.mbox` |
| 67 | - **Bibliography**, drawn up to 1 MB: `.bib` | |
| 67 | 68 | - **Protocol Buffers**, drawn up to 1 MB: `.proto` |
| 68 | 69 | - **INI or properties**, drawn up to 1 MB: `.ini`, `.properties` |
| 69 | 70 | - **Rich text**, drawn up to 5 MB: `.rtf` |
scripts/kinds-guide.ts +1 −1
| @@ -18,7 +18,7 @@ export const NAMES: Readonly<Record<FileKind, string>> = { | ||
| 18 | 18 | excalidraw: "Excalidraw", step: "STEP model", sarif: "SARIF report", lcov: "Coverage (lcov)", junit: "JUnit report", |
| 19 | 19 | opml: "OPML", feed: "RSS or Atom feed", env: "Env example", openapi: "OpenAPI", package: "package.json", makefile: "Makefile", |
| 20 | 20 | dockerfile: "Dockerfile", workflow: "GitHub workflow", compose: "Compose file", sql: "SQL", graphql: "GraphQL", |
| 21 | epub: "EPUB book", mbox: "Mailbox", proto: "Protocol Buffers", ini: "INI or properties", rtf: "Rich text", xml: "XML", text: "Other text", binary: "Other binary", | |
| 21 | epub: "EPUB book", mbox: "Mailbox", bib: "Bibliography", proto: "Protocol Buffers", ini: "INI or properties", rtf: "Rich text", xml: "XML", text: "Other text", binary: "Other binary", | |
| 22 | 22 | }; |
| 23 | 23 | |
| 24 | 24 | /** 1_000_000 as `1 MB`; 4_000_000_000 as `4 GB`; no cap as `none: download only`. */ |
src/tui/screens/workspace.ts +2 −1
| @@ -95,6 +95,7 @@ import { sheetView } from "./workspace_sheet.ts"; | ||
| 95 | 95 | import { sqliteStep, sqliteView, type SqliteAt } from "./workspace_sqlite.ts"; |
| 96 | 96 | import { officeView } from "./workspace_office.ts"; |
| 97 | 97 | import { parquetView } from "./workspace_parquet.ts"; |
| 98 | import { bibView } from "./workspace_bib.ts"; | |
| 98 | 99 | import { layoutTable, type Grid } from "../ui/grid.ts"; |
| 99 | 100 | import type { Ctx, Screen } from "../app/app.ts"; |
| 100 | 101 | import { stripAnsi } from "../term/ansi.ts"; |
| @@ -1552,7 +1553,7 @@ export function workspaceScreen( | ||
| 1552 | 1553 | // bytes as text is how a terminal ends up beeping at you. |
| 1553 | 1554 | const base = looksLikeImage(t.path) ? null : sqliteView(t.path, read.value.body, { table: 0, page: 1 }); |
| 1554 | 1555 | if (base !== null && typeof base !== "string") db = read.value.body; |
| 1555 | const book = looksLikeImage(t.path) ? null : (base ?? sheetView(t.path, read.value.body) ?? officeView(t.path, read.value.body) ?? parquetView(t.path, read.value.body)); | |
| 1556 | const book = looksLikeImage(t.path) ? null : (base ?? sheetView(t.path, read.value.body) ?? officeView(t.path, read.value.body) ?? parquetView(t.path, read.value.body) ?? bibView(t.path, read.value.body)); | |
| 1556 | 1557 | if (looksLikeImage(t.path)) picture = read.value.body; |
| 1557 | 1558 | else if (book !== null && typeof book !== "string") sheet = book; |
| 1558 | 1559 | else if (typeof book === "string") note = book; |
src/tui/screens/workspace_bib.ts added +27
| @@ -0,0 +1,27 @@ | ||
| 1 | /** | |
| 2 | * A .bib file in the terminal workspace, read the way the page reads it: the same `parseBib`, | |
| 3 | * drawn as a table of references. The same gate too: the name must say so (`fileKind`), under | |
| 4 | * the page's cap. | |
| 5 | * | |
| 6 | * `bibView(path, body)` answers a table, a sentence (the workspace shows it as its note), or | |
| 7 | * `null` for a file that is not a bibliography. | |
| 8 | */ | |
| 9 | ||
| 10 | import { fileKind, HEAD_BYTES, RENDER_CAP } from "../../web/render/kinds.ts"; | |
| 11 | import { parseBib } from "../../web/render/bib.ts"; | |
| 12 | import type { Table } from "../../text/csv.ts"; | |
| 13 | ||
| 14 | export function bibView(path: string, body: Uint8Array): Table | string | null { | |
| 15 | if (fileKind(path, body.subarray(0, HEAD_BYTES)) !== "bib") return null; | |
| 16 | if (body.length > RENDER_CAP.bib) | |
| 17 | return `This file is over ${RENDER_CAP.bib / 1_000_000} MB, too large to open here. Download it instead.`; | |
| 18 | const doc = parseBib(new TextDecoder().decode(body)); | |
| 19 | if (doc.items.length === 0) return "This file holds no references."; | |
| 20 | const header = ["Key", "Title", "Authors", "Venue", "Year"]; | |
| 21 | const rows = doc.items.map((i) => | |
| 22 | i.kind === "entry" | |
| 23 | ? [`${i.key} (${i.type})`, i.title, i.authors, i.venue, i.year] | |
| 24 | : [i.raw.split("\n", 1)[0]!, `not read: ${i.reason}`, "", "", ""], | |
| 25 | ); | |
| 26 | return { header, rows, more: Math.max(0, doc.total - doc.items.length), delimiter: ",", columns: header.length }; | |
| 27 | } |
src/web/render/bib.ts added +381
| @@ -0,0 +1,381 @@ | ||
| 1 | /** | |
| 2 | * A BibTeX `.bib` file as references: one row per entry with its key, type, | |
| 3 | * authors, title, journal or booktitle, and year. Braces and common TeX | |
| 4 | * accents are unescaped, `@string` macros expand (and `#` joins pieces), | |
| 5 | * `@comment` and `@preamble` are skipped. An entry that does not parse is | |
| 6 | * shown raw with a one-line reason; the rest still draw. Hand-written, no | |
| 7 | * dependency; bounded by the kind's size cap, `ENTRY_CAP` rows and `MAX_STEPS` | |
| 8 | * characters scanned. Every cell is escaped by the template. | |
| 9 | */ | |
| 10 | ||
| 11 | import { html } from "../html.ts"; | |
| 12 | import { fileSize } from "../ui.ts"; | |
| 13 | import type { Rendered, RenderInput } from "./renderers.ts"; | |
| 14 | ||
| 15 | export const ENTRY_CAP = 500; | |
| 16 | /** Characters the scanner may step over: the time bound. */ | |
| 17 | export const MAX_STEPS = 2_000_000; | |
| 18 | const RAW_CAP = 400; | |
| 19 | ||
| 20 | export interface BibEntry { | |
| 21 | readonly kind: "entry"; | |
| 22 | readonly key: string; | |
| 23 | readonly type: string; | |
| 24 | readonly authors: string; | |
| 25 | readonly title: string; | |
| 26 | readonly venue: string; | |
| 27 | readonly year: string; | |
| 28 | } | |
| 29 | export interface BibBroken { | |
| 30 | readonly kind: "broken"; | |
| 31 | readonly raw: string; | |
| 32 | readonly reason: string; | |
| 33 | } | |
| 34 | export type BibItem = BibEntry | BibBroken; | |
| 35 | ||
| 36 | export interface BibDoc { | |
| 37 | readonly items: readonly BibItem[]; | |
| 38 | /** Entries found, drawn or not. */ | |
| 39 | readonly total: number; | |
| 40 | readonly truncated: boolean; | |
| 41 | } | |
| 42 | ||
| 43 | const ACCENTS: Readonly<Record<string, string>> = { | |
| 44 | '"': "̈", "'": "́", "`": "̀", "^": "̂", "~": "̃", "=": "̄", ".": "̇", | |
| 45 | u: "̆", v: "̌", H: "̋", c: "̧", k: "̨", r: "̊", d: "̣", b: "̱", | |
| 46 | }; | |
| 47 | const LETTERS: Readonly<Record<string, string>> = { | |
| 48 | ss: "ß", o: "ø", O: "Ø", l: "ł", L: "Ł", ae: "æ", AE: "Æ", oe: "œ", OE: "Œ", aa: "å", AA: "Å", i: "ı", j: "ȷ", | |
| 49 | }; | |
| 50 | const SYMBOLS: Readonly<Record<string, string>> = { | |
| 51 | "&": "&", "%": "%", $: "$", "#": "#", _: "_", "{": "{", "}": "}", "\\": " ", " ": " ", ",": " ", "-": "", | |
| 52 | }; | |
| 53 | ||
| 54 | /** TeX text as plain text: accents composed, braces dropped, `--` as a dash. */ | |
| 55 | export function unTeX(s: string): string { | |
| 56 | return piece(s).replace(/\s+/g, " ").trim(); | |
| 57 | } | |
| 58 | ||
| 59 | /** `unTeX` without the final tidy, so pieces joined by `#` keep their spaces. */ | |
| 60 | function piece(s: string): string { | |
| 61 | let out = ""; | |
| 62 | for (let i = 0; i < s.length; i++) { | |
| 63 | const c = s[i]!; | |
| 64 | if (c === "{" || c === "}") continue; | |
| 65 | if (c === "~") { | |
| 66 | out += " "; | |
| 67 | continue; | |
| 68 | } | |
| 69 | if (c === "-" && s[i + 1] === "-") { | |
| 70 | let n = 0; | |
| 71 | while (s[i] === "-") (i++, n++); | |
| 72 | i--; | |
| 73 | out += n >= 3 ? "—" : "–"; | |
| 74 | continue; | |
| 75 | } | |
| 76 | if (c !== "\\") { | |
| 77 | out += c; | |
| 78 | continue; | |
| 79 | } | |
| 80 | const next = s[i + 1]; | |
| 81 | if (next === undefined) break; | |
| 82 | if (/[A-Za-z]/.test(next)) { | |
| 83 | let j = i + 1; | |
| 84 | while (j < s.length && /[A-Za-z]/.test(s[j]!)) j++; | |
| 85 | const name = s.slice(i + 1, j); | |
| 86 | i = j - 1; | |
| 87 | const letter = LETTERS[name]; | |
| 88 | if (letter !== undefined) { | |
| 89 | out += letter; | |
| 90 | if (s[i + 1] === " ") i++; | |
| 91 | continue; | |
| 92 | } | |
| 93 | if (ACCENTS[name] !== undefined && name.length === 1) { | |
| 94 | const [ch, used] = accentTarget(s, i + 1); | |
| 95 | out += (ch + ACCENTS[name]).normalize("NFC"); | |
| 96 | i += used; | |
| 97 | continue; | |
| 98 | } | |
| 99 | // An unknown command (\textit, \emph …): its name drops, its argument stays. | |
| 100 | if (s[i + 1] === " ") i++; | |
| 101 | continue; | |
| 102 | } | |
| 103 | if (ACCENTS[next] !== undefined) { | |
| 104 | const [ch, used] = accentTarget(s, i + 2); | |
| 105 | out += (ch + ACCENTS[next]!).normalize("NFC"); | |
| 106 | i += 1 + used; | |
| 107 | continue; | |
| 108 | } | |
| 109 | out += SYMBOLS[next] ?? next; | |
| 110 | i++; | |
| 111 | } | |
| 112 | return out; | |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * The letter an accent applies to, starting at `at` (`{\'e}`, `\'{e}`, `\' e`, `\'\i`): [letter, | |
| 117 | * chars used]. | |
| 118 | */ | |
| 119 | function accentTarget(s: string, at: number): [string, number] { | |
| 120 | let j = at; | |
| 121 | while (s[j] === " ") j++; | |
| 122 | let braced = false; | |
| 123 | if (s[j] === "{") { | |
| 124 | braced = true; | |
| 125 | j++; | |
| 126 | } | |
| 127 | let ch = ""; | |
| 128 | if (s[j] === "\\" && (s[j + 1] === "i" || s[j + 1] === "j")) { | |
| 129 | ch = s[j + 1]!; | |
| 130 | j += 2; | |
| 131 | } else if (s[j] !== undefined && s[j] !== "}" && s[j] !== "\\") { | |
| 132 | ch = s[j]!; | |
| 133 | j++; | |
| 134 | } | |
| 135 | if (braced && s[j] === "}") j++; | |
| 136 | else if (braced && ch === "") j = at; | |
| 137 | return [ch, j - at]; | |
| 138 | } | |
| 139 | ||
| 140 | class Stop extends Error {} | |
| 141 | ||
| 142 | /** Read a bibliography. Pure; never throws. */ | |
| 143 | export function parseBib(source: string): BibDoc { | |
| 144 | const src = source.replace(/^/, ""); | |
| 145 | const macros = new Map<string, string>([ | |
| 146 | ["jan", "January"], ["feb", "February"], ["mar", "March"], ["apr", "April"], ["may", "May"], ["jun", "June"], | |
| 147 | ["jul", "July"], ["aug", "August"], ["sep", "September"], ["oct", "October"], ["nov", "November"], ["dec", "December"], | |
| 148 | ]); | |
| 149 | const items: BibItem[] = []; | |
| 150 | let total = 0; | |
| 151 | let truncated = false; | |
| 152 | let pos = 0; | |
| 153 | let steps = 0; | |
| 154 | const n = src.length; | |
| 155 | ||
| 156 | const tick = (k: number) => { | |
| 157 | steps += k; | |
| 158 | if (steps > MAX_STEPS) throw new Stop(); | |
| 159 | }; | |
| 160 | const skipWs = () => { | |
| 161 | while (pos < n && /\s/.test(src[pos]!)) pos++; | |
| 162 | }; | |
| 163 | ||
| 164 | /** From an opening `{` at `pos`: the raw inside, or undefined when it never closes. */ | |
| 165 | const braced = (): string | undefined => { | |
| 166 | let depth = 0; | |
| 167 | const start = pos + 1; | |
| 168 | for (; pos < n; pos++) { | |
| 169 | const c = src[pos]!; | |
| 170 | if (c === "\\") pos++; | |
| 171 | else if (c === "{") depth++; | |
| 172 | else if (c === "}" && --depth === 0) { | |
| 173 | tick(pos - start + 1); | |
| 174 | return src.slice(start, pos++); | |
| 175 | } | |
| 176 | } | |
| 177 | tick(n - start); | |
| 178 | return undefined; | |
| 179 | }; | |
| 180 | ||
| 181 | /** One field value: pieces joined by `#`, each a `{…}`, `"…"`, number or macro name. */ | |
| 182 | const value = (keep = false): { text: string } | { error: string } => { | |
| 183 | let text = ""; | |
| 184 | for (;;) { | |
| 185 | skipWs(); | |
| 186 | const c = src[pos]; | |
| 187 | if (c === "{") { | |
| 188 | const inner = braced(); | |
| 189 | if (inner === undefined) return { error: "unbalanced braces" }; | |
| 190 | text += piece(inner); | |
| 191 | } else if (c === '"') { | |
| 192 | let depth = 0; | |
| 193 | const start = ++pos; | |
| 194 | for (; pos < n; pos++) { | |
| 195 | const d = src[pos]!; | |
| 196 | if (d === "\\") pos++; | |
| 197 | else if (d === "{") depth++; | |
| 198 | else if (d === "}") depth--; | |
| 199 | else if (d === '"' && depth <= 0) break; | |
| 200 | } | |
| 201 | tick(Math.min(pos, n) - start); // the scan is paid for whether or not the quote closes | |
| 202 | if (pos >= n) return { error: "unterminated quoted value" }; | |
| 203 | text += piece(src.slice(start, pos++)); | |
| 204 | } else { | |
| 205 | const m = /^[^\s{}"#,=()@]+/.exec(src.slice(pos, pos + 80)); | |
| 206 | if (m === null) return { error: "a value was expected" }; | |
| 207 | pos += m[0].length; | |
| 208 | text += /^\d+$/.test(m[0]) ? m[0] : (macros.get(m[0].toLowerCase()) ?? m[0]); | |
| 209 | } | |
| 210 | skipWs(); | |
| 211 | if (src[pos] === "#") pos++; | |
| 212 | else return { text: keep ? text : text.replace(/\s+/g, " ").trim() }; | |
| 213 | } | |
| 214 | }; | |
| 215 | ||
| 216 | try { | |
| 217 | for (;;) { | |
| 218 | const at = src.indexOf("@", pos); | |
| 219 | if (at < 0) break; | |
| 220 | tick(at - pos + 1); | |
| 221 | pos = at + 1; | |
| 222 | const head = /^\s*([A-Za-z]+)\s*([{(])/.exec(src.slice(pos, pos + 64)); | |
| 223 | if (head === null) continue; // an @ in prose between entries | |
| 224 | const type = head[1]!.toLowerCase(); | |
| 225 | const open = head[2]!; | |
| 226 | const close = open === "{" ? "}" : ")"; | |
| 227 | pos += head[0].length; | |
| 228 | const bodyStart = pos; | |
| 229 | const rawOf = () => { | |
| 230 | const t = src.slice(at, Math.min(pos, at + RAW_CAP * 4)).trim(); | |
| 231 | return t.length > RAW_CAP ? `${t.slice(0, RAW_CAP)}…` : t; | |
| 232 | }; | |
| 233 | const fail = (reason: string) => { | |
| 234 | // Resume at the next line that opens an entry, so one broken entry costs only itself. | |
| 235 | const next = /\n\s*@[A-Za-z]+\s*[{(]/g; | |
| 236 | next.lastIndex = bodyStart; | |
| 237 | const m = next.exec(src); | |
| 238 | pos = m === null ? n : m.index; | |
| 239 | tick(pos - bodyStart); | |
| 240 | if (type !== "comment" && type !== "preamble") { | |
| 241 | total++; | |
| 242 | if (items.length < ENTRY_CAP) items.push({ kind: "broken", raw: rawOf(), reason }); | |
| 243 | else truncated = true; | |
| 244 | } | |
| 245 | }; | |
| 246 | ||
| 247 | if (type === "comment") { | |
| 248 | if (open === "{") { | |
| 249 | pos--; | |
| 250 | const inner = braced(); | |
| 251 | if (inner === undefined) pos = n; | |
| 252 | } | |
| 253 | continue; // `@comment text` with no brace runs to the next @ | |
| 254 | } | |
| 255 | if (type === "preamble") { | |
| 256 | pos--; | |
| 257 | if (src[pos] === "(") { | |
| 258 | const e = src.indexOf(")", pos); | |
| 259 | pos = e < 0 ? n : e + 1; | |
| 260 | } else if (braced() === undefined) pos = n; | |
| 261 | continue; | |
| 262 | } | |
| 263 | if (type === "string") { | |
| 264 | skipWs(); | |
| 265 | const name = /^[^\s{}"#,=()@]+/.exec(src.slice(pos, pos + 80)); | |
| 266 | if (name === null) { | |
| 267 | fail("@string has no name"); | |
| 268 | continue; | |
| 269 | } | |
| 270 | pos += name[0].length; | |
| 271 | skipWs(); | |
| 272 | if (src[pos] !== "=") { | |
| 273 | fail("@string has no ="); | |
| 274 | continue; | |
| 275 | } | |
| 276 | pos++; | |
| 277 | const v = value(true); | |
| 278 | if ("error" in v) { | |
| 279 | fail(v.error); | |
| 280 | continue; | |
| 281 | } | |
| 282 | macros.set(name[0].toLowerCase(), v.text); | |
| 283 | skipWs(); | |
| 284 | if (src[pos] === close) pos++; | |
| 285 | continue; | |
| 286 | } | |
| 287 | ||
| 288 | // An entry: key, then `name = value` pairs. | |
| 289 | skipWs(); | |
| 290 | const key = /^[^\s,{}()="#%]+/.exec(src.slice(pos, pos + 200)); | |
| 291 | if (key === null) { | |
| 292 | fail("the entry has no citation key"); | |
| 293 | continue; | |
| 294 | } | |
| 295 | pos += key[0].length; | |
| 296 | const fields = new Map<string, string>(); | |
| 297 | let reason = ""; | |
| 298 | for (;;) { | |
| 299 | skipWs(); | |
| 300 | if (src[pos] === ",") { | |
| 301 | pos++; | |
| 302 | continue; | |
| 303 | } | |
| 304 | if (src[pos] === close) { | |
| 305 | pos++; | |
| 306 | break; | |
| 307 | } | |
| 308 | if (pos >= n) { | |
| 309 | reason = "the entry is never closed"; | |
| 310 | break; | |
| 311 | } | |
| 312 | const nm = /^[A-Za-z][A-Za-z0-9_:.+-]*/.exec(src.slice(pos, pos + 80)); | |
| 313 | if (nm === null) { | |
| 314 | reason = "a field name was expected"; | |
| 315 | break; | |
| 316 | } | |
| 317 | pos += nm[0].length; | |
| 318 | skipWs(); | |
| 319 | if (src[pos] !== "=") { | |
| 320 | reason = `field “${nm[0].slice(0, 40)}” has no =`; | |
| 321 | break; | |
| 322 | } | |
| 323 | pos++; | |
| 324 | const v = value(); | |
| 325 | if ("error" in v) { | |
| 326 | reason = `field “${nm[0].slice(0, 40)}”: ${v.error}`; | |
| 327 | break; | |
| 328 | } | |
| 329 | fields.set(nm[0].toLowerCase(), v.text); | |
| 330 | } | |
| 331 | if (reason !== "") { | |
| 332 | fail(reason); | |
| 333 | continue; | |
| 334 | } | |
| 335 | tick(pos - bodyStart); | |
| 336 | total++; | |
| 337 | if (items.length >= ENTRY_CAP) { | |
| 338 | truncated = true; | |
| 339 | continue; | |
| 340 | } | |
| 341 | const f = (k: string) => fields.get(k) ?? ""; | |
| 342 | items.push({ | |
| 343 | kind: "entry", | |
| 344 | key: key[0], | |
| 345 | type, | |
| 346 | authors: f("author") || f("editor"), | |
| 347 | title: f("title"), | |
| 348 | venue: f("journal") || f("booktitle") || f("journaltitle") || f("publisher") || f("school") || f("institution"), | |
| 349 | year: f("year") || (/\b\d{4}\b/.exec(f("date"))?.[0] ?? ""), | |
| 350 | }); | |
| 351 | } | |
| 352 | } catch (e) { | |
| 353 | if (!(e instanceof Stop)) throw e; | |
| 354 | truncated = true; | |
| 355 | } | |
| 356 | return { items, total, truncated }; | |
| 357 | } | |
| 358 | ||
| 359 | /** `Knuth, Donald E. and Lamport, Leslie` as a list of names; `and` splits, a long list is cut. */ | |
| 360 | function authorList(authors: string): string { | |
| 361 | const names = authors.split(/\s+and\s+/i).map((a) => a.trim()).filter((a) => a !== ""); | |
| 362 | return names.length > 8 ? `${names.slice(0, 8).join("; ")}; et al.` : names.join("; "); | |
| 363 | } | |
| 364 | ||
| 365 | export function bib(input: RenderInput): Rendered { | |
| 366 | const doc = parseBib(new TextDecoder().decode(input.body)); | |
| 367 | if (doc.items.length === 0) return { content: html``, lines: 0, fallback: "This file holds no references." }; | |
| 368 | const rows = doc.items.map((i) => | |
| 369 | i.kind === "entry" | |
| 370 | ? html`<tr><th scope="row"><code>${i.key}</code> <span class="muted small">${i.type}</span></th><td>${i.title}</td><td>${authorList(i.authors)}</td><td>${i.venue}</td><td>${i.year}</td></tr>` | |
| 371 | : html`<tr><td colspan="5"><pre><code>${i.raw}</code></pre><p class="muted small">${i.reason}</p></td></tr>`, | |
| 372 | ); | |
| 373 | const more = doc.total - doc.items.length; | |
| 374 | const foot = | |
| 375 | doc.truncated || more > 0 | |
| 376 | ? html`<p class="muted small">${more > 0 ? `${more.toLocaleString()} more` : "More"} not shown</p>` | |
| 377 | : html``; | |
| 378 | const content = html`<div><div class="table-scroll" role="region" tabindex="0" aria-label="References"><table class="table" data-dense><thead><tr><th scope="col">Key</th><th scope="col">Title</th><th scope="col">Authors</th><th scope="col">Venue</th><th scope="col">Year</th></tr></thead><tbody>${rows}</tbody></table></div>${foot}</div>`; | |
| 379 | const t = doc.total; | |
outdated
Both doors: add the |
||
| 380 | return { content, lines: 0, summary: `${t.toLocaleString()} ${t === 1 ? "reference" : "references"} · ${fileSize(input.size)}` }; | |
| 381 | } |
src/web/render/kinds.ts +3
| @@ -70,6 +70,7 @@ export type FileKind = | ||
| 70 | 70 | | "sql" |
| 71 | 71 | | "graphql" |
| 72 | 72 | | "mbox" |
| 73 | | "bib" | |
| 73 | 74 | | "proto" |
| 74 | 75 | | "ini" |
| 75 | 76 | | "rtf" |
| @@ -155,6 +156,7 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 155 | 156 | sql: 1 * MB, |
| 156 | 157 | graphql: 1 * MB, |
| 157 | 158 | mbox: 5 * MB, |
| 159 | bib: 1 * MB, | |
| 158 | 160 | proto: 1 * MB, |
| 159 | 161 | ini: 1 * MB, |
| 160 | 162 | rtf: 5 * MB, |
| @@ -313,6 +315,7 @@ const TEXTUAL: Readonly<Record<string, FileKind>> = { | ||
| 313 | 315 | graphql: "graphql", |
| 314 | 316 | gql: "graphql", |
| 315 | 317 | mbox: "mbox", |
| 318 | bib: "bib", | |
| 316 | 319 | proto: "proto", |
| 317 | 320 | }; |
| 318 | 321 |
src/web/render/renderers.ts +2
| @@ -14,6 +14,7 @@ import { envView } from "./env.ts"; | ||
| 14 | 14 | import { sqlView } from "./sql.ts"; |
| 15 | 15 | import { graphql } from "./graphql.ts"; |
| 16 | 16 | import { mbox } from "./mbox.ts"; |
| 17 | import { bib } from "./bib.ts"; | |
| 17 | 18 | import { proto } from "./proto.ts"; |
| 18 | 19 | import { ini } from "./ini.ts"; |
| 19 | 20 | import { rtf } from "./rtf.ts"; |
| @@ -165,6 +166,7 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = { | ||
| 165 | 166 | sql: sqlView, |
| 166 | 167 | graphql, |
| 167 | 168 | mbox, |
| 169 | bib, | |
| 168 | 170 | proto, |
| 169 | 171 | ini, |
| 170 | 172 | rtf, |
test/bib-render.test.ts added +108
| @@ -0,0 +1,108 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { bib, ENTRY_CAP, MAX_STEPS, parseBib, unTeX } from "../src/web/render/bib.ts"; | |
| 3 | import { bibView } from "../src/tui/screens/workspace_bib.ts"; | |
| 4 | import type { Table } from "../src/text/csv.ts"; | |
| 5 | import { fileKind, kindByName, RENDER_CAP } from "../src/web/render/kinds.ts"; | |
| 6 | ||
| 7 | const enc = (s: string) => new TextEncoder().encode(s); | |
| 8 | const draw = (text: string) => { | |
| 9 | const body = enc(text); | |
| 10 | const r = bib({ path: "refs.bib", size: body.length, body, media: "", download: "" }); | |
| 11 | return { html: String(r.content), summary: r.summary, fallback: r.fallback }; | |
| 12 | }; | |
| 13 | const entries = (text: string) => parseBib(text).items; | |
| 14 | ||
| 15 | describe("bib kind", () => { | |
| 16 | test("by extension", () => { | |
| 17 | expect(fileKind("refs.bib", enc("@article{a,}"))).toBe("bib"); | |
| 18 | expect(kindByName("doc/refs.bib")).toBe("bib"); | |
| 19 | }); | |
| 20 | }); | |
| 21 | ||
| 22 | describe("parseBib", () => { | |
| 23 | test("article", () => { | |
| 24 | const [e] = entries(`@article{knuth84, author = {Knuth, Donald E.}, title = {Literate Programming}, journal = {The Computer Journal}, year = 1984 }`); | |
| 25 | expect(e).toMatchObject({ kind: "entry", key: "knuth84", type: "article", authors: "Knuth, Donald E.", title: "Literate Programming", venue: "The Computer Journal", year: "1984" }); | |
| 26 | }); | |
| 27 | test("inproceedings uses booktitle", () => { | |
| 28 | const [e] = entries(`@InProceedings{k1,\n author="A and B",\n title="T",\n booktitle={Proc. of X},\n year={2020},\n}`); | |
| 29 | expect(e).toMatchObject({ type: "inproceedings", venue: "Proc. of X", year: "2020", authors: "A and B" }); | |
| 30 | }); | |
| 31 | test("string macros expand, and join with #", () => { | |
| 32 | const [e] = entries(`@string{jcj = "The Computer Journal"}\n@string{pre = {Proc. }}\n@article{a, journal = jcj, booktitle = pre # {Foo}, year = 1999, month = jan}`); | |
| 33 | expect(e).toMatchObject({ venue: "The Computer Journal", year: "1999" }); | |
| 34 | expect(entries(`@string{pre = {Proc. }}\n@inproceedings{a, booktitle = pre # {Foo}}`)[0]).toMatchObject({ venue: "Proc. Foo" }); | |
| 35 | }); | |
| 36 | test("@comment and @preamble are skipped, not counted", () => { | |
| 37 | const doc = parseBib(`@comment{@article{x, title={no}}}\n@comment free text\n@preamble{"\\newcommand{x}{y}"}\n@book{b, title={Yes}}`); | |
| 38 | expect(doc.total).toBe(1); | |
| 39 | expect(doc.items[0]).toMatchObject({ key: "b", title: "Yes" }); | |
| 40 | }); | |
| 41 | test("nested braces and TeX accents", () => { | |
| 42 | const [e] = entries(`@article{a, title = {The {TeX}book: {A {Deep} Look}}, author = {Erd{\\H{o}}s, Paul and M{\\"u}ller, J. and Fran\\c{c}ois and Gar{\\'\\i}a}}`); | |
| 43 | expect(e).toMatchObject({ title: "The TeXbook: A Deep Look", authors: "Erdős, Paul and Müller, J. and François and Garía" }); | |
| 44 | expect(unTeX("{\\'e}cole \\ss{} na\\\"{i}ve pages 1--2 \\&")).toBe("école ß naïve pages 1–2 &"); | |
| 45 | }); | |
| 46 | test("a broken entry is raw with a reason and the rest renders", () => { | |
| 47 | const doc = parseBib(`@article{good1, title={A}}\n@article{bad, title = {never closed, year = 2000\n\n@article{good2, title={B}}`); | |
| 48 | expect(doc.items.map((i) => i.kind)).toEqual(["entry", "broken", "entry"]); | |
| 49 | const broken = doc.items[1]!; | |
| 50 | expect(broken.kind === "broken" && broken.reason).toBe("field “title”: unbalanced braces"); | |
| 51 | expect(broken.kind === "broken" && broken.raw).toContain("@article{bad"); | |
| 52 | expect(doc.items[2]).toMatchObject({ key: "good2" }); | |
| 53 | }); | |
| 54 | test("a field with no = is broken", () => { | |
| 55 | const [e] = entries(`@article{k, title}`); | |
| 56 | expect(e).toMatchObject({ kind: "broken" }); | |
| 57 | }); | |
| 58 | test("entries are capped; time is bounded", () => { | |
| 59 | const many = Array.from({ length: ENTRY_CAP + 5 }, (_, i) => `@misc{k${i}, title={t}}`).join("\n"); | |
| 60 | const doc = parseBib(many); | |
| 61 | expect(doc.items.length).toBe(ENTRY_CAP); | |
| 62 | expect(doc.total).toBe(ENTRY_CAP + 5); | |
| 63 | const t = Date.now(); | |
| 64 | const hostile = parseBib("@a{k,".repeat(50_000) + "{".repeat(MAX_STEPS)); | |
| 65 | expect(Date.now() - t).toBeLessThan(2000); | |
| 66 | expect(hostile.items.length).toBeLessThanOrEqual(ENTRY_CAP); | |
| 67 | }); | |
| 68 | test("unterminated quotes on every line cost time once, not per entry", () => { | |
| 69 | const t = Date.now(); | |
| 70 | const doc = parseBib("@a{k, t=\"{\n".repeat(20_000)); | |
| 71 | expect(Date.now() - t).toBeLessThan(2000); | |
| 72 | expect(doc.truncated).toBe(true); | |
| 73 | expect(doc.items.length).toBeLessThanOrEqual(ENTRY_CAP); | |
| 74 | }); | |
| 75 | }); | |
| 76 | ||
| 77 | describe("bib", () => { | |
| 78 | test("renders a row per reference", () => { | |
| 79 | const r = draw(`@article{k, author={A and B}, title={T}, journal={J}, year={2001}}`); | |
| 80 | expect(r.html).toContain("<code>k</code>"); | |
| 81 | expect(r.html).toContain("A; B"); | |
| 82 | expect(r.summary).toContain("1 reference"); | |
| 83 | }); | |
| 84 | test("markup in a title is escaped", () => { | |
| 85 | const r = draw(`@article{"><img src=x>, title = {<script>alert(1)</script> {\\textbf{x}}}, author = {<b>a</b>}, journal={<i>j</i>}}\n@article{bad, title={<img onerror=1>`); | |
| 86 | expect(r.html).not.toContain("<script"); | |
| 87 | expect(r.html).not.toContain("<img"); | |
| 88 | expect(r.html).not.toContain("<b>a"); | |
| 89 | expect(r.html).not.toContain("<i>j"); | |
| 90 | expect(r.html).toContain("<script>"); | |
| 91 | }); | |
| 92 | test("nothing to draw falls back", () => { | |
| 93 | expect(draw("just prose").fallback).toBeDefined(); | |
| 94 | }); | |
| 95 | }); | |
| 96 | ||
| 97 | describe("bibView (terminal)", () => { | |
| 98 | test("one row per reference, from the same parser", () => { | |
| 99 | const v = bibView("refs.bib", enc("@article{k, author={A}, title={T}, journal={J}, year=2001}")) as Table; | |
| 100 | expect(v.header).toEqual(["Key", "Title", "Authors", "Venue", "Year"]); | |
| 101 | expect(v.rows).toEqual([["k (article)", "T", "A", "J", "2001"]]); | |
| 102 | }); | |
| 103 | test("not a bib, empty, and too big", () => { | |
| 104 | expect(bibView("a.txt", enc("@a{k}"))).toBeNull(); | |
| 105 | expect(bibView("refs.bib", enc("nothing"))).toBe("This file holds no references."); | |
| 106 | expect(typeof bibView("refs.bib", new Uint8Array(RENDER_CAP.bib + 1).fill(32))).toBe("string"); | |
| 107 | }); | |
| 108 | }); |
test/fuzz-renderers.test.ts +6
| @@ -194,6 +194,12 @@ export const SAMPLES: Readonly<Record<string, { path: string; files: readonly st | ||
| 194 | 194 | "From [email protected] Mon Jan 1 00:00:00 2024\nFrom: Ann <[email protected]>\nSubject: =?UTF-8?B?aGVsbG8=?=\n <b>world</b>\nDate: Mon, 1 Jan 2024\n\n>From body line\n\nFrom [email protected] Tue\nFrom: Bob\nSubject: two\n\nx\n", |
| 195 | 195 | ], |
| 196 | 196 | }, |
| 197 | bib: { | |
| 198 | path: "refs.bib", | |
| 199 | files: [ | |
| 200 | '@string{j = "J. <b>X</b>"}\n@comment{x}\n@article{k1, author = {Erd{\\H{o}}s, P. and M{\\"u}ller}, title = {A {B} <i>c</i>}, journal = j # {!}, year = 2001}\n@inproceedings{k2, booktitle = "Proc", year = {2}}\n@article{bad, title = {open\n', | |
| 201 | ], | |
| 202 | }, | |
| 197 | 203 | proto: { |
| 198 | 204 | path: "user.proto", |
| 199 | 205 | files: [ |
test/render-kinds.test.ts +1
| @@ -111,6 +111,7 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = { | ||
| 111 | 111 | graphql: [enc("type A { b: Int }\n"), "graphql", "application/octet-stream"], |
| 112 | 112 | gql: [enc("type A { b: Int }\n"), "graphql", "application/octet-stream"], |
| 113 | 113 | mbox: [enc("From [email protected] Mon Jan 1 00:00:00 2024\nSubject: x\n\nhi\n"), "mbox", "application/octet-stream"], |
| 114 | bib: [enc("@article{k, title={T}}\n"), "bib", "application/octet-stream"], | |
| 114 | 115 | properties: [enc("a=b\n"), "ini", "application/octet-stream"], |
| 115 | 116 | rtf: [enc("{\\rtf1 hi}"), "rtf", "application/octet-stream"], |
| 116 | 117 | xml: [enc("<a/>"), "xml", "application/octet-stream"], |
new above
offline
Principle 7/3 and phone use: six dense columns in one table scroll sideways at 360px, so the title (the thing people read) can be off-screen. Fold Type into the key cell as muted text and let Title wrap first; and a broken entry's
<pre>should sit in the same row pattern. Also the secondaria-label="References"on the table repeats the region's label (principle 1: nothing says itself twice); drop one.