@sie-js/vkp 1.0.4 → 1.0.6
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/.github/dependabot.yml +11 -0
- package/package.json +1 -1
- package/src/index.js +24 -3
- package/src/parser.js +27 -9
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
updates:
|
|
8
|
+
- package-ecosystem: "npm" # See documentation for possible values
|
|
9
|
+
directory: "/" # Location of package manifests
|
|
10
|
+
schedule:
|
|
11
|
+
interval: "weekly"
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -117,9 +117,26 @@ function vkpDetectContent(text) {
|
|
|
117
117
|
return "UNKNOWN";
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
// CP1251 -> UTF-8 + CRLF -> LF (with
|
|
120
|
+
// CP1251 -> UTF-8 + CRLF -> LF (with RTF support)
|
|
121
121
|
async function vkpNormalizeWithRTF(text) {
|
|
122
|
+
if (!Buffer.isBuffer(text))
|
|
123
|
+
throw new Error(`Patch text is not Buffer!`);
|
|
124
|
+
|
|
122
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
|
|
123
140
|
let parsed = await new Promise((resolve, reject) => {
|
|
124
141
|
RTFParser.string(text, (err, doc) => {
|
|
125
142
|
if (err) {
|
|
@@ -129,9 +146,11 @@ async function vkpNormalizeWithRTF(text) {
|
|
|
129
146
|
}
|
|
130
147
|
});
|
|
131
148
|
});
|
|
149
|
+
|
|
132
150
|
let lines = [];
|
|
133
|
-
for (let p of parsed.content)
|
|
151
|
+
for (let p of parsed.content) {
|
|
134
152
|
lines.push(p.content.map((s) => s.value).join(''));
|
|
153
|
+
}
|
|
135
154
|
return lines.join('\n');
|
|
136
155
|
}
|
|
137
156
|
return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
|
|
@@ -139,6 +158,8 @@ async function vkpNormalizeWithRTF(text) {
|
|
|
139
158
|
|
|
140
159
|
// CP1251 -> UTF-8 + CRLF -> LF
|
|
141
160
|
function vkpNormalize(text) {
|
|
161
|
+
if (!Buffer.isBuffer(text))
|
|
162
|
+
throw new Error(`Patch text is not Buffer!`);
|
|
142
163
|
return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
|
|
143
164
|
}
|
|
144
165
|
|
|
@@ -147,4 +168,4 @@ function vkpCanonicalize(text) {
|
|
|
147
168
|
return iconv.encode(text.replace(/(\r\n|\n|\r)/g, "\r\n"), 'windows-1251');
|
|
148
169
|
}
|
|
149
170
|
|
|
150
|
-
export { vkpParse, vkpRawParser, vkpNormalize, vkpNormalizeWithRTF, vkpCanonicalize, vkpDetectContent };
|
|
171
|
+
export { vkpParse, vkpRawParser, vkpNormalize, vkpNormalizeWithRTF, vkpCanonicalize, vkpDetectContent, VkpParseError };
|
package/src/parser.js
CHANGED
|
@@ -97,6 +97,9 @@ function vkpRawParser(text, options = {}) {
|
|
|
97
97
|
throw new VkpParseError("Syntax error", getLocation());
|
|
98
98
|
}
|
|
99
99
|
} catch (e) {
|
|
100
|
+
if (!(e instanceof VkpParseError))
|
|
101
|
+
throw e;
|
|
102
|
+
|
|
100
103
|
let loc = getLocation();
|
|
101
104
|
let token;
|
|
102
105
|
while ((token = nextToken())) {
|
|
@@ -122,7 +125,8 @@ function parseComments() {
|
|
|
122
125
|
} else if (token.type == TOKEN.COMMENT || token.type == TOKEN.MULTILINE_COMMENT || token.type == TOKEN.UNFINISHED_COMMENT) {
|
|
123
126
|
if (token.type == TOKEN.UNFINISHED_COMMENT)
|
|
124
127
|
state.onWarning(new VkpParseError(`Unfinished multiline comment`, getLocation()));
|
|
125
|
-
comments.push(
|
|
128
|
+
comments.push(parseCommentValue(token.value));
|
|
129
|
+
nextToken();
|
|
126
130
|
} else {
|
|
127
131
|
break;
|
|
128
132
|
}
|
|
@@ -133,16 +137,16 @@ function parseComments() {
|
|
|
133
137
|
function parsePatchPragma() {
|
|
134
138
|
let pragma = parsePragmaValue(peekToken().value);
|
|
135
139
|
nextToken();
|
|
136
|
-
let
|
|
137
|
-
return { pragma,
|
|
140
|
+
let comment = parseCommentsAfterExpr();
|
|
141
|
+
return { pragma, comment };
|
|
138
142
|
}
|
|
139
143
|
|
|
140
144
|
function parsePatchOffsetCorrector() {
|
|
141
145
|
let text = peekToken().value;
|
|
142
146
|
let offset = parseOffsetValue(text);
|
|
143
147
|
nextToken();
|
|
144
|
-
let
|
|
145
|
-
return { text, offset,
|
|
148
|
+
let comment = parseCommentsAfterExpr();
|
|
149
|
+
return { text, offset, comment };
|
|
146
150
|
}
|
|
147
151
|
|
|
148
152
|
function parsePatchRecord() {
|
|
@@ -160,10 +164,10 @@ function parsePatchRecord() {
|
|
|
160
164
|
if (!data.length)
|
|
161
165
|
throw new VkpParseError(`Empty patch data record!`, getLocation());
|
|
162
166
|
|
|
163
|
-
let
|
|
167
|
+
let comment = parseCommentsAfterExpr();
|
|
164
168
|
return {
|
|
165
169
|
address,
|
|
166
|
-
|
|
170
|
+
comment,
|
|
167
171
|
old: data.length == 2 ? data[0] : null,
|
|
168
172
|
new: data.length == 2 ? data[1] : data[0],
|
|
169
173
|
};
|
|
@@ -244,12 +248,13 @@ function parseCommentsAfterExpr() {
|
|
|
244
248
|
} else if (token.type == TOKEN.COMMENT || token.type == TOKEN.MULTILINE_COMMENT || token.type == TOKEN.UNFINISHED_COMMENT) {
|
|
245
249
|
if (token.type == TOKEN.UNFINISHED_COMMENT)
|
|
246
250
|
state.onWarning(new VkpParseError(`Unfinished multiline comment`, getLocation()));
|
|
247
|
-
comments.push(
|
|
251
|
+
comments.push(parseCommentValue(token.value));
|
|
252
|
+
nextToken();
|
|
248
253
|
} else {
|
|
249
254
|
throw new VkpParseError("Syntax error", getLocation());
|
|
250
255
|
}
|
|
251
256
|
}
|
|
252
|
-
return comments;
|
|
257
|
+
return comments.join(" ");
|
|
253
258
|
}
|
|
254
259
|
|
|
255
260
|
function nextToken() {
|
|
@@ -272,6 +277,19 @@ function prevToken() {
|
|
|
272
277
|
/**
|
|
273
278
|
* Token value parsers
|
|
274
279
|
* */
|
|
280
|
+
function parseCommentValue(v) {
|
|
281
|
+
if (v.startsWith(';')) {
|
|
282
|
+
return v.substr(1);
|
|
283
|
+
} else if (v.startsWith('#')) {
|
|
284
|
+
return v.substr(1);
|
|
285
|
+
} else if (v.startsWith('//')) {
|
|
286
|
+
return v.substr(2);
|
|
287
|
+
} else if (v.startsWith('/*')) {
|
|
288
|
+
return v.slice(2, -2);
|
|
289
|
+
}
|
|
290
|
+
throw new VkpParseError(`Invalid comment type`, getLocation());
|
|
291
|
+
}
|
|
292
|
+
|
|
275
293
|
function parseAnyNumberValue(v) {
|
|
276
294
|
let m;
|
|
277
295
|
let tmpBuffer = Buffer.allocUnsafe(8);
|