app.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const fs = require("fs");
  2. const xml2js = require("xml2js");
  3. const { EPub } = require("epub2");
  4. // 读取 toc.ncx 文件
  5. // fs.readFile('./test2_副本/OEBPS/toc.ncx', 'utf8', (err, data) => {
  6. // if (err) {
  7. // console.error("Error reading the file:", err);
  8. // return;
  9. // }
  10. // // 使用 xml2js 解析 XML 数据
  11. // const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true });
  12. // parser.parseString(data, (err, result) => {
  13. // if (err) {
  14. // console.error("Error parsing the XML:", err);
  15. // return;
  16. // }
  17. // console.log(20, result);
  18. // // 转换结果为 JSON 格式
  19. // const jsonResult = JSON.stringify(result, null, 2);
  20. // // 输出 JSON 数据
  21. // console.log(jsonResult);
  22. // // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile()
  23. // fs.writeFile('output.json', jsonResult, (err) => {
  24. // if (err) {
  25. // console.error("Error writing JSON file:", err);
  26. // } else {
  27. // console.log("JSON data saved to output.json");
  28. // }
  29. // });
  30. // });
  31. // });
  32. // 先处理
  33. // 读取 OEBPS/Text/chapter396.html 文件
  34. fs.readFile(
  35. "./book/test2/OEBPS/Text/:::::::::::::::::::::*:*:::***::.html",
  36. // "./book/test/OEBPS/Text/chapter396.html",
  37. "utf8",
  38. (err, data) => {
  39. if (err) {
  40. console.error("Error reading the file:", err);
  41. return;
  42. }
  43. console.log(4545, data);
  44. // 使用 xml2js 解析 XML 数据
  45. /* const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true });
  46. parser.parseString(data, (err, result) => {
  47. if (err) {
  48. console.error("Error parsing the XML:", err);
  49. return;
  50. }
  51. // 转换结果为 JSON 格式
  52. const jsonResult = JSON.stringify(result, null, 2);
  53. // 输出 JSON 数据
  54. console.log(jsonResult);
  55. // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile()
  56. fs.writeFile('output.json', jsonResult, (err) => {
  57. if (err) {
  58. console.error("Error writing JSON file:", err);
  59. } else {
  60. console.log("JSON data saved to output.json");
  61. }
  62. });
  63. }); */
  64. }
  65. );
  66. // (async () => {
  67. // // https://github.com/bluelovers/ws-epub/blob/master/packages/epub2/test/example/example2.ts
  68. // const epub = await EPub.createAsync("./book/test2.epub", null, "");
  69. // // let imgs = epub.listImage();
  70. // // console.log(imgs);
  71. // console.log(78, epub.spine.contents);
  72. // await epub.getChapterAsync(epub.spine.contents[2].id).then(function (data) {
  73. // console.log("\nFIRST CHAPTER:\n");
  74. // console.log(data); // first 512 bytes
  75. // });
  76. // })();