@sie-js/vkp 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/index.js +43 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sie-js/vkp",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Parser and utils for the .VKP files format which is used in the V-Klay.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"iconv-lite": "^0.6.3",
|
|
16
|
-
"moo": "^0.5.2"
|
|
16
|
+
"moo": "^0.5.2",
|
|
17
|
+
"rtf-parser": "^1.3.3"
|
|
17
18
|
},
|
|
18
19
|
"devDependencies": {
|
|
19
20
|
"glob": "^10.3.12",
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import iconv from 'iconv-lite';
|
|
2
2
|
import { vkpRawParser } from './parser.js';
|
|
3
3
|
import { VkpParseError } from './VkpParseError.js';
|
|
4
|
+
import RTFParser from 'rtf-parser';
|
|
4
5
|
|
|
5
6
|
const DEFAULT_PRAGMAS = {
|
|
6
7
|
warn_no_old_on_apply: true,
|
|
@@ -116,8 +117,49 @@ function vkpDetectContent(text) {
|
|
|
116
117
|
return "UNKNOWN";
|
|
117
118
|
}
|
|
118
119
|
|
|
120
|
+
// CP1251 -> UTF-8 + CRLF -> LF (with RTF support)
|
|
121
|
+
async function vkpNormalizeWithRTF(text) {
|
|
122
|
+
if (!Buffer.isBuffer(text))
|
|
123
|
+
throw new Error(`Patch text is not Buffer!`);
|
|
124
|
+
|
|
125
|
+
if (text.indexOf('{\\rtf1') >= 0) {
|
|
126
|
+
// Strip RTF images
|
|
127
|
+
while (true) {
|
|
128
|
+
let pictureIndex = text.indexOf('{\\pict');
|
|
129
|
+
if (pictureIndex >= 0) {
|
|
130
|
+
let pictureEndIndex = text.indexOf('}', pictureIndex);
|
|
131
|
+
if (pictureIndex >= 0) {
|
|
132
|
+
text = Buffer.concat([ text.slice(0, pictureIndex), text.slice(pictureEndIndex + 1) ]);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
text = text.toString('utf-8').replace(/{\\pict.*?\}/gsi, ''); // remove pictures
|
|
140
|
+
let parsed = await new Promise((resolve, reject) => {
|
|
141
|
+
RTFParser.string(text, (err, doc) => {
|
|
142
|
+
if (err) {
|
|
143
|
+
reject(err);
|
|
144
|
+
} else {
|
|
145
|
+
resolve(doc);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
let lines = [];
|
|
151
|
+
for (let p of parsed.content) {
|
|
152
|
+
lines.push(p.content.map((s) => s.value).join(''));
|
|
153
|
+
}
|
|
154
|
+
return lines.join('\n');
|
|
155
|
+
}
|
|
156
|
+
return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
|
|
157
|
+
}
|
|
158
|
+
|
|
119
159
|
// CP1251 -> UTF-8 + CRLF -> LF
|
|
120
160
|
function vkpNormalize(text) {
|
|
161
|
+
if (!Buffer.isBuffer(text))
|
|
162
|
+
throw new Error(`Patch text is not Buffer!`);
|
|
121
163
|
return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
|
|
122
164
|
}
|
|
123
165
|
|
|
@@ -126,4 +168,4 @@ function vkpCanonicalize(text) {
|
|
|
126
168
|
return iconv.encode(text.replace(/(\r\n|\n|\r)/g, "\r\n"), 'windows-1251');
|
|
127
169
|
}
|
|
128
170
|
|
|
129
|
-
export { vkpParse, vkpRawParser, vkpNormalize, vkpCanonicalize, vkpDetectContent };
|
|
171
|
+
export { vkpParse, vkpRawParser, vkpNormalize, vkpNormalizeWithRTF, vkpCanonicalize, vkpDetectContent };
|