|
| 1 | +import { MiniDOM } from './minidom.ts'; |
| 2 | +import * as fs from 'node:fs/promises'; |
| 3 | + |
| 4 | +type FileName = string; |
| 5 | + |
| 6 | +/** The data extracted from the minutes that is supposed to be displayed */ |
| 7 | +interface DisplayedData { |
| 8 | + /** The File name of the minutes */ |
| 9 | + fname: FileName; |
| 10 | + /** Date of the minutes */ |
| 11 | + date: Date; |
| 12 | + /** Array of TOC entries (can be HTML) */ |
| 13 | + toc: string[]; |
| 14 | + /** Array of Resolution entries entries (can be HTML) */ |
| 15 | + res: string[]; |
| 16 | +} |
| 17 | + |
| 18 | +/** The data regrouped per years */ |
| 19 | +export type GroupedData = Map<number, DisplayedData[]>; |
| 20 | + |
| 21 | +/** Files names that must be ignored in the directory of minutes, if there */ |
| 22 | +const ignoredFiles: string[] = ["index.html", "resolutions.html"]; |
| 23 | + |
| 24 | +/** |
| 25 | + * Get the file names of all the minutes for a given WG. |
| 26 | + * The file names are relative to the directory. |
| 27 | + * |
| 28 | + * |
| 29 | + * @param directory |
| 30 | + * @returns |
| 31 | + */ |
| 32 | +async function getMinutes(directory: string): Promise<FileName[]> { |
| 33 | + return (await fs.readdir(directory)) |
| 34 | + .filter(file => !ignoredFiles.includes(file)) |
| 35 | + .map(file => `${directory}/${file}`); |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +/** |
| 40 | + * Get all TOCs and resolutions with the respective date; one block each that can |
| 41 | + * be displayed in the generated HTML |
| 42 | + * |
| 43 | + * @param minutes |
| 44 | + * @returns |
| 45 | + */ |
| 46 | +async function getAllData(minutes: FileName[]): Promise<DisplayedData[]> { |
| 47 | + /* |
| 48 | + * Extract a list of <li> entries from the content of a minutes file, using a CSS selector. |
| 49 | + */ |
| 50 | + const extractListEntries = (fname: FileName, content: MiniDOM, selector: string): string[] => { |
| 51 | + // There is only one cleanup operation for now, but it could be extended if needed |
| 52 | + const cleanupData = (nav: string): string => { |
| 53 | + return nav |
| 54 | + // References should not be relative |
| 55 | + .replace(/href="#/g, `target="_blank" href="${fname}#`) |
| 56 | + ; |
| 57 | + }; |
| 58 | + |
| 59 | + const resLines: NodeListOf<Element> = content.querySelectorAll(selector); |
| 60 | + |
| 61 | + if (resLines.length === 0) { |
| 62 | + return []; |
| 63 | + } else { |
| 64 | + return Array.from(resLines) |
| 65 | + // Get the HTML line corresponding to an 'li' element |
| 66 | + .map((line: Element) => line.innerHTML) |
| 67 | + // Cleanup each the line before returning it |
| 68 | + .map(cleanupData); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Get the data for a single entry; the Promises are collected in an array |
| 73 | + // for a parallel execution via Promise.allSettled. |
| 74 | + const retrieveDisplayData = async (fname: FileName): Promise<DisplayedData> => { |
| 75 | + // Get the minutes file as a text |
| 76 | + const response = await fs.readFile(fname, "utf-8"); |
| 77 | + // Parse the (HTML) text into a MiniDOM |
| 78 | + const content = new MiniDOM(response); |
| 79 | + |
| 80 | + // Find the date of the minutes |
| 81 | + const date_title :string | null | undefined = content.querySelector("header h2:first-of-type")?.textContent; |
| 82 | + const date = new Date(date_title ?? "1970-01-01"); |
| 83 | + |
| 84 | + return { |
| 85 | + fname : fname, |
| 86 | + date : date, |
| 87 | + toc : extractListEntries(fname, content, "#toc ol li"), |
| 88 | + res : extractListEntries(fname, content, "#ResolutionSummary ol li"), |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + // Gather all the Promises for a parallel execution |
| 93 | + const promises: Promise<DisplayedData>[] = minutes.map(retrieveDisplayData); |
| 94 | + |
| 95 | + // Some of the promises might have failed, so we need to filter those out. |
| 96 | + // But we want to display everything we can... |
| 97 | + const results : PromiseSettledResult<DisplayedData>[] = await Promise.allSettled(promises); |
| 98 | + const output = results |
| 99 | + .filter((result) => result.status === "fulfilled") |
| 100 | + .map((result) => result.value); |
| 101 | + |
| 102 | + // Sorting the output by date before returning it |
| 103 | + return output.sort((a, b) => { |
| 104 | + if (a.date > b.date) return -1; |
| 105 | + if (a.date < b.date) return 1; |
| 106 | + else return 0; |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +/** |
| 112 | + * Main entry point to get the Data grouped by year. The data themselves are arrays of strings, in HTML format. |
| 113 | + * |
| 114 | + * @param directory |
| 115 | + * @returns |
| 116 | + */ |
| 117 | +export async function getGroupedData(directory: string): Promise<GroupedData> { |
| 118 | + const groupDisplayedDataByYear = (data: DisplayedData[]): GroupedData => { |
| 119 | + const groups: GroupedData = new Map<number, DisplayedData[]>(); |
| 120 | + for (const entry of data) { |
| 121 | + const year = entry.date.getFullYear(); |
| 122 | + if (!groups.has(year)) { |
| 123 | + groups.set(year, []); |
| 124 | + } |
| 125 | + groups.get(year)?.push(entry); |
| 126 | + } |
| 127 | + return groups; |
| 128 | + } |
| 129 | + |
| 130 | + // Get the references to all the minutes |
| 131 | + const minutes: FileName[] = await getMinutes(directory); |
| 132 | + // For each of the minutes, get the content. |
| 133 | + const display: DisplayedData[] = await getAllData(minutes); |
| 134 | + return groupDisplayedDataByYear(display); |
| 135 | +} |
0 commit comments