@sie-js/vkp 1.0.2 → 1.0.4

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/index.js +24 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sie-js/vkp",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
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,
@@ -104,6 +105,8 @@ function vkpParse(text, options) {
104
105
  }
105
106
 
106
107
  function vkpDetectContent(text) {
108
+ if (text.indexOf('{\\rtf1') >= 0)
109
+ return "RTF";
107
110
  let trimmedText = text.replace(/\/\*.*?\*\//gs, '').replace(/(\/\/|;|#).*?$/mg, '');
108
111
  if (trimmedText.match(/^\s*(0x[a-f0-9]+|[a-f0-9]+)\s*:[^\\/]/mi))
109
112
  return "PATCH";
@@ -114,6 +117,26 @@ function vkpDetectContent(text) {
114
117
  return "UNKNOWN";
115
118
  }
116
119
 
120
+ // CP1251 -> UTF-8 + CRLF -> LF (with RTC support)
121
+ async function vkpNormalizeWithRTF(text) {
122
+ if (text.indexOf('{\\rtf1') >= 0) {
123
+ let parsed = await new Promise((resolve, reject) => {
124
+ RTFParser.string(text, (err, doc) => {
125
+ if (err) {
126
+ reject(err);
127
+ } else {
128
+ resolve(doc);
129
+ }
130
+ });
131
+ });
132
+ let lines = [];
133
+ for (let p of parsed.content)
134
+ lines.push(p.content.map((s) => s.value).join(''));
135
+ return lines.join('\n');
136
+ }
137
+ return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
138
+ }
139
+
117
140
  // CP1251 -> UTF-8 + CRLF -> LF
118
141
  function vkpNormalize(text) {
119
142
  return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
@@ -124,4 +147,4 @@ function vkpCanonicalize(text) {
124
147
  return iconv.encode(text.replace(/(\r\n|\n|\r)/g, "\r\n"), 'windows-1251');
125
148
  }
126
149
 
127
- export { vkpParse, vkpRawParser, vkpNormalize, vkpCanonicalize, vkpDetectContent };
150
+ export { vkpParse, vkpRawParser, vkpNormalize, vkpNormalizeWithRTF, vkpCanonicalize, vkpDetectContent };