Update processing and add try-catch per page

This commit is contained in:
D 2021-11-17 16:27:38 +01:00
parent df651c1f89
commit 1334c96aaa
5 changed files with 36 additions and 20 deletions

View file

@ -18,22 +18,30 @@ export const PAGE_VERSIONS = {
const MAX_WIDTH = 440; const MAX_WIDTH = 440;
export function processHTML(root: HTMLElement, docname: string): void { export function processHTML(root: HTMLElement, docname: string): void {
processGlobal(root, docname); try {
processGlobal(root, docname);
} catch (e) {
console.error(`Error processing page: ${docname}`);
}
switch (docname) { try {
case "Guide_to_chemistry": switch (docname) {
processChemistry(root); case "Guide_to_chemistry":
break; processChemistry(root);
case "Infections": break;
processVirology(root); case "Infections":
break; processVirology(root);
case "Guide_to_food": break;
processFood(root); case "Guide_to_food":
break; processFood(root);
case "Guide_to_drinks": break;
processDrinks(root); case "Guide_to_drinks":
break; processDrinks(root);
default: break;
default:
}
} catch (e) {
console.error(`Error processing page: ${docname} (specific enhancements)`);
} }
} }

View file

@ -204,11 +204,11 @@ export function chemistryScript(root: HTMLElement): void {
); );
registerSearchEntries( registerSearchEntries(
el.map((element, id) => ({ Array.from(root.querySelectorAll<HTMLElement>("table.wikitable > tbody > tr:not(:first-child) th .reagent-header")).map((element, id) => ({
page: "Guide_to_chemistry", page: "Guide_to_chemistry",
name: element name: element
.querySelector("th .reagent-header")
.textContent.trim() .textContent.trim()
.replace(/\n.+$/gm, "")
.replace("▮", ""), .replace("▮", ""),
element, element,
alignment: "center", alignment: "center",

View file

@ -41,6 +41,7 @@ export function processFood(root: HTMLElement): void {
]; ];
baseFoodTables.forEach(({ selector, title, process }) => { baseFoodTables.forEach(({ selector, title, process }) => {
const table = root.querySelector<HTMLElement>(`${selector} .wikitable`); const table = root.querySelector<HTMLElement>(`${selector} .wikitable`);
if (!table) return;
const foods = parseTable(table).map((row) => { const foods = parseTable(table).map((row) => {
const foodBlock = document.createElement("td"); const foodBlock = document.createElement("td");
foodBlock.innerHTML = `<div class="food-block"> foodBlock.innerHTML = `<div class="food-block">
@ -108,6 +109,7 @@ export function processFood(root: HTMLElement): void {
]; ];
foodRecipesTables.forEach((selector) => { foodRecipesTables.forEach((selector) => {
const table = root.querySelector<HTMLElement>(`${selector} .wikitable`); const table = root.querySelector<HTMLElement>(`${selector} .wikitable`);
if (!table) return;
const recipes = parseTable(table).map((row) => { const recipes = parseTable(table).map((row) => {
const foodBlock = document.createElement("td"); const foodBlock = document.createElement("td");
foodBlock.innerHTML = ` foodBlock.innerHTML = `

View file

@ -2,7 +2,7 @@ import { registerSearchEntries } from "../search";
export function genericScript(root: HTMLElement, docname: string): void { export function genericScript(root: HTMLElement, docname: string): void {
const el = Array.from( const el = Array.from(
root.querySelectorAll<HTMLElement>("div.mw-headline-cont[id][data-name]") root.querySelectorAll<HTMLElement>(".mw-headline-cont[id][data-name]")
); );
// Init fuzzy search with headlines // Init fuzzy search with headlines

View file

@ -75,7 +75,10 @@ export function processGlobal(root: HTMLElement, docname: string): void {
if (toc) { if (toc) {
const tocHeader = toc.querySelector("h2"); const tocHeader = toc.querySelector("h2");
toc.parentNode.insertBefore(tocHeader, toc); toc.parentNode.insertBefore(tocHeader, toc);
toc.removeChild(toc.querySelector("#toctitle")); const tocTitle = toc.querySelector("#toctitle")
if (tocTitle != null) {
toc.removeChild(tocTitle);
}
} }
// Group headers and content so stickies don't overlap // Group headers and content so stickies don't overlap
@ -99,10 +102,13 @@ export function processGlobal(root: HTMLElement, docname: string): void {
const container = findParent(span, (el) => const container = findParent(span, (el) =>
el.classList.contains("mw-headline-cont") el.classList.contains("mw-headline-cont")
); );
if (container) { if (container && container.querySelectorAll<HTMLElement>(".mw-headline").length === 1) {
container.id = span.id; container.id = span.id;
span.id += "-span"; span.id += "-span";
container.dataset.name = span.textContent; container.dataset.name = span.textContent;
} else {
span.dataset.name = span.textContent;
span.classList.add("mw-headline-cont");
} }
}); });
} }