Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 242x 242x 242x 37x 1x 1x 1x 1x 19x 19x 37x 37x 19x 18x 19x 19x 16x 19x 15x 15x 1x 3x 31x 31x 22x 22x 22x 1x 1x 46x 41x 24x 24x 24x 8x 8x 7x 7x 7x 22x 22x 22x 22x 7x 7x | import type { CourseDefinition } from './definition.ts';
import { parseTableHeaderCourses } from './table.ts';
/** Fetches a URL's body as text; injected so this layer stays runtime-neutral (no global `fetch`). */
export type TableFetchText = (url: string) => Promise<string>;
/** The courses a difficulty table publishes, under the label its folder bar shows. */
export interface TableHeaderCourses {
/** The table's `name`, else its `symbol`, else the host it was fetched from. */
label: string;
/** Every course of the header, its groups flattened in order. */
courses: CourseDefinition[];
/** The header JSON's resolved URL. */
headerURL: string;
/** The header's `data_url` (the table's song list) resolved against {@link headerURL}, when it names a valid one. */
dataURL?: string;
}
const META_TAG_PATTERN = /<meta\b([^>]*)>/giu;
const ATTRIBUTE_PATTERN = /([^\s"'=/>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/gu;
const HTML_ENTITIES: Readonly<Record<string, string>> = { amp: '&', quot: '"', apos: "'", lt: '<', gt: '>' };
function decodeHTMLEntities(text: string): string {
return text.replace(/&(#x[0-9a-f]+|#[0-9]+|[a-z]+);/giu, (entity, body: string) => {
const lower = body.toLowerCase();
Iif (lower.startsWith('#x')) return String.fromCodePoint(Number.parseInt(lower.slice(2), 16));
Iif (lower.startsWith('#')) return String.fromCodePoint(Number.parseInt(lower.slice(1), 10));
return HTML_ENTITIES[lower] ?? entity;
});
}
function readAttributes(source: string): Map<string, string> {
const attributes = new Map<string, string>();
for (const match of source.matchAll(ATTRIBUTE_PATTERN)) {
const name = match[1]!.toLowerCase();
Eif (!attributes.has(name)) attributes.set(name, decodeHTMLEntities(match[2] ?? match[3] ?? match[4] ?? ''));
}
return attributes;
}
/**
* The header JSON URL a difficulty-table page names in `<meta name="bmstable" content="header.json">`, resolved
* against the page URL. Attribute order, quoting and case do not matter. `undefined` when the page has no such tag or
* its content is not a valid URL.
*/
export function resolveTableHeaderURL(pageURL: string, html: string): string | undefined {
for (const match of html.matchAll(META_TAG_PATTERN)) {
const attributes = readAttributes(match[1]!);
if (attributes.get('name')?.trim().toLowerCase() !== 'bmstable') continue;
const content = attributes.get('content')?.trim();
if (content === undefined || content.length === 0) continue;
try {
return new URL(content, pageURL).href;
} catch {
return undefined;
}
}
return undefined;
}
function parseJSONObject(text: string): Record<string, unknown> | undefined {
// Strip a UTF-8 BOM; some tables serve one.
const trimmed = text.replace(/^/u, '').trim();
if (!trimmed.startsWith('{')) return undefined;
try {
const value: unknown = JSON.parse(trimmed);
return value !== null && typeof value === 'object' && !Array.isArray(value)
? (value as Record<string, unknown>)
: undefined;
} catch {
return undefined;
}
}
function hostOf(url: string): string {
try {
return new URL(url).host || url;
} catch {
return url;
}
}
function nonEmptyString(value: unknown): string | undefined {
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
}
/**
* Loads a difficulty table's courses from its page URL (whose `bmstable` meta names the header JSON) or from the header
* JSON URL itself, and parses them with {@link parseTableHeaderCourses}. Only the header is fetched: the song list
* behind `data_url` is not. Throws when the page names no header or the header is not a JSON object; fetch failures
* propagate from `fetchText`.
*/
export async function fetchTableHeaderCourses(url: string, fetchText: TableFetchText): Promise<TableHeaderCourses> {
const first = await fetchText(url);
let headerURL = url;
let header = parseJSONObject(first);
if (header === undefined) {
const resolved = resolveTableHeaderURL(url, first);
if (resolved === undefined) throw new Error(`No difficulty-table header (bmstable meta) found at ${url}`);
headerURL = resolved;
header = parseJSONObject(await fetchText(resolved));
if (header === undefined) throw new Error(`The difficulty-table header at ${resolved} is not a JSON object`);
}
const dataURL = resolveDataURL(header.data_url, headerURL);
return {
label: nonEmptyString(header.name) ?? nonEmptyString(header.symbol) ?? hostOf(url),
courses: parseTableHeaderCourses(header).flat(),
headerURL,
...(dataURL !== undefined ? { dataURL } : {}),
};
}
function resolveDataURL(value: unknown, headerURL: string): string | undefined {
const raw = nonEmptyString(value);
if (raw === undefined) return undefined;
try {
return new URL(raw, headerURL).href;
} catch {
return undefined;
}
}
|