- 使用line-reader模塊
包含使用iconv-lite模塊轉(zhuǎn)義字符串
const fs = require('fs');
const lineReader = require('line-reader');
const path = require('path');
const iconv = require("iconv-lite");
const co = require('co');
/**
* @method 讀取小說(shuō)并解析
* @param paths 文件地址
* @returns {Function}
*/
readerTXT(paths) {
return function (cb) {
co(function* () {
let count = 0; //章節(jié)數(shù)
let contents = [];//記錄內(nèi)容
let oldTitle = '簡(jiǎn)介';//記錄當(dāng)前標(biāo)題
let content = [];//記錄當(dāng)前內(nèi)容
//調(diào)用lineReader模塊的eachLine按行讀取接口,并設(shè)置讀取字節(jié)碼,方便字符編碼轉(zhuǎn)義
lineReader.eachLine(paths, {encoding: 'binary'}, function (line, last) {
//轉(zhuǎn)義
let oldStr = new Buffer(line, 'binary');
//默認(rèn)utf8
let str = iconv.decode(oldStr, 'utf8');
//判斷是否是gbk
if (str.indexOf('?') !== -1) {
str = iconv.decode(oldStr, 'gb2312');
}
//判斷是否按規(guī)則提取內(nèi)容
let chapterTest = /(^\第)(.{1,9})[章](\s*)(.*)|(^\[前言序章完本])(\s*)(.*)/;
if (chapterTest.test(str)) {
//保存章節(jié)
contents.push({
title: oldTitle,
filename: count,
content: content
});
oldTitle = str;
content = [];
count++;
} else {
if (str && (str.indexOf("第") === -1 && str.indexOf("卷") === -1) && (str.indexOf("第") === -1 && str.indexOf("部") === -1) && str.indexOf("===") === -1 && (str.indexOf("更多") === -1 && str.indexOf("http") === -1)) {
content.push(str);
}
}
//判斷是否到最后一行
if (last) {
contents.push({
title: oldTitle,
filename: count,
content: content
});
cb(null, contents);
}
});
}).catch(function (err) {
cb(new Error(err.message), null);
})
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者