From 87b011191514af3730a78142f59a0b4eb6e13f39 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 1 Mar 2023 18:16:17 +0100 Subject: [PATCH] Work on multiple slides --- index.js | 127 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 76 insertions(+), 51 deletions(-) diff --git a/index.js b/index.js index a7c7def..a611d38 100644 --- a/index.js +++ b/index.js @@ -231,65 +231,90 @@ async function main() { await page.setViewport(size); await page.goto('file://' + path); - // Only consider the first slide (#\\331 === #1, which is the id of the first slide) - // We don't take into account the other slides because it will mess up with our screenshot varification - let root = await page.$('#\\31'); + let output = []; - // If there is no slide, try to run on HTML body - if (root === null) { - error('not a marp HTML file'); - process.exit(1); - } + for (let pageIndex = 1;; pageIndex++) { - // Hide slides controls - await page.evaluate(() => { - for (let elt of document.getElementsByClassName('bespoke-marp-osc')) { - elt.style.visibility = "hidden"; + let root = await page.$('#\\3' + pageIndex); + if (root === null) { + break; } - }); - // Take a first screenshot - await page.screenshot({path: (outputDir === null ? __dirname : outputDir) + '/' + 'screenshot1.png'}); + info("analysing slide " + pageIndex); + + // Hide slides controls + await page.evaluate(() => { + for (let elt of document.getElementsByClassName('bespoke-marp-osc')) { + elt.style.visibility = "hidden"; + } + }); + + // Take a first screenshot + await page.screenshot({path: (outputDir === null ? __dirname : outputDir) + '/' + 'screenshot1.png'}); + + // Edit the page to shrink elements in order to get better bounding boxes + if (shrinkBoxes) { + info("shrinking bounding boxes"); + await addSpan(root); + info("boundingboxes shrunk"); + eprintln(); + } + + // Take another screenshot and check the modification we made didn't change the layout of the page + await page.screenshot({path: (outputDir === null ? __dirname : outputDir) + '/' + 'screenshot2.png'}); + + // Compare both screenshots + let file1 = await fs.readFile(__dirname + '/' + 'screenshot1.png'); + let file2 = await fs.readFile(__dirname + '/' + 'screenshot2.png'); + let filesAreSame = file1.map((x, i) => x === file2[i]).reduce((a, b) => a && b, true); + + if (!filesAreSame) { + // Check psnr + let psnr = await image.psnr(__dirname + '/' + 'screenshot1.png', __dirname + '/' + 'screenshot2.png'); + + // Crash if they're different + if (psnr > 70) { + warning(filename + " produced slight diff: psnr = " + psnr); + } else { + await error("page edit changed the layout: psnr = " + psnr); + process.exit(1); + } + } + + // Analyse the root and output the result + info("performing analysis"); + let analyse = await analyseElement(root, page, outputDir, threshold); + info("analysis done"); + + // Append the current slide to the output + if (flatten) { + + analyse = flattenTree(analyse); + for (let elt of anaylse) { + output.push(elt); + } + + } else { + + output.push(analyse); + + } + + // Go to the next page + await page.evaluate(el => { + let buttons = document.getElementsByTagName('button'); + for (let button of buttons) { + if (button.title === "Next slide") { + button.click(); + } + } + }); - // Edit the page to shrink elements in order to get better bounding boxes - if (shrinkBoxes) { - info("shrinking bounding boxes"); - await addSpan(root); - info("boundingboxes shrunk"); eprintln(); } - // Take another screenshot and check the modification we made didn't change the layout of the page - await page.screenshot({path: (outputDir === null ? __dirname : outputDir) + '/' + 'screenshot2.png'}); - - // Compare both screenshots - let file1 = await fs.readFile(__dirname + '/' + 'screenshot1.png'); - let file2 = await fs.readFile(__dirname + '/' + 'screenshot2.png'); - let filesAreSame = file1.map((x, i) => x === file2[i]).reduce((a, b) => a && b, true); - - if (!filesAreSame) { - // Check psnr - let psnr = await image.psnr(__dirname + '/' + 'screenshot1.png', __dirname + '/' + 'screenshot2.png'); - - // Crash if they're different - if (psnr > 70) { - warning(filename + " produced slight diff: psnr = " + psnr); - } else { - await error("page edit changed the layout: psnr = " + psnr); - process.exit(1); - } - } - - // Analyse the root and output the result - info("performing analysis"); - let analyse = await analyseElement(root, page, outputDir, threshold); - info("analysis done"); - - if (flatten) { - analyse = flattenTree(analyse); - } - - let json = JSON.stringify(analyse, undefined, 4); + info("saving output"); + let json = JSON.stringify(output, undefined, 4); if (outputDir === null) { console.log(json); } else {