|
@@ -1,62 +1,24 @@
|
|
|
-const fs = require('fs');
|
|
|
-const xml2js = require('xml2js');
|
|
|
-const {EPub} = require('epub2');
|
|
|
+const fs = require("fs");
|
|
|
+const xml2js = require("xml2js");
|
|
|
+const { EPub } = require("epub2");
|
|
|
|
|
|
// 读取 toc.ncx 文件
|
|
|
-fs.readFile('./test2_副本/OEBPS/toc.ncx', 'utf8', (err, data) => {
|
|
|
- if (err) {
|
|
|
- console.error("Error reading the file:", err);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 使用 xml2js 解析 XML 数据
|
|
|
- const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true });
|
|
|
- parser.parseString(data, (err, result) => {
|
|
|
- if (err) {
|
|
|
- console.error("Error parsing the XML:", err);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- console.log(20, result);
|
|
|
-
|
|
|
-
|
|
|
- // 转换结果为 JSON 格式
|
|
|
- const jsonResult = JSON.stringify(result, null, 2);
|
|
|
-
|
|
|
- // 输出 JSON 数据
|
|
|
- console.log(jsonResult);
|
|
|
-
|
|
|
- // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile()
|
|
|
- fs.writeFile('output.json', jsonResult, (err) => {
|
|
|
- if (err) {
|
|
|
- console.error("Error writing JSON file:", err);
|
|
|
- } else {
|
|
|
- console.log("JSON data saved to output.json");
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
-});
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-// 读取 OEBPS/Text/chapter396.html 文件
|
|
|
-// fs.readFile('./OEBPS/Text/chapter396.html', 'utf8', (err, data) => {
|
|
|
+// fs.readFile('./test2_副本/OEBPS/toc.ncx', 'utf8', (err, data) => {
|
|
|
// if (err) {
|
|
|
// console.error("Error reading the file:", err);
|
|
|
// return;
|
|
|
// }
|
|
|
|
|
|
-// console.log(4545, data);
|
|
|
-
|
|
|
-
|
|
|
// // 使用 xml2js 解析 XML 数据
|
|
|
-// /* const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true });
|
|
|
+// const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true });
|
|
|
// parser.parseString(data, (err, result) => {
|
|
|
// if (err) {
|
|
|
// console.error("Error parsing the XML:", err);
|
|
|
// return;
|
|
|
// }
|
|
|
|
|
|
+// console.log(20, result);
|
|
|
+
|
|
|
// // 转换结果为 JSON 格式
|
|
|
// const jsonResult = JSON.stringify(result, null, 2);
|
|
|
|
|
@@ -71,13 +33,58 @@ fs.readFile('./test2_副本/OEBPS/toc.ncx', 'utf8', (err, data) => {
|
|
|
// console.log("JSON data saved to output.json");
|
|
|
// }
|
|
|
// });
|
|
|
-// }); */
|
|
|
+// });
|
|
|
// });
|
|
|
|
|
|
+// 先处理
|
|
|
+// 读取 OEBPS/Text/chapter396.html 文件
|
|
|
+fs.readFile(
|
|
|
+ "./book/test2/OEBPS/Text/:::::::::::::::::::::*:*:::***::.html",
|
|
|
+ // "./book/test/OEBPS/Text/chapter396.html",
|
|
|
+ "utf8",
|
|
|
+ (err, data) => {
|
|
|
+ if (err) {
|
|
|
+ console.error("Error reading the file:", err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(4545, data);
|
|
|
+
|
|
|
+ // 使用 xml2js 解析 XML 数据
|
|
|
+ /* const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true });
|
|
|
+ parser.parseString(data, (err, result) => {
|
|
|
+ if (err) {
|
|
|
+ console.error("Error parsing the XML:", err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
+ // 转换结果为 JSON 格式
|
|
|
+ const jsonResult = JSON.stringify(result, null, 2);
|
|
|
+
|
|
|
+ // 输出 JSON 数据
|
|
|
+ console.log(jsonResult);
|
|
|
+
|
|
|
+ // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile()
|
|
|
+ fs.writeFile('output.json', jsonResult, (err) => {
|
|
|
+ if (err) {
|
|
|
+ console.error("Error writing JSON file:", err);
|
|
|
+ } else {
|
|
|
+ console.log("JSON data saved to output.json");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }); */
|
|
|
+ }
|
|
|
+);
|
|
|
|
|
|
// (async () => {
|
|
|
-// const epub = await EPub.createAsync('/Users/honghaitao/code/chm_py/test.epub', null,'');
|
|
|
-// let imgs = epub.listImage();
|
|
|
-// console.log(imgs);
|
|
|
-// })()
|
|
|
+// // https://github.com/bluelovers/ws-epub/blob/master/packages/epub2/test/example/example2.ts
|
|
|
+// const epub = await EPub.createAsync("./book/test2.epub", null, "");
|
|
|
+// // let imgs = epub.listImage();
|
|
|
+// // console.log(imgs);
|
|
|
+// console.log(78, epub.spine.contents);
|
|
|
+
|
|
|
+// await epub.getChapterAsync(epub.spine.contents[2].id).then(function (data) {
|
|
|
+// console.log("\nFIRST CHAPTER:\n");
|
|
|
+// console.log(data); // first 512 bytes
|
|
|
+// });
|
|
|
+// })();
|