app.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // 读取 OEBPS/Text/chapter396.html 文件
  33. // fs.readFile('./OEBPS/Text/chapter396.html', 'utf8', (err, data) => {
  34. // if (err) {
  35. // console.error("Error reading the file:", err);
  36. // return;
  37. // }
  38. // console.log(4545, data);
  39. // // 使用 xml2js 解析 XML 数据
  40. // /* const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true });
  41. // parser.parseString(data, (err, result) => {
  42. // if (err) {
  43. // console.error("Error parsing the XML:", err);
  44. // return;
  45. // }
  46. // // 转换结果为 JSON 格式
  47. // const jsonResult = JSON.stringify(result, null, 2);
  48. // // 输出 JSON 数据
  49. // console.log(jsonResult);
  50. // // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile()
  51. // fs.writeFile('output.json', jsonResult, (err) => {
  52. // if (err) {
  53. // console.error("Error writing JSON file:", err);
  54. // } else {
  55. // console.log("JSON data saved to output.json");
  56. // }
  57. // });
  58. // }); */
  59. // });
  60. // (async () => {
  61. // const epub = await EPub.createAsync('/Users/honghaitao/code/chm_py/test.epub', null,'');
  62. // let imgs = epub.listImage();
  63. // console.log(imgs);
  64. // })()