base.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const start = 1;
  2. module.exports = {
  3. opf: function (name, list) {
  4. return new Promise(resolve => {
  5. function page1(list) {
  6. let html = '';
  7. list.forEach(key => {
  8. if (startAndEnd(key)) {
  9. html += `
  10. <item id="text${key.index}" media-type="text/x-oeb1-document" href="page/text${key.index}.html"></item>`;
  11. }
  12. });
  13. return html;
  14. }
  15. function page2(list) {
  16. let html = '';
  17. list.forEach(key => {
  18. if (startAndEnd(key)) {
  19. html += `
  20. <itemref idref="text${key.index}"/>`;
  21. }
  22. });
  23. return html;
  24. }
  25. let html = `<?xml version="1.0" encoding="iso-8859-1"?>
  26. <package unique-identifier="uid" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:asd="http://www.idpf.org/asdfaf">
  27. <metadata>
  28. <dc-metadata xmlns:dc="http://purl.org/metadata/dublin_core"
  29. xmlns:oebpackage="http://openebook.org/namespaces/oeb-package/1.0/">
  30. <dc:Title>${name}</dc:Title>
  31. <dc:Language>zh</dc:Language>
  32. <dc:Creator>Amazon.com</dc:Creator>
  33. <dc:Copyrights>Amazon.com</dc:Copyrights>
  34. <dc:Publisher>Amazon.com</dc:Publisher>
  35. <x-metadata>
  36. <EmbeddedCover>./danshijianzong.jpg</EmbeddedCover>
  37. </x-metadata>
  38. </dc-metadata>
  39. </metadata>
  40. <manifest>
  41. <item id="content" media-type="text/x-oeb1-document" href="toc.html"></item>
  42. <item id="ncx" media-type="application/x-dtbncx+xml" href="toc.ncx"/>
  43. ${page1(list)}
  44. </manifest>
  45. <spine toc="ncx">
  46. <itemref idref="content"/>
  47. ${page2(list)}
  48. </spine>
  49. <guide>
  50. <reference type="toc" title="Table of Contents" href="toc.html"/>
  51. <reference type="text" title="Book" href="text1.html"/>
  52. </guide>
  53. </package>`;
  54. resolve(html);
  55. });
  56. },
  57. toc: function (list) {
  58. return new Promise(resolve => {
  59. function page() {
  60. let html = '';
  61. list.forEach(key => {
  62. if (startAndEnd(key)) {
  63. html += `
  64. <li><a href="page/text${key.index}.html#id${key.index}">${key.title}</a></li>`;
  65. }
  66. });
  67. return html;
  68. }
  69. let html = `<!DOCTYPE html>
  70. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh">
  71. <head>
  72. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  73. <title>TOC</title>
  74. </head>
  75. <body>
  76. <h1 id="toc">目录</h1>
  77. <ul>
  78. ${page()}
  79. </ul>
  80. </body>
  81. </html>
  82. `;
  83. resolve(html);
  84. });
  85. },
  86. ncx: function (name, list) {
  87. return new Promise(resolve => {
  88. function page() {
  89. let html = '';
  90. list.forEach(key => {
  91. if (startAndEnd(key)) {
  92. html += `
  93. <navPoint id="navpoint-${key.index + 1}" playOeder="${key.index + 1}"><navLabel><text>${key.title}</text></navLabel><content src="page/text${key.index}.html#id${key.index}"></content></navPoint>`;
  94. }
  95. });
  96. return html;
  97. }
  98. let html = `<?xml version="1.0"?>
  99. <!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
  100. "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
  101. <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
  102. <head>
  103. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  104. </head>
  105. <docTitle>
  106. <text>${name}</text>
  107. </docTitle>
  108. <navMap>
  109. <navPoint id="navpoint-1" playOrder="1"><navLabel><text>Content</text></navLabel><content src="toc.html#toc"/></navPoint>
  110. ${page()}
  111. </navMap>
  112. </ncx>`;
  113. resolve(html);
  114. });
  115. },
  116. page: function (item, content) {
  117. return new Promise(resolve => {
  118. const html = `
  119. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh">
  120. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  121. <body>
  122. <h3 id="id${item.index}">${content.title}</h3>
  123. <p>
  124. ${content.page}
  125. </p>
  126. </body>
  127. </html>
  128. `;
  129. resolve(html);
  130. });
  131. },
  132. name: '择天记'
  133. };
  134. function startAndEnd(item) {
  135. return item.index > 0 && item.index < 605;
  136. }