From 51f758bf47345d47db72c093d1cf2ab479ad0b7d Mon Sep 17 00:00:00 2001 From: David Bolack Date: Thu, 15 Aug 2024 17:15:49 -0500 Subject: [PATCH 01/11] Rework page counters for skipping and resets. Solves #513 This adds the .skipCounting and .resetCounting classes for causing a page number to not be incremented or to reset the number at 1. The ToC Snippet is corrected to match the displayed page numbers while correctly tracking the page ids. --- .../V3/5ePHB/snippets/tableOfContents.gen.js | 49 ++++++++++++++----- themes/V3/Blank/style.less | 14 ++++-- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 3aea01735..dbd0794d5 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -1,14 +1,41 @@ const _ = require('lodash'); const dedent = require('dedent-tabs').default; -const getTOC = (pages)=>{ +const getTOC = ()=>{ - const recursiveAdd = (title, page, targetDepth, child, curDepth=0)=>{ + const iframe = document.getElementById('BrewRenderer'); + const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; + const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); + const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; + + const res = []; + + const pageMap = []; + + const walkPages = ()=>{ + let current = 0; + let skip = 0; + let reset = 0; + const pages = iframeDocument.querySelectorAll('.page'); + _.each(pages, (page)=>{ + current++; + if(page.querySelectorAll('.skipCounting').length > 0) { + skip += 1; + } else if(page.querySelectorAll('.resetCounting').length > 0) { + reset = current - 1; + skip = 0; + } + pageMap[current] = current - reset - skip; + }); + }; + + const recursiveAdd = (title, page, anchor, targetDepth, child, curDepth=0)=>{ if(curDepth > 5) return; // Something went wrong. if(curDepth == targetDepth) { child.push({ title : title, page : page, + anchor : anchor, children : [] }); } else { @@ -16,26 +43,23 @@ const getTOC = (pages)=>{ child.push({ title : null, page : page, + anchor : anchor, children : [] }); } - recursiveAdd(title, page, targetDepth, _.last(child).children, curDepth+1,); + recursiveAdd(title, page, anchor, targetDepth, _.last(child).children, curDepth+1,); } }; - const res = []; - - const iframe = document.getElementById('BrewRenderer'); - const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; - const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); - const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; + walkPages(); _.each(headings, (heading)=>{ + const pageAnchor = heading.closest('.page').id; const onPage = parseInt(heading.closest('.page').id?.replace(/^p/, '')); const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC'); if(ToCExclude != 'exclude') { - recursiveAdd(heading.textContent.trim(), onPage, headerDepth.indexOf(heading.tagName), res); + recursiveAdd(heading.textContent.trim(), pageMap[onPage], pageAnchor, headerDepth.indexOf(heading.tagName), res); } }); return res; @@ -46,7 +70,7 @@ const ToCIterate = (entries, curDepth=0)=>{ const levelPad = ['- ###', ' - ####', ' - ', ' - ', ' - ', ' - ']; const toc = []; if(entries.title !== null){ - toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page}}}](#p${entries.page})`); + toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page}}}](#${entries.anchor})`); } if(entries.children.length) { _.each(entries.children, (entry, idx)=>{ @@ -60,8 +84,7 @@ const ToCIterate = (entries, curDepth=0)=>{ }; module.exports = function(props){ - const pages = props.brew.text.split('\\page'); - const TOC = getTOC(pages); + const TOC = getTOC(); const markdown = _.reduce(TOC, (r, g1, idx1)=>{ r.push(ToCIterate(g1).join('\n')); return r; diff --git a/themes/V3/Blank/style.less b/themes/V3/Blank/style.less index 0f779c38b..84bd5ded5 100644 --- a/themes/V3/Blank/style.less +++ b/themes/V3/Blank/style.less @@ -12,7 +12,7 @@ } @page { margin : 0; } -body { counter-reset : page-numbers; } +body { counter-reset : page-numbers 0; } * { -webkit-print-color-adjust : exact; } //***************************** @@ -51,7 +51,6 @@ body { counter-reset : page-numbers; } height : 279.4mm; padding : 1.4cm 1.9cm 1.7cm; overflow : hidden; - counter-increment : page-numbers; background-color : var(--HB_Color_Background); text-rendering : optimizeLegibility; contain : size; @@ -481,4 +480,13 @@ body { counter-reset : page-numbers; } &:nth-child(even) { .pageNumber { left : 30px; } } -} + + .resetCounting { + counter-set : page-numbers 1; + } + + &:not(:has(.skipCounting)) { + counter-increment : page-numbers; + } + +} \ No newline at end of file From dcc7a22272888e8af7b7ded624403ea13e033c54 Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sat, 7 Sep 2024 19:47:16 -0500 Subject: [PATCH 02/11] First pass at code fixes. Move functions out of function Use querySelector instead of querySelectorAll Flip skip and reset counter order. --- .../V3/5ePHB/snippets/tableOfContents.gen.js | 82 ++++++++++--------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index dbd0794d5..7017ac7dc 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -1,55 +1,59 @@ const _ = require('lodash'); const dedent = require('dedent-tabs').default; -const getTOC = ()=>{ +const iframe = document.getElementById('BrewRenderer'); +const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; +const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); +const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; - const iframe = document.getElementById('BrewRenderer'); - const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; - const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); - const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; +const res = []; - const res = []; +const pageMap = []; - const pageMap = []; +const walkPages = ()=>{ + let current = 0; + let skip = 0; + let reset = 0; + const pages = iframeDocument.querySelectorAll('.page'); + _.each(pages, (page)=>{ + current++; + const doSkip = (page.querySelector('.skipCounting').length > 0); + const doReset = (page.querySelector('.resetCounting').length > 0); + if(doReset) { + reset = current - 1; + skip = 0; + } else if(doSkip){ + skip += 1; + } + pageMap[current] = current - reset - skip; + }); +}; - const walkPages = ()=>{ - let current = 0; - let skip = 0; - let reset = 0; - const pages = iframeDocument.querySelectorAll('.page'); - _.each(pages, (page)=>{ - current++; - if(page.querySelectorAll('.skipCounting').length > 0) { - skip += 1; - } else if(page.querySelectorAll('.resetCounting').length > 0) { - reset = current - 1; - skip = 0; - } - pageMap[current] = current - reset - skip; +const recursiveAdd = (title, page, actualPage, targetDepth, child, curDepth=0)=>{ + const anchor = `p${actualPage}`; + if(curDepth > 5) return; // Something went wrong. + if(curDepth == targetDepth) { + child.push({ + title : title, + page : page, + anchor : anchor, + children : [] }); - }; - - const recursiveAdd = (title, page, anchor, targetDepth, child, curDepth=0)=>{ - if(curDepth > 5) return; // Something went wrong. - if(curDepth == targetDepth) { + } else { + if(child.length == 0) { child.push({ - title : title, + title : null, page : page, anchor : anchor, children : [] }); - } else { - if(child.length == 0) { - child.push({ - title : null, - page : page, - anchor : anchor, - children : [] - }); - } - recursiveAdd(title, page, anchor, targetDepth, _.last(child).children, curDepth+1,); } - }; + recursiveAdd(title, page, anchor, targetDepth, _.last(child).children, curDepth+1,); + } +}; + + +const getTOC = ()=>{ walkPages(); @@ -59,7 +63,7 @@ const getTOC = ()=>{ const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC'); if(ToCExclude != 'exclude') { - recursiveAdd(heading.textContent.trim(), pageMap[onPage], pageAnchor, headerDepth.indexOf(heading.tagName), res); + recursiveAdd(heading.textContent.trim(), pageMap[onPage], onPage, headerDepth.indexOf(heading.tagName), res); } }); return res; From 049b64cd41eac394341e4f81547d3c7f8134a669 Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sat, 7 Sep 2024 19:54:52 -0500 Subject: [PATCH 03/11] Remove unneeded variable --- themes/V3/5ePHB/snippets/tableOfContents.gen.js | 1 - 1 file changed, 1 deletion(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 7017ac7dc..2c0f5e222 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -58,7 +58,6 @@ const getTOC = ()=>{ walkPages(); _.each(headings, (heading)=>{ - const pageAnchor = heading.closest('.page').id; const onPage = parseInt(heading.closest('.page').id?.replace(/^p/, '')); const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC'); From 3b0028da69a398df11a6f992f031ce356c4db9a7 Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sat, 7 Sep 2024 20:12:05 -0500 Subject: [PATCH 04/11] Move some of thos variables back. --- themes/V3/5ePHB/snippets/tableOfContents.gen.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 2c0f5e222..2c8c3cfa4 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -1,16 +1,11 @@ const _ = require('lodash'); const dedent = require('dedent-tabs').default; -const iframe = document.getElementById('BrewRenderer'); -const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; -const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); -const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; - const res = []; const pageMap = []; -const walkPages = ()=>{ +const walkPages = (iframeDocument)=>{ let current = 0; let skip = 0; let reset = 0; @@ -54,8 +49,12 @@ const recursiveAdd = (title, page, actualPage, targetDepth, child, curDepth=0)=> const getTOC = ()=>{ + const iframe = document.getElementById('BrewRenderer'); + const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; + const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); + const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; - walkPages(); + walkPages(iframeDocument); _.each(headings, (heading)=>{ const onPage = parseInt(heading.closest('.page').id?.replace(/^p/, '')); From 0f8461ced6e313657232a46a6f823c9a533d6a5a Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sat, 7 Sep 2024 20:16:16 -0500 Subject: [PATCH 05/11] Not a collection. --- themes/V3/5ePHB/snippets/tableOfContents.gen.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 2c8c3cfa4..2c1c931f8 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -12,8 +12,8 @@ const walkPages = (iframeDocument)=>{ const pages = iframeDocument.querySelectorAll('.page'); _.each(pages, (page)=>{ current++; - const doSkip = (page.querySelector('.skipCounting').length > 0); - const doReset = (page.querySelector('.resetCounting').length > 0); + const doSkip = (page.querySelector('.skipCounting') > 0); + const doReset = (page.querySelector('.resetCounting') > 0); if(doReset) { reset = current - 1; skip = 0; From b58688bd62c88e68011fc0d56ca9746dcec5b3fb Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sat, 7 Sep 2024 20:19:56 -0500 Subject: [PATCH 06/11] Stop comparing lengths, dude. --- themes/V3/5ePHB/snippets/tableOfContents.gen.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 2c1c931f8..2d88c04db 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -12,8 +12,8 @@ const walkPages = (iframeDocument)=>{ const pages = iframeDocument.querySelectorAll('.page'); _.each(pages, (page)=>{ current++; - const doSkip = (page.querySelector('.skipCounting') > 0); - const doReset = (page.querySelector('.resetCounting') > 0); + const doSkip = (page.querySelector('.skipCounting')); + const doReset = (page.querySelector('.resetCounting')); if(doReset) { reset = current - 1; skip = 0; From 6ea724bb1642fca73d6352d33508ccfa665d14fe Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sat, 7 Sep 2024 20:30:16 -0500 Subject: [PATCH 07/11] Start skipping .skipCount in ToC --- themes/V3/5ePHB/snippets/tableOfContents.gen.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 2d88c04db..c9faff553 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -11,6 +11,7 @@ const walkPages = (iframeDocument)=>{ let reset = 0; const pages = iframeDocument.querySelectorAll('.page'); _.each(pages, (page)=>{ + let showPage = true; current++; const doSkip = (page.querySelector('.skipCounting')); const doReset = (page.querySelector('.resetCounting')); @@ -19,8 +20,12 @@ const walkPages = (iframeDocument)=>{ skip = 0; } else if(doSkip){ skip += 1; + showPage = false; } - pageMap[current] = current - reset - skip; + pageMap[current] = { + pageNumber : current - reset - skip, + showPage : showPage + }; }); }; @@ -72,7 +77,7 @@ const ToCIterate = (entries, curDepth=0)=>{ const levelPad = ['- ###', ' - ####', ' - ', ' - ', ' - ', ' - ']; const toc = []; if(entries.title !== null){ - toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page}}}](#${entries.anchor})`); + if(entries.page.showPage) toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page.pageNumber}}}](#${entries.anchor})`); } if(entries.children.length) { _.each(entries.children, (entry, idx)=>{ From b0dffc6df15abd105ff320ed3ec84805bd491caa Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sat, 7 Sep 2024 20:35:43 -0500 Subject: [PATCH 08/11] Drop empty entries --- themes/V3/5ePHB/snippets/tableOfContents.gen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index c9faff553..614bb1cfb 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -87,7 +87,7 @@ const ToCIterate = (entries, curDepth=0)=>{ } }); } - return toc; + return toc.length > 0 ? toc : null; }; module.exports = function(props){ From 758c2799a1cf12aec4c38f32e3e7796d4d73c5d5 Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sat, 7 Sep 2024 20:40:29 -0500 Subject: [PATCH 09/11] That was the wrong way. Lets try this ugly fix. --- themes/V3/5ePHB/snippets/tableOfContents.gen.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 614bb1cfb..92cf6f8e3 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -87,7 +87,7 @@ const ToCIterate = (entries, curDepth=0)=>{ } }); } - return toc.length > 0 ? toc : null; + return toc; }; module.exports = function(props){ @@ -95,7 +95,7 @@ module.exports = function(props){ const markdown = _.reduce(TOC, (r, g1, idx1)=>{ r.push(ToCIterate(g1).join('\n')); return r; - }, []).join('\n'); + }, []).join('\n').replace('\n\n', '\n'); return dedent` {{toc,wide From 7881d4b4a2e22e591561fa4ca9080f5b024d93cb Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sun, 8 Sep 2024 16:22:58 -0400 Subject: [PATCH 10/11] Small logic cleanup and renaming --- .../V3/5ePHB/snippets/tableOfContents.gen.js | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 92cf6f8e3..97143cf5c 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -5,26 +5,23 @@ const res = []; const pageMap = []; -const walkPages = (iframeDocument)=>{ - let current = 0; - let skip = 0; - let reset = 0; +const mapPages = (iframeDocument)=>{ + let actualPage = 0; + let mappedPage = 0; const pages = iframeDocument.querySelectorAll('.page'); _.each(pages, (page)=>{ - let showPage = true; - current++; - const doSkip = (page.querySelector('.skipCounting')); - const doReset = (page.querySelector('.resetCounting')); - if(doReset) { - reset = current - 1; - skip = 0; - } else if(doSkip){ - skip += 1; - showPage = false; - } - pageMap[current] = { - pageNumber : current - reset - skip, - showPage : showPage + actualPage++; + const doSkip = page.querySelector('.skipCounting'); + const doReset = page.querySelector('.resetCounting'); + + if(doReset) + mappedPage = 1; + if(!doSkip && !doReset) + mappedPage++; + + pageMap[actualPage] = { + mappedPage : mappedPage, + showPage : !doSkip }; }); }; @@ -59,7 +56,7 @@ const getTOC = ()=>{ const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; - walkPages(iframeDocument); + mapPages(iframeDocument); _.each(headings, (heading)=>{ const onPage = parseInt(heading.closest('.page').id?.replace(/^p/, '')); @@ -77,7 +74,7 @@ const ToCIterate = (entries, curDepth=0)=>{ const levelPad = ['- ###', ' - ####', ' - ', ' - ', ' - ', ' - ']; const toc = []; if(entries.title !== null){ - if(entries.page.showPage) toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page.pageNumber}}}](#${entries.anchor})`); + if(entries.page.showPage) toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page.mappedPage}}}](#${entries.anchor})`); } if(entries.children.length) { _.each(entries.children, (entry, idx)=>{ From 4bc957159dbf3db2481b6cd23ca32f81c8b1fadb Mon Sep 17 00:00:00 2001 From: David Bolack Date: Sun, 8 Sep 2024 15:23:03 -0500 Subject: [PATCH 11/11] Move a couple of variables back out of the global space because that was bad. --- themes/V3/5ePHB/snippets/tableOfContents.gen.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 92cf6f8e3..e10cb50d7 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -1,11 +1,7 @@ const _ = require('lodash'); const dedent = require('dedent-tabs').default; -const res = []; - -const pageMap = []; - -const walkPages = (iframeDocument)=>{ +const walkPages = (iframeDocument, pageMap)=>{ let current = 0; let skip = 0; let reset = 0; @@ -54,12 +50,15 @@ const recursiveAdd = (title, page, actualPage, targetDepth, child, curDepth=0)=> const getTOC = ()=>{ + const pageMap = []; + const res = []; + const iframe = document.getElementById('BrewRenderer'); const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; - walkPages(iframeDocument); + walkPages(iframeDocument, pageMap); _.each(headings, (heading)=>{ const onPage = parseInt(heading.closest('.page').id?.replace(/^p/, ''));