-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
154 lines (148 loc) · 5.69 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//文件下载
import * as path from "path";
import cheerio from "cheerio";
import { promises as fs } from "fs";
import Tool from "./src/tool";
import zip from "zip-local";
/**
* 地方志网站地址
*/
export const urlBook = "http://www.hnsqw.com.cn/zmdsjk/zmdsz/zmddqzs/"
let ncx = `<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<ncx version="2005-1"
xmlns="http://www.daisy.org/z3986/2005/ncx/">
<head>
<meta content="3" name="dtb:depth"/>
</head>
<docTitle>
<text>地方志</text>
</docTitle>
<navMap>
</navMap>
</ncx>`
/**
* 信息资源文件
*/
let opf = `<?xml version="1.0" encoding="utf-8"?>
<package version="2.0" unique-identifier="uuid_id" xmlns="http://www.idpf.org/2007/opf">
<metadata xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/" xmlns:opf="http://www.idpf.org/2007/opf"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dc:title>地方志</dc:title>
<dc:language>zh</dc:language>
<dc:date opf:event="modification">2016-03-12</dc:date>
<dc:type>地方志</dc:type>
</metadata>
<manifest>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
<item id="style.css" href="Styles/style.css" media-type="text/css"/>
</manifest>
<spine toc="ncx">
</spine>
<guide>
</guide>
</package>`
/**
* 描述文件
*/
let metaINF = `<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>`
/**
* 创建文件夹目录
*/
export let dirPath = path.join(__dirname, "file");
Tool.mkdir(dirPath);
Tool.getHtml(urlBook).then(async res => {
const $ = cheerio.load(res as string);
for (const item of $("[src]").toArray()) {
const temp = $(item).attr("src")
// 是否是XML文件
if (temp.includes("xml")) {
// 下载目录文件
await Tool.downFile(urlBook + temp, temp, dirPath);
// 调用主要处理
await main(path.join(dirPath, temp));
}
}
})
function main(params: string) {
return fs.readFile(params).then(async result => {
/**
* XML目录
*/
const $ = cheerio.load(result, { xmlMode: true });
/**
* 地方志标题
*/
const title = $("TITLE").children("a").text().replace(/\s/, "")
dirPath = path.join(dirPath, title); // 根据标题生产目录
Tool.mkdir(dirPath) // 创建根目录
Tool.mkdirAll(dirPath) // 创建所以
/**
* ncx目录
*/
const $ncx = cheerio.load(ncx, { xmlMode: true });
$ncx("docTitle").children("text").text(title)
/**
* opf组织XML:配置文件
*/
const $opf = cheerio.load(opf, { xmlMode: true })
$opf("dc\\:title").text(title)
$opf("dc\\:date").text(new Date().toLocaleDateString())
/**
* 信息文件写入
*/
await fs.writeFile(path.join(dirPath, "/META-INF/container.xml"), metaINF)
/**
* 下载链接数组
*/
let linkArray: string[] = [];
$("STRUCTURE").eq(0).children().toArray().map(PIECE => {
if (PIECE.attribs) {
linkArray.push(PIECE.attribs.link)
$ncx("navMap").append(Tool.getNcx(PIECE, linkArray.length))
/**
* 一级索引
*/
const indexPIECE = linkArray.length;
// 下一级
PIECE.children.map(CHAPTER => {
if (CHAPTER.attribs) {
linkArray.push(CHAPTER.attribs.link);
$ncx(`navPoint[playOrder="${indexPIECE}"]`).append(Tool.getNcx(CHAPTER, linkArray.length));
/**
* 二级索引
*/
const indexCHAPTER = linkArray.length;
// 下一级
CHAPTER.children.map(SECTION => {
if (SECTION.attribs) {
linkArray.push(SECTION.attribs.link)
$ncx(`navPoint[playOrder="${indexCHAPTER}"]`).append(Tool.getNcx(SECTION, linkArray.length))
}
})
}
})
}
})
linkArray = [...new Set(linkArray)]
// 下载
await Tool.downHtmlFiles(linkArray, dirPath, $opf)
// 写入ncx目录
await fs.writeFile(path.join(dirPath, "/OEBPS/toc.ncx"), $ncx.html({ decodeEntities: false }))
// 写入opf配置
await fs.writeFile(path.join(dirPath, "/OEBPS/content.opf"), $opf.html({ decodeEntities: false }))
// 清理CSS
await Tool.cleanCSS();
// 打包
await zip.sync.zip(dirPath).compress().save(dirPath + ".epub");
}).catch(err => {
if (err) return new Error("打开XML文件错误!")
})
}