/**
 * Minified by jsDelivr using Terser v5.37.0.
 * Original file: /npm/html2epub@0.1.1/lib/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
var fs=require("fs"),url=require("url"),http=require("http"),https=require("https"),path=require("path"),mime=require("mime"),cheerio=require("cheerio"),archiver=require("archiver"),httpFilter=/^https?:\/\//,htmlFilter=/\.x?html?$/;function getUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)}))}function findFilesSync(e,t){var n=[];return function i(r,o){o=path.resolve(r,o),(fs.readdirSync(o)||[]).forEach((function(r){fs.lstatSync(path.resolve(o,r)).isDirectory()?i(o,r):t&&!t.test(r)||n.push(path.relative(e,path.resolve(o,r)))}))}(e,"."),n}function getNonExistingFileSync(e){if(fs.existsSync(e)){for(var t=1;fs.existsSync(e+"."+t);)t++;return e+"."+t}return e}var html2epub=module.exports=function e(t){if(!(this instanceof e))throw new Error("html2epub must be constructed by `new`");this.identifier=t.identifier||getUUID(),this.title=t.title||"Untitled",this.charset=t.charset||"UTF-8",this.language=t.language||"en",this.dc=t.dc||{},this.modified=t.modified||(new Date).toISOString().replace(/\.[0-9]{3}Z$/,"Z"),this.format=t.format||"epub",this.depth=t.depth||3,this.headings=t.headings||"h1,h2,h3,h4,h5,h6",this.keepAllHeadings=t.keepAllHeadings,this.basedir=t.basedir||process.cwd(),this.spine=t.spine||findFilesSync(this.basedir,htmlFilter),this.outputFile=getNonExistingFileSync(t.outputFile||path.basename(this.basedir)+".epub")};function getHeadingID(e){for(var t="",n="";e.length&&!n.length&&!(t=e.attr("id"));){for(var i=e.prev();i.length;)n+=i.text(),i=i.prev();e=e.parent()}return t}function indent(e){for(var t="\n    ",n=0;n<e;n++)t+="  ";return t}function buildToC_txt(e,t){var n="";return e.forEach((function(e){e.headings.forEach((function(e){e.level<t&&(n+=indent(e.level)+e.title)}))})),n+"\n"}function buildToC_json(e,t,n){var i={children:[]},r=i,o=0;return e.forEach((function(e){e.headings.forEach((function(e){if(e.level<t){var a={title:e.title,href:e.href};if(e.level<o){r=i;for(var l=0;l<e.level;l++)r=r.children[r.children.length-1]}else if(e.level==o+1)(r=r.children[r.children.length-1]).children=[];else if(e.level>o+1)if(console.error("non-continous heading (h"+(e.level+1)+"): "+e.title),n)a=null;else for(var s=0;s<e.level-o;s++)r.children.length||r.children.push({}),(r=r.children[r.children.length-1]).children=[];a&&(o=e.level,r.children.push(a))}}))})),i.children}function buildToC_ncx(e,t){var n=cheerio.load("<navMap></navMap>",{xmlMode:!0,decodeEntities:!1}),i=[n("navMap")],r=0,o=1;return e.forEach((function(e){e.headings.forEach((function(e){if(e.level<t){var n=indent(e.level)+'<navPoint id="nav_'+o+'" playOrder="'+o+'"><navLabel><text>'+e.title+'</text></navLabel><content src="'+e.href+'" /></navPoint>';e.level<=r?i[e.level].append(n):(i[e.level]=i[r].find("navPoint").last(),i[r].find("navPoint").last().append(n)),r=e.level,o++}}))})),n.html()}function buildToC_xhtml(e,t){var n=cheerio.load('  <nav epub:type="toc"><ol></ol>\n  </nav>',{xmlMode:!0,decodeEntities:!1}),i=[n("ol")],r=0;return e.forEach((function(e){e.headings.forEach((function(e){if(e.level<t){var n=e.href?'<a href="'+e.href+'">'+e.title+"</a>":"<span>"+e.title+"</span>",o=indent(e.level)+"<li>"+n+"</li>";e.level<=r?i[e.level].append(o):(i[r].find("li").last().append("<ol>"+o+"</ol>"),i[e.level]=i[r].find("ol").last()),r=e.level}}))})),n.html()}function zeroPadding(e,t,n){for(var i=(++t).toString();i.length<n;)i="0"+i;return e+i}function buildOPF_manifest(e,t){var n=[],i=0,r=e.length.toString().length;e.forEach((function(e,o){var a="",l=mime.lookup(e);t.indexOf(e)>=0?a=zeroPadding("page_",t.indexOf(e),r):"application/x-dtbncx+xml"==l?(a="ncx",i++):"application/oebps-package+xml"!=l&&(a=zeroPadding("res_",o,r)),a&&n.push('<item id="'+a+'" media-type="'+l+'" href="'+e+'"'+("toc.xhtml"==e?' properties="nav"':"")+" />")})),i>1&&console.error("several NCX files have been found.");var o="\n  <manifest>\n    "+n.sort().join("\n    ")+"\n  </manifest>",a="\n  <spine"+(1==i?' toc="ncx"':"")+">";return t.forEach((function(e,t){var n=zeroPadding("page_",t,r);a+='\n    <itemref idref="'+n+'" />'})),o+(a+="\n  </spine>")}function epubArchive(e,t,n){var i=fs.createWriteStream(e),r=archiver("zip");i.on("close",(function(){console.log(e+" - "+r.pointer()+" bytes"),"function"==typeof n&&n()})),r.on("error",(function(e){throw e})),r.pipe(i),r.append("application/epub+zip",{name:"mimetype",store:!0});var o='<?xml version="1.0"?>\n<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">\n   <rootfiles>\n     <rootfile full-path="'+t+'" media-type="application/oebps-package+xml"/>\n   </rootfiles>\n</container>';return r.append(o,{name:"META-INF/container.xml"}),r}html2epub.prototype.getHeadings=function(e,t){var n=[],i=this.keepAllHeadings,r=this.headings.charAt(1);return e(this.headings).each((function(o,a){var l=e(a),s={level:parseInt(a.tagName.substr(1),10)-r,title:l.text().replace(/^\s+|\s+$/g,"").replace(/\s+/g," ")},h=getHeadingID(l);h?s.href=t+"#"+h:o||(s.href=t),(s.href||i)&&n.push(s)})),n},html2epub.prototype.parseHeadingsSync=function(){var e=[],t=this;return this.spine.forEach((function(n,i){var r=fs.readFileSync(path.resolve(t.basedir,n)),o=cheerio.load(r,{decodeEntities:!1});e.push({href:n,headings:t.getHeadings(o,n)})})),e},html2epub.prototype.showToC=function(e,t){var n="";switch(e=e||this.parseHeadingsSync(),t=t||this.format){case"txt":n=buildToC_txt(e,this.depth);break;case"json":var i=buildToC_json(e,this.depth,this.strict);n=JSON.stringify(i,null,2);break;case"ncx":n='<?xml version="1.0" encoding="'+this.charset+'"?>\n<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">\n  <head>\n    <meta name="dtb:uid" content="'+this.identifier+'" />\n    <meta name="dtb:depth" content="'+this.depth+'" />\n  </head>\n  <docTitle>\n    <text>'+this.title+"</text>\n  </docTitle>\n  "+buildToC_ncx(e,this.depth)+"\n</ncx>";break;case"xhtml":n='<?xml version="1.0" encoding="'+this.charset+'"?>\n<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">\n<head>\n  <meta charset="'+this.charset+'" />\n  <title>'+this.title+'</title>\n  <style type="text/css"> nav ol { list-style-type: none; } </style>\n</head>\n<body>\n'+buildToC_xhtml(e,this.depth)+"\n</body>\n</html>";break;default:console.error('unsupported output format: "'+t+'"')}return n},html2epub.prototype.showOPF=function(e){var t="";for(var n in this.dc)t+="\n    <dc:"+n+">"+this.dc[n]+"</dc:"+n+">";return'<?xml version="1.0" encoding="'+this.charset+'"?>\n<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="uuid">\n  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">\n    <dc:identifier id="uuid">'+this.identifier+"</dc:identifier>\n    <dc:title>"+this.title+"</dc:title>\n    <dc:language>"+this.language+"</dc:language>"+t+'\n    <meta property="dcterms:modified">'+this.modified+"</meta>\n  </metadata>"+buildOPF_manifest(e,this.spine)+"\n</package>"},html2epub.prototype.convertSync=function(){var e=this.basedir;function t(t){return fs.existsSync(path.resolve(e,t))}var n=epubArchive(this.outputFile,"EPUB/content.opf"),i=findFilesSync(e),r=this.parseHeadingsSync();t("toc.xhtml")||(i.push("toc.xhtml"),n.append(this.showToC(r,"xhtml"),{name:"EPUB/toc.xhtml"})),t("toc.ncx")||(i.push("toc.ncx"),n.append(this.showToC(r,"ncx"),{name:"EPUB/toc.ncx"})),t("content.opf")||n.append(this.showOPF(i),{name:"EPUB/content.opf"}),n.bulk([{expand:!0,cwd:e,src:["**"],dest:"EPUB"}]),n.finalize()};var urlTrim=/^url\(['"]?|['"]?\)$/g,urlDetect=/url\(['"]?[^'"\)]*['"]?\)/g,urlPattern=/url\((['"]?)([^'"\)]*)(['"]?)\)/g;function download(e,t,n,i){n="function"==typeof n?n:function(){},i="function"==typeof i?i:function(){};var r=url.parse(e).protocol,o=function(){console.error(r+" is not supported")};switch(r){case"http:":o=http.get;break;case"https:":o=https.get}o(e,(function(i){var r="";t&&(i.setEncoding(t),"base64"==t&&(r="data:"+mime.lookup(e)+";base64,")),i.on("data",(function(e){r+=e})),i.on("end",(function(){n(r)}))})).on("error",(function(e){i(e)}))}html2epub.prototype.convert=function(e){var t=this,n=epubArchive(this.outputFile,"EPUB/content.opf",e),i=new Array(this.spine.length),r=this.spine.length,o=[],a=o.length;function l(e,l){n.append(e,{name:"EPUB/"+l.replace(httpFilter,"")}),r||a||(o.forEach((function(e,t,n){n[t]=e.replace(httpFilter,"")})),t.spine.forEach((function(e,t,n){n[t]=e.replace(httpFilter,""),o.push(e.replace(httpFilter,""))})),o.push("toc.xhtml"),o.push("toc.ncx"),n.append(t.showOPF(o),{name:"EPUB/content.opf"}),n.append(t.showToC(i,"xhtml"),{name:"EPUB/toc.xhtml"}),n.append(t.showToC(i,"ncx"),{name:"EPUB/toc.ncx"}),n.finalize())}this.spine.forEach((function(e,n){download(e,"",(function(s){console.log("  downloading: "+e);var h=cheerio.load(s),c=e.replace(/[^\/]*$/,"");function p(e){e=url.resolve(c,e),o.indexOf(e)<0&&(o.push(e),a++,download(e,"binary",(function(t){l(t,e,--a)})),console.log("  downloading: "+e))}h("base").length?c=h("base").last().attr("href"):h("head").prepend('\n  <base href="'+c+'" />'),h("style").each((function(e,t){(h(t).text().match(urlDetect)||[]).forEach((function(e){p(e.replace(urlTrim,""))}))})),h("img, audio, video").each((function(e,t){p(h(t).attr("src"))})),h("link[rel=stylesheet]").each((function(e,t){p(h(t).attr("href"))}));var d=e.replace(httpFilter,"");i[n]={href:d,headings:t.getHeadings(h,d)},l(s,e,--r)}),(function(t){console.error("Could not get "+e+" - "+t.message)}))}))};
//# sourceMappingURL=/sm/d6c4349d3f4c75c83a0d7472e641a1bced0edc1108d641a3d1512d43cbac0649.map