-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves #207 create a browser-compatible version of the Document con…
…verter - Reduce the scope of the classes - Use browserify to produce a browser-compatible file - Inline files content (ie. replace readFileSync) to bundle everything into a single JavaScript file - Add tests
- Loading branch information
1 parent
67a9b11
commit 9b340cd
Showing
12 changed files
with
1,685 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ node_modules/ | |
/test/output/*.pdf | ||
/test/fixtures/*.html | ||
/test/output/visual-comparison-workdir/ | ||
/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,63 @@ | ||
/* global Paged, PagedPolyfill */ | ||
window.AsciidoctorPDF = window.AsciidoctorPDF || {} | ||
window.AsciidoctorPDF.status = 'rendering' | ||
class PagedReadyHandler extends Paged.Handler { | ||
afterRendered (pages) { | ||
window.AsciidoctorPDF.status = 'ready' | ||
} | ||
;(function() { | ||
window.AsciidoctorPDF = window.AsciidoctorPDF || {} | ||
window.AsciidoctorPDF.status = 'rendering'; | ||
|
||
// BEGIN: workaround to prevent duplicated content at end/beginning of page | ||
// https://gitlab.pagedmedia.org/tools/pagedjs/issues/126 | ||
constructor (chunker, polisher, caller) { | ||
super(chunker, polisher, caller) | ||
this.carriageReturn = String.fromCharCode(10) | ||
} | ||
class PagedReadyHandler extends Paged.Handler { | ||
afterRendered (_) { | ||
window.AsciidoctorPDF.status = 'ready' | ||
} | ||
|
||
checkNode (node) { | ||
if (!node) return | ||
if (node.nodeType !== 3) return | ||
if (node.textContent === this.carriageReturn) { | ||
node.remove() | ||
// BEGIN: workaround to prevent duplicated content at end/beginning of page | ||
// https://gitlab.pagedmedia.org/tools/pagedjs/issues/126 | ||
constructor (chunker, polisher, caller) { | ||
super(chunker, polisher, caller) | ||
this.carriageReturn = String.fromCharCode(10) | ||
} | ||
} | ||
|
||
afterParsed (parsed) { | ||
const template = document.querySelector('template').content | ||
const breakAfterAvoidElements = template.querySelectorAll('[data-break-after="avoid"], [data-break-before="avoid"]') | ||
for (const el of breakAfterAvoidElements) { | ||
this.checkNode(el.previousSibling) | ||
this.checkNode(el.nextSibling) | ||
checkNode (node) { | ||
if (!node) return | ||
if (node.nodeType !== 3) return | ||
if (node.textContent === this.carriageReturn) { | ||
node.remove() | ||
} | ||
} | ||
} | ||
// END: workaround to prevent duplicated content at end/beginning of page | ||
} | ||
|
||
Paged.registerHandlers(PagedReadyHandler) | ||
afterParsed (_) { | ||
const template = document.querySelector('template').content | ||
const breakAfterAvoidElements = template.querySelectorAll('[data-break-after="avoid"], [data-break-before="avoid"]') | ||
for (const el of breakAfterAvoidElements) { | ||
this.checkNode(el.previousSibling) | ||
this.checkNode(el.nextSibling) | ||
} | ||
} | ||
// END: workaround to prevent duplicated content at end/beginning of page | ||
} | ||
|
||
window.PagedConfig = window.PagedConfig || {} | ||
Paged.registerHandlers(PagedReadyHandler) | ||
|
||
window.PagedConfig.auto = false | ||
window.PagedConfig = window.PagedConfig || {} | ||
window.PagedConfig.auto = false | ||
|
||
// same logic as pagedjs, but waiting for 'complete' instead of 'interactive' | ||
const ready = new Promise(function (resolve, reject) { | ||
if (document.readyState === 'complete') { | ||
resolve(document.readyState) | ||
return | ||
} | ||
|
||
document.onreadystatechange = function () { | ||
const ready = new Promise(function (resolve, reject) { | ||
if (document.readyState === 'complete') { | ||
resolve(document.readyState) | ||
return | ||
} | ||
} | ||
}) | ||
|
||
ready.then(async function () { | ||
const done = await PagedPolyfill.preview(window.PagedConfig.content, window.PagedConfig.stylesheets, window.PagedConfig.renderTo) | ||
if (window.PagedConfig.after) { | ||
await window.PagedConfig.after(done) | ||
} | ||
}) | ||
document.onreadystatechange = function () { | ||
if (document.readyState === 'complete') { | ||
resolve(document.readyState) | ||
} | ||
} | ||
}) | ||
|
||
ready.then(async function () { | ||
const done = await PagedPolyfill.preview(window.PagedConfig.content, window.PagedConfig.stylesheets, window.PagedConfig.renderTo) | ||
if (window.PagedConfig.after) { | ||
await window.PagedConfig.after(done) | ||
} | ||
}) | ||
})() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,50 @@ | ||
/* global Paged */ | ||
class RepeatingTableElements extends Paged.Handler { | ||
constructor (chunker, polisher, caller) { // eslint-disable-line no-useless-constructor | ||
super(chunker, polisher, caller) | ||
} | ||
;(function () { | ||
class RepeatingTableElements extends Paged.Handler { | ||
constructor (chunker, polisher, caller) { // eslint-disable-line no-useless-constructor | ||
super(chunker, polisher, caller) | ||
} | ||
|
||
afterPageLayout (pageElement, page, breakToken, chunker) { | ||
// Find all split table elements | ||
const tables = pageElement.querySelectorAll('table[data-split-from]') | ||
afterPageLayout (pageElement, page, breakToken, chunker) { | ||
// Find all split table elements | ||
const tables = pageElement.querySelectorAll('table[data-split-from]') | ||
|
||
tables.forEach((table) => { | ||
// Get the reference UUID of the node | ||
const ref = table.dataset.ref | ||
// Find the node in the original source | ||
const sourceTable = chunker.source.querySelector("[data-ref='" + ref + "']") | ||
// Repeat the <thead> element (if exists) | ||
this.repeatElement(sourceTable, table, 'thead') | ||
// Repeat the <colgroup> elements (if at least one exists) | ||
this.repeatElements(sourceTable, table, 'colgroup') | ||
// Repeat the <caption> element (if exists) | ||
this.repeatElement(sourceTable, table, 'caption') | ||
}) | ||
} | ||
|
||
repeatElement (sourceTable, table, querySelector) { | ||
const element = sourceTable.querySelector(querySelector) | ||
if (element) { | ||
// Clone the element | ||
const clonedElement = element.cloneNode(true) | ||
// Insert the element at the start of the split table | ||
table.insertBefore(clonedElement, table.firstChild) | ||
tables.forEach((table) => { | ||
// Get the reference UUID of the node | ||
const ref = table.dataset.ref | ||
// Find the node in the original source | ||
const sourceTable = chunker.source.querySelector("[data-ref='" + ref + "']") | ||
// Repeat the <thead> element (if exists) | ||
this.repeatElement(sourceTable, table, 'thead') | ||
// Repeat the <colgroup> elements (if at least one exists) | ||
this.repeatElements(sourceTable, table, 'colgroup') | ||
// Repeat the <caption> element (if exists) | ||
this.repeatElement(sourceTable, table, 'caption') | ||
}) | ||
} | ||
} | ||
|
||
repeatElements(sourceTable, table, querySelector) { | ||
const elements = sourceTable.querySelectorAll(querySelector) | ||
if (elements) { | ||
elements.forEach((element) => { | ||
repeatElement (sourceTable, table, querySelector) { | ||
const element = sourceTable.querySelector(querySelector) | ||
if (element) { | ||
// Clone the element | ||
const clonedElement = element.cloneNode(true) | ||
// Insert the element at the start of the split table | ||
table.insertBefore(clonedElement, table.firstChild) | ||
}) | ||
} | ||
} | ||
|
||
repeatElements (sourceTable, table, querySelector) { | ||
const elements = sourceTable.querySelectorAll(querySelector) | ||
if (elements) { | ||
elements.forEach((element) => { | ||
// Clone the element | ||
const clonedElement = element.cloneNode(true) | ||
// Insert the element at the start of the split table | ||
table.insertBefore(clonedElement, table.firstChild) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
Paged.registerHandlers(RepeatingTableElements) | ||
Paged.registerHandlers(RepeatingTableElements) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.