diff --git a/projects/pdf-salad/src/Externs.hx b/projects/pdf-salad/src/Externs.hx index 5de4d6f1..34b1d529 100644 --- a/projects/pdf-salad/src/Externs.hx +++ b/projects/pdf-salad/src/Externs.hx @@ -9,7 +9,7 @@ extern class PDFDocument { public static function load(bytes:Buffer, ?options:LoadOptions):Promise; public function save():Promise; public function getPageCount():Int; - public function copyPages(srcDoc:PDFDocument, indices:Array):Array; + public function copyPages(srcDoc:PDFDocument, indices:Array):Promise>; public function addPage(page:PDFPage):Void; } diff --git a/projects/pdf-salad/src/Main.hx b/projects/pdf-salad/src/Main.hx index e2e22c41..6293b194 100644 --- a/projects/pdf-salad/src/Main.hx +++ b/projects/pdf-salad/src/Main.hx @@ -1,14 +1,9 @@ package; -import haxe.Constraints; -import js.lib.Uint8Array; -import js.node.Fs; -import js.lib.Promise; -import kiss.Kiss; -import kiss.Prelude; -import Externs; -using StringTools; -@:build(kiss.Kiss.build()) -class Main {} +class Main { + static function main() { + Main_.main(); + } +} diff --git a/projects/pdf-salad/src/Main.kiss b/projects/pdf-salad/src/Main.kiss deleted file mode 100644 index 34416bfb..00000000 --- a/projects/pdf-salad/src/Main.kiss +++ /dev/null @@ -1,27 +0,0 @@ -(function loadAll [:Array paths :Function callback &opt :Array pdfs] - (unless pdfs (set pdfs [])) - (localVar nextPdf (paths.shift)) - (if (nextPdf.endsWith ".pdf") - (awaitLet [pdf (PDFDocument.load (Fs.readFileSync (print nextPdf)))] - (pdfs.push pdf) - (if paths - (loadAll paths callback pdfs) - (callback pdfs))) - (when paths - (loadAll paths callback pdfs)))) - -// TODO add sequentialPerPDF argument (which, when used, .shift()s pages from the beginning of PDFs) -// TODO add chunkSize argument (default 1, which specifies how many pages in order to pull from a random PDF. value of -1 means take the whole PDF, and remove it from the list) -// TODO make output page limit optional -(let [[sourceDir numPages] (Sys.args)] - (loadAll (for file (Fs.readdirSync sourceDir) (+ sourceDir "/" file)) - (lambda [:Array inputPdfs] - (awaitLet [saladPdf (PDFDocument.create) - pages (Promise.all - (for _ (range 0 (Std.parseInt numPages)) - (let [:PDFDocument pdf (nth inputPdfs (Std.random inputPdfs.length)) - page (Std.random (pdf.getPageCount))] - (saladPdf.copyPages pdf [page]))))] - (doFor page pages (saladPdf.addPage (first page))) - (awaitLet [bytesOut (saladPdf.save)] - (Fs.writeFileSync "out.pdf" bytesOut)))))) \ No newline at end of file diff --git a/projects/pdf-salad/src/Main_.kiss b/projects/pdf-salad/src/Main_.kiss new file mode 100644 index 00000000..c435c952 --- /dev/null +++ b/projects/pdf-salad/src/Main_.kiss @@ -0,0 +1,28 @@ +(import haxe.Constraints) +(import js.lib.Uint8Array) +(import js.node.Fs) +(import js.lib.Promise) +(import Externs) + + +(function loadAll [:Array paths] + (Promise.all + (filter + (for path paths + (when (path.endsWith ".pdf") + (PDFDocument.load (Fs.readFileSync path))))))) + +// TODO add sequentialPerPDF argument (which, when used, .shift()s pages from the beginning of PDFs) +// TODO add chunkSize argument (default 1, which specifies how many pages in order to pull from a random PDF. value of -1 means take the whole PDF, and remove it from the list) +// TODO make output page limit optional +(let [[sourceDir numPages] (Sys.args)] + (awaitLet [inputPdfs (loadAll (for file (Fs.readdirSync sourceDir) (joinPath sourceDir file))) + saladPdf (PDFDocument.create) + pages (Promise.all + (for _ (range 0 (Std.parseInt numPages)) + (let [:PDFDocument pdf (nth inputPdfs (Std.random inputPdfs.length)) + page (Std.random (pdf.getPageCount))] + (saladPdf.copyPages pdf [page]))))] + (doFor page pages (saladPdf.addPage (first page))) + (awaitLet [bytesOut (saladPdf.save)] + (Fs.writeFileSync "out.pdf" bytesOut)))) \ No newline at end of file