-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprecomputed.ts
75 lines (63 loc) · 2.63 KB
/
precomputed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {Book} from "./books";
import {readUtf8} from "./files";
const SHULCHAN_ARUKH_HEADERS: any = (
JSON.parse(readUtf8("precomputed/shulchan_arukh_headings.json"))
);
export function shulchanArukhChapterTitle(ref: string): string | undefined {
const bookAndChapter = ref.split("Shulchan Arukh, ")[1].split(":")[0];
const separatorIndex = bookAndChapter.lastIndexOf(" ");
const book = bookAndChapter.slice(0, separatorIndex);
const chapter = bookAndChapter.slice(separatorIndex + 1);
if (book.includes(", Seder ")) {
return undefined;
}
return SHULCHAN_ARUKH_HEADERS[book][chapter]?.replace(/\.$/, "");
}
const SUGYA_POINTERS_CACHE: Record<string, Record<string, any>> = {};
export function getSugyaSpanningRefWithDefiniteCacheHit(
masechet: string, ref: string,
): string | undefined {
return SUGYA_POINTERS_CACHE[masechet][ref];
}
export function getSugyaSpanningRef(masechet: string, ref: string): string | undefined {
if (masechet in SUGYA_POINTERS_CACHE) {
const result = getSugyaSpanningRefWithDefiniteCacheHit(masechet, ref);
// It's already a spanning ref, but Sefaria doesn't provide the span ourselves, so let's try
// to create it ourselves.
if (!result && ref.includes("-")) {
// TODO: there's more sophisticated work that can be done here, but the complexity doesn't
// seem like it's worth it.
return getSugyaSpanningRefWithDefiniteCacheHit(masechet, ref.slice(0, ref.indexOf("-")));
}
return result;
}
let text: string;
try {
text = readUtf8(`precomputed/sugyot/pointers/${masechet}-pointers.json`);
} catch {
SUGYA_POINTERS_CACHE[masechet] = {};
return undefined;
}
SUGYA_POINTERS_CACHE[masechet] = JSON.parse(text);
return getSugyaSpanningRef(masechet, ref);
}
export function mishnaReferencePath(book: Book): string {
return `precomputed/mishna_references/${book.canonicalName}.json`;
}
const MISHNA_REFERENCES_CACHE: Record<string, Record<string, Record<string, string>>> = {};
export function mishnaReferencesForPage(book: Book, page: string): Record<string, string> {
if (book.canonicalName in MISHNA_REFERENCES_CACHE) {
return MISHNA_REFERENCES_CACHE[book.canonicalName][page] ?? {};
}
MISHNA_REFERENCES_CACHE[book.canonicalName] = JSON.parse(readUtf8(mishnaReferencePath(book)));
return mishnaReferencesForPage(book, page);
}
const SEGMENT_COUNT_CACHE: Record<string, number> = {};
export function segmentCount(pageRef: string): number {
if (Object.keys(SEGMENT_COUNT_CACHE).length === 0) {
Object.assign(
SEGMENT_COUNT_CACHE,
JSON.parse(readUtf8("precomputed/segmentsPerPage.json")));
}
return SEGMENT_COUNT_CACHE[pageRef];
}