Browse Source

多种文件格式解析

john 8 months ago
parent
commit
f6de559106
6 changed files with 63 additions and 52 deletions
  1. BIN
      .DS_Store
  2. 2 1
      .gitignore
  3. 2 1
      chm_node/.gitignore
  4. 57 50
      chm_node/app.js
  5. 1 0
      epub_node/.gitignore
  6. 1 0
      expores_node/.gitignore

BIN
.DS_Store


+ 2 - 1
.gitignore

@@ -4,4 +4,5 @@ base_files/*
 *.epub
 *.zip
 book/*
-.idea
+.idea
+.DS_Store

+ 2 - 1
chm_node/.gitignore

@@ -4,4 +4,5 @@ base_files/*
 *.epub
 *.zip
 book/*
-.idea
+.idea
+.DS_Store

+ 57 - 50
chm_node/app.js

@@ -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
+//   });
+// })();

+ 1 - 0
epub_node/.gitignore

@@ -3,3 +3,4 @@ base_files/*
 *.tar
 pm2Log/*
 public/*
+.DS_Store

+ 1 - 0
expores_node/.gitignore

@@ -3,3 +3,4 @@ base_files/*
 *.tar
 pm2Log/*
 public/*
+.DS_Store