-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnexFile.mjs
186 lines (166 loc) · 5.4 KB
/
EnexFile.mjs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { default as sax } from 'sax';
import { createReadStream } from 'fs';
import atob from 'atob';
/** Parses an enex file and provides access to the documents (pdfs) it contains */
export default class EnexFile {
constructor(filePath) {
const documents = []
/**
* to process the parsed xml we use contexts that match
* the XML Elements we're interested.
*
* A context may have the following functions:
* - open: invoked when the context is opened, i.e. when the opening tag is parsed
* - close: invoked when the closing tag is parsed
* - text, cdata: invoked when a text/cdata section is parsed
*
* A context may also have an property tags mapping element-names to contexts. When
* a respective opening tag is encountered the context is set to tags[tagName] till
* the element is closed.
*/
const rootContext = {
tags: {
note: {
open: function () {
this.value = {
pdfs: [],
tags: []
}
},
tags: {
title: {
text: function (value) {
rootContext.tags.note.value.title = value
}
},
created: {
text: function (value) {
rootContext.tags.note.value.created = value
}
},
tag: {
text: function (value) {
rootContext.tags.note.value.tags.push(value)
}
},
resource: {
open: function () {
this.value = {}
},
tags: {
data: {
text: function (value) {
let binaryString = atob(value)
let len, offset, bytes
if (rootContext.tags.note.tags.resource.value.data) {
offset = rootContext.tags.note.tags.resource.value.data.length
len = binaryString.length + offset
bytes = new Uint8Array(len)
bytes.set(rootContext.tags.note.tags.resource.value.data)
} else {
len = binaryString.length
bytes = new Uint8Array(len)
offset = 0
}
for (var i = offset; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i - offset)
}
rootContext.tags.note.tags.resource.value.data = bytes
}
},
mime: {
text: function (value) {
rootContext.tags.note.tags.resource.value.mime = value
}
},
'file-name': {
text: function (value) {
rootContext.tags.note.tags.resource.value.fileName = value
}
}
},
close: function () {
if (this.value.mime === 'application/pdf') {
rootContext.tags.note.value.pdfs.push({
fileName: this.value.fileName,
data: this.value.data
})
}
}
},
content: {
cdata: function (value) {
rootContext.tags.note.value.content = value
}
},
},
close: function () {
this.value.pdfs.forEach(pdf => {
const { pdfs, ...document } = this.value
document.pdf = pdf
if (pdfs.length > 1) {
document.title += ' - ' + pdf.fileName
}
documents.push(document)
})
}
}
}
}
const saxStream = sax.createStream(true, {})
saxStream.on("error", function (e) {
console.error("error!", e)
})
saxStream.on("opentag", function (node) {
if (currentContext && currentContext.tags) {
if (currentContext.tags[node.name]) {
setContext(currentContext.tags[node.name])
if (currentContext.open) currentContext.open(node)
currentContextTag = node.name
}
}
})
saxStream.on("text", function (value) {
if (currentContext && currentContext.text) {
currentContext.text(value)
}
})
saxStream.on("cdata", function (value) {
if (currentContext && currentContext.cdata) {
currentContext.cdata(value)
}
})
saxStream.on("closetag", function (tagName) {
if (currentContextTag === tagName) {
if (currentContext.close) currentContext.close()
resetContext()
}
//console.log('closed tag '+ tagName)
})
this.documentsPromise = new Promise((resolve, reject) => {
saxStream.on("end", function () {
resolve(documents)
})
})
let currentContextTag = null
let currentContext = null
const contextStack = []
function setContext(c) {
contextStack.push({
contex: currentContext,
tag: currentContextTag
})
currentContext = c
}
function resetContext() {
const stackEntry = contextStack.pop()
currentContext = stackEntry.contex
currentContextTag = stackEntry.tag
}
setContext(rootContext)
createReadStream(filePath).pipe(saxStream)
}
getDocuments() {
return this.documentsPromise
}
}