@customyai/cli 0.7.2 → 0.11.0
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/CHANGELOG.md +82 -0
- package/LICENSE +2 -1
- package/README.md +84 -5
- package/dist/bin.cjs +6 -2
- package/dist/bin.mjs +5 -1
- package/dist/chunk-2A6WFLXS.cjs +235 -0
- package/dist/chunk-3M6ZLCSN.mjs +237 -0
- package/dist/chunk-ABK4QQWW.cjs +244 -0
- package/dist/chunk-BYMLRMNS.mjs +27 -0
- package/dist/chunk-MFJL3NO5.cjs +31 -0
- package/dist/chunk-QKIF4C5R.mjs +226 -0
- package/dist/chunk-VA4VPLZ4.mjs +3266 -0
- package/dist/chunk-VKMVRAMV.cjs +3286 -0
- package/dist/index.cjs +32 -11
- package/dist/index.d.cts +73 -3
- package/dist/index.d.ts +73 -3
- package/dist/index.mjs +2 -1
- package/dist/json-NETFQXLJ.mjs +67 -0
- package/dist/json-NSJMTBPP.cjs +70 -0
- package/dist/po-AKWHAYBL.mjs +387 -0
- package/dist/po-IJGMXNVY.cjs +394 -0
- package/dist/pseudo-CKYMJJBX.mjs +101 -0
- package/dist/pseudo-LBLMBD2T.cjs +106 -0
- package/dist/quality-6SEZNA6Q.cjs +207 -0
- package/dist/quality-ILK6IA5O.mjs +199 -0
- package/dist/xliff-DEJHB2SY.mjs +512 -0
- package/dist/xliff-DXICWMWC.cjs +518 -0
- package/package.json +9 -3
- package/dist/chunk-762M6SBX.mjs +0 -1098
- package/dist/chunk-TSPERSQL.cjs +0 -1113
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import { I18nToolsError, compareCodeUnits, utf8ByteLength } from './chunk-BYMLRMNS.mjs';
|
|
2
|
+
import { CATALOG_LOCALE, CATALOG_KEY, CATALOG_NAMESPACE, CATALOG_DOC_VERSION } from './chunk-QKIF4C5R.mjs';
|
|
3
|
+
import { tokenizeIcu } from './chunk-3M6ZLCSN.mjs';
|
|
4
|
+
|
|
5
|
+
// src/vendor/i18n-tools/xml.ts
|
|
6
|
+
var DEFAULT_XML_LIMITS = { maxBytes: 4 * 1024 * 1024, maxDepth: 32, maxNodes: 2e5, maxAttributes: 32, maxNameLength: 128 };
|
|
7
|
+
var XML_NS = "http://www.w3.org/XML/1998/namespace";
|
|
8
|
+
var INVALID_CHAR = /[^\u0009\u000A\u000D --�\u{10000}-\u{10FFFF}]/u;
|
|
9
|
+
var NAME_RE = /[\p{L}_:][\p{L}\p{N}._:\-·]*/uy;
|
|
10
|
+
var NCNAME = /^[\p{L}_][\p{L}\p{N}._\-·]*$/u;
|
|
11
|
+
var fail = (code, text, src, pos) => {
|
|
12
|
+
let line = 1;
|
|
13
|
+
let last = -1;
|
|
14
|
+
for (let k = src.indexOf("\n"); k !== -1 && k < pos; k = src.indexOf("\n", k + 1)) {
|
|
15
|
+
line += 1;
|
|
16
|
+
last = k;
|
|
17
|
+
}
|
|
18
|
+
throw new I18nToolsError(code, `${text} (l\xEDnea ${line}, columna ${pos - last})`);
|
|
19
|
+
};
|
|
20
|
+
function isXmlChar(cp) {
|
|
21
|
+
return cp === 9 || cp === 10 || cp === 13 || cp >= 32 && cp <= 55295 || cp >= 57344 && cp <= 65533 || cp >= 65536 && cp <= 1114111;
|
|
22
|
+
}
|
|
23
|
+
function decodeEntities(raw, src, pos) {
|
|
24
|
+
if (!raw.includes("&")) return raw;
|
|
25
|
+
let out = "";
|
|
26
|
+
let i = 0;
|
|
27
|
+
while (i < raw.length) {
|
|
28
|
+
const amp = raw.indexOf("&", i);
|
|
29
|
+
if (amp === -1) {
|
|
30
|
+
out += raw.slice(i);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
out += raw.slice(i, amp);
|
|
34
|
+
const semi = raw.indexOf(";", amp + 1);
|
|
35
|
+
if (semi === -1 || semi - amp > 12) return fail("malformed-xml", "referencia a entidad sin cerrar", src, pos + amp);
|
|
36
|
+
const body = raw.slice(amp + 1, semi);
|
|
37
|
+
if (body === "amp") out += "&";
|
|
38
|
+
else if (body === "lt") out += "<";
|
|
39
|
+
else if (body === "gt") out += ">";
|
|
40
|
+
else if (body === "quot") out += '"';
|
|
41
|
+
else if (body === "apos") out += "'";
|
|
42
|
+
else if (/^#x[0-9A-Fa-f]{1,6}$/.test(body) || /^#[0-9]{1,7}$/.test(body)) {
|
|
43
|
+
const cp = body[1] === "x" ? Number.parseInt(body.slice(2), 16) : Number.parseInt(body.slice(1), 10);
|
|
44
|
+
if (!isXmlChar(cp)) return fail("invalid-char", `la referencia &${body}; no es un car\xE1cter XML v\xE1lido`, src, pos + amp);
|
|
45
|
+
out += String.fromCodePoint(cp);
|
|
46
|
+
} else return fail("undefined-entity", `entidad no definida &${body}; (no se admiten entidades propias ni externas)`, src, pos + amp);
|
|
47
|
+
i = semi + 1;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
function decodeXmlBytes(input, maxBytes) {
|
|
52
|
+
if (typeof input === "string") {
|
|
53
|
+
if (input.length > maxBytes || utf8ByteLength(input) > maxBytes) throw new I18nToolsError("too-large", `el documento pasa de ${maxBytes} bytes`);
|
|
54
|
+
return input.charCodeAt(0) === 65279 ? input.slice(1) : input;
|
|
55
|
+
}
|
|
56
|
+
if (input.length > maxBytes) throw new I18nToolsError("too-large", `el documento pasa de ${maxBytes} bytes`);
|
|
57
|
+
if (input.length >= 2 && (input[0] === 255 && input[1] === 254 || input[0] === 254 && input[1] === 255)) {
|
|
58
|
+
throw new I18nToolsError("invalid-encoding", "solo se admite UTF-8 (se detect\xF3 UTF-16)");
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: false }).decode(input).replace(/^\uFEFF/, "");
|
|
62
|
+
} catch {
|
|
63
|
+
throw new I18nToolsError("invalid-encoding", "el documento no es UTF-8 v\xE1lido");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function parseXml(input, limits = {}) {
|
|
67
|
+
const lim = { ...DEFAULT_XML_LIMITS, ...limits };
|
|
68
|
+
const decoded = decodeXmlBytes(input, lim.maxBytes);
|
|
69
|
+
const bad = INVALID_CHAR.exec(decoded);
|
|
70
|
+
if (bad) fail("invalid-char", `car\xE1cter no permitido en XML (U+${(bad[0].codePointAt(0) ?? 0).toString(16).toUpperCase().padStart(4, "0")})`, decoded, bad.index);
|
|
71
|
+
const src = decoded.replace(/\r\n?/g, "\n");
|
|
72
|
+
const n = src.length;
|
|
73
|
+
let pos = 0;
|
|
74
|
+
let nodes = 0;
|
|
75
|
+
const skipWs = () => {
|
|
76
|
+
while (pos < n && (src[pos] === " " || src[pos] === " " || src[pos] === "\n")) pos += 1;
|
|
77
|
+
};
|
|
78
|
+
const count = () => {
|
|
79
|
+
nodes += 1;
|
|
80
|
+
if (nodes > lim.maxNodes) fail("too-many-nodes", `m\xE1s de ${lim.maxNodes} nodos`, src, pos);
|
|
81
|
+
};
|
|
82
|
+
const readName = (what) => {
|
|
83
|
+
NAME_RE.lastIndex = pos;
|
|
84
|
+
const m = NAME_RE.exec(src);
|
|
85
|
+
if (!m) return fail("malformed-xml", `se esperaba ${what}`, src, pos);
|
|
86
|
+
if (m[0].length > lim.maxNameLength) fail("too-large", "nombre demasiado largo", src, pos);
|
|
87
|
+
pos += m[0].length;
|
|
88
|
+
return m[0];
|
|
89
|
+
};
|
|
90
|
+
if (src.startsWith("<?xml") && /\s/.test(src[5] ?? "")) {
|
|
91
|
+
const end = src.indexOf("?>");
|
|
92
|
+
if (end === -1) fail("malformed-xml", "declaraci\xF3n XML sin cerrar", src, 0);
|
|
93
|
+
const decl = src.slice(5, end);
|
|
94
|
+
const version = /\bversion\s*=\s*(?:"([^"]*)"|'([^']*)')/.exec(decl);
|
|
95
|
+
if (!version || (version[1] ?? version[2]) !== "1.0") fail("malformed-xml", "solo se admite XML 1.0", src, 0);
|
|
96
|
+
const enc = /\bencoding\s*=\s*(?:"([^"]*)"|'([^']*)')/.exec(decl);
|
|
97
|
+
if (enc && (enc[1] ?? enc[2] ?? "").toLowerCase() !== "utf-8") fail("invalid-encoding", 'solo se admite encoding="UTF-8"', src, 0);
|
|
98
|
+
if (!/^(?:\s+(?:version|encoding|standalone)\s*=\s*(?:"[^"]*"|'[^']*'))+\s*$/.test(decl)) fail("malformed-xml", "declaraci\xF3n XML mal formada", src, 0);
|
|
99
|
+
pos = end + 2;
|
|
100
|
+
}
|
|
101
|
+
const skipMisc = () => {
|
|
102
|
+
for (; ; ) {
|
|
103
|
+
skipWs();
|
|
104
|
+
if (src.startsWith("<!--", pos)) {
|
|
105
|
+
const end = src.indexOf("-->", pos + 4);
|
|
106
|
+
if (end === -1) fail("malformed-xml", "comentario sin cerrar", src, pos);
|
|
107
|
+
if (src.slice(pos + 4, end).includes("--")) fail("malformed-xml", "\xAB--\xBB dentro de un comentario", src, pos);
|
|
108
|
+
pos = end + 3;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const rejectBang = () => {
|
|
115
|
+
if (/^<!DOCTYPE/i.test(src.slice(pos, pos + 9))) return fail("doctype-forbidden", "DOCTYPE no permitido (sin DTD ni entidades externas)", src, pos);
|
|
116
|
+
return fail("forbidden-construct", "construcci\xF3n \xAB<!\u2026\xBB no permitida", src, pos);
|
|
117
|
+
};
|
|
118
|
+
skipMisc();
|
|
119
|
+
if (pos >= n) fail("malformed-xml", "documento vac\xEDo", src, pos);
|
|
120
|
+
if (src.startsWith("<!", pos) && !src.startsWith("<!--", pos)) rejectBang();
|
|
121
|
+
if (src.startsWith("<?", pos)) fail("forbidden-construct", "instrucciones de proceso no permitidas", src, pos);
|
|
122
|
+
if (src[pos] !== "<") fail("malformed-xml", "se esperaba el elemento ra\xEDz", src, pos);
|
|
123
|
+
const stack = [];
|
|
124
|
+
let root = null;
|
|
125
|
+
let text = [];
|
|
126
|
+
const flushText = () => {
|
|
127
|
+
if (text.length === 0) return;
|
|
128
|
+
const top = stack[stack.length - 1];
|
|
129
|
+
const joined = text.join("");
|
|
130
|
+
text = [];
|
|
131
|
+
if (!top || joined === "") return;
|
|
132
|
+
count();
|
|
133
|
+
top.children.push(joined);
|
|
134
|
+
};
|
|
135
|
+
while (pos < n) {
|
|
136
|
+
if (stack.length === 0 && root) {
|
|
137
|
+
skipMisc();
|
|
138
|
+
if (pos < n) {
|
|
139
|
+
if (src.startsWith("<!", pos)) rejectBang();
|
|
140
|
+
fail("malformed-xml", "contenido despu\xE9s del elemento ra\xEDz", src, pos);
|
|
141
|
+
}
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
if (src[pos] !== "<") {
|
|
145
|
+
let end = src.indexOf("<", pos);
|
|
146
|
+
if (end === -1) end = n;
|
|
147
|
+
const chunk = src.slice(pos, end);
|
|
148
|
+
if (chunk.includes("]]>")) fail("malformed-xml", "\xAB]]>\xBB no permitido en el texto", src, pos);
|
|
149
|
+
text.push(decodeEntities(chunk, src, pos));
|
|
150
|
+
pos = end;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (src.startsWith("<!--", pos)) {
|
|
154
|
+
const end = src.indexOf("-->", pos + 4);
|
|
155
|
+
if (end === -1) fail("malformed-xml", "comentario sin cerrar", src, pos);
|
|
156
|
+
if (src.slice(pos + 4, end).includes("--")) fail("malformed-xml", "\xAB--\xBB dentro de un comentario", src, pos);
|
|
157
|
+
pos = end + 3;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (src.startsWith("<![CDATA[", pos)) {
|
|
161
|
+
const end = src.indexOf("]]>", pos + 9);
|
|
162
|
+
if (end === -1) fail("malformed-xml", "CDATA sin cerrar", src, pos);
|
|
163
|
+
text.push(src.slice(pos + 9, end));
|
|
164
|
+
pos = end + 3;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (src.startsWith("<!", pos)) rejectBang();
|
|
168
|
+
if (src.startsWith("<?", pos)) fail("forbidden-construct", "instrucciones de proceso no permitidas", src, pos);
|
|
169
|
+
if (src[pos + 1] === "/") {
|
|
170
|
+
flushText();
|
|
171
|
+
const open = stack.pop();
|
|
172
|
+
pos += 2;
|
|
173
|
+
if (!open) return fail("malformed-xml", "etiqueta de cierre sin abrir", src, pos);
|
|
174
|
+
const name2 = readName("el nombre de la etiqueta de cierre");
|
|
175
|
+
if (name2 !== open.name) fail("malformed-xml", `se esperaba </${open.name}> y apareci\xF3 </${name2}>`, src, pos);
|
|
176
|
+
skipWs();
|
|
177
|
+
if (src[pos] !== ">") fail("malformed-xml", "etiqueta de cierre mal formada", src, pos);
|
|
178
|
+
pos += 1;
|
|
179
|
+
const el = { name: open.name, prefix: open.prefix, local: open.local, ns: open.ns, attrs: open.attrs, children: open.children };
|
|
180
|
+
const parent2 = stack[stack.length - 1];
|
|
181
|
+
if (parent2) parent2.children.push(el);
|
|
182
|
+
else root = el;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
flushText();
|
|
186
|
+
if (stack.length >= lim.maxDepth) fail("too-deep", `anidamiento de m\xE1s de ${lim.maxDepth} niveles`, src, pos);
|
|
187
|
+
count();
|
|
188
|
+
pos += 1;
|
|
189
|
+
const name = readName("el nombre del elemento");
|
|
190
|
+
const attrs = /* @__PURE__ */ Object.create(null);
|
|
191
|
+
let attrCount = 0;
|
|
192
|
+
let selfClose = false;
|
|
193
|
+
for (; ; ) {
|
|
194
|
+
const before = pos;
|
|
195
|
+
skipWs();
|
|
196
|
+
if (src[pos] === ">") {
|
|
197
|
+
pos += 1;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
if (src[pos] === "/" && src[pos + 1] === ">") {
|
|
201
|
+
pos += 2;
|
|
202
|
+
selfClose = true;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
if (pos === before) fail("malformed-xml", "falta un espacio entre atributos", src, pos);
|
|
206
|
+
if (pos >= n) fail("malformed-xml", "etiqueta sin cerrar", src, pos);
|
|
207
|
+
const an = readName("el nombre del atributo");
|
|
208
|
+
skipWs();
|
|
209
|
+
if (src[pos] !== "=") fail("malformed-xml", `el atributo ${an} necesita \xAB=\xBB`, src, pos);
|
|
210
|
+
pos += 1;
|
|
211
|
+
skipWs();
|
|
212
|
+
const q = src[pos];
|
|
213
|
+
if (q !== '"' && q !== "'") return fail("malformed-xml", `el valor de ${an} necesita comillas`, src, pos);
|
|
214
|
+
const end = src.indexOf(q, pos + 1);
|
|
215
|
+
if (end === -1) return fail("malformed-xml", "valor de atributo sin cerrar", src, pos);
|
|
216
|
+
const raw = src.slice(pos + 1, end);
|
|
217
|
+
if (raw.includes("<")) fail("malformed-xml", "\xAB<\xBB no permitido en un valor de atributo", src, pos);
|
|
218
|
+
if (Object.hasOwn(attrs, an)) fail("malformed-xml", `atributo repetido ${an}`, src, pos);
|
|
219
|
+
attrCount += 1;
|
|
220
|
+
if (attrCount > lim.maxAttributes) fail("too-many-nodes", `m\xE1s de ${lim.maxAttributes} atributos`, src, pos);
|
|
221
|
+
attrs[an] = decodeEntities(raw.replace(/[\t\n]/g, " "), src, pos + 1);
|
|
222
|
+
pos = end + 1;
|
|
223
|
+
}
|
|
224
|
+
const parent = stack[stack.length - 1];
|
|
225
|
+
const scope = /* @__PURE__ */ Object.create(null);
|
|
226
|
+
if (parent) Object.assign(scope, parent.scope);
|
|
227
|
+
else scope["xml"] = XML_NS;
|
|
228
|
+
for (const [an, av] of Object.entries(attrs)) {
|
|
229
|
+
if (an === "xmlns") scope[""] = av;
|
|
230
|
+
else if (an.startsWith("xmlns:")) {
|
|
231
|
+
const p = an.slice(6);
|
|
232
|
+
if (!NCNAME.test(p) || p === "xmlns" || p === "xml") fail("malformed-xml", `prefijo de espacio de nombres no permitido ${p}`, src, pos);
|
|
233
|
+
if (av === "") fail("malformed-xml", `el prefijo ${p} no puede quedar vac\xEDo`, src, pos);
|
|
234
|
+
scope[p] = av;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const colon = name.indexOf(":");
|
|
238
|
+
const prefix = colon === -1 ? null : name.slice(0, colon);
|
|
239
|
+
const local = colon === -1 ? name : name.slice(colon + 1);
|
|
240
|
+
if (colon !== -1 && (!NCNAME.test(prefix ?? "") || !NCNAME.test(local))) fail("malformed-xml", `nombre calificado no v\xE1lido ${name}`, src, pos);
|
|
241
|
+
for (const an of Object.keys(attrs)) {
|
|
242
|
+
const c = an.indexOf(":");
|
|
243
|
+
if (c > 0 && !an.startsWith("xmlns:") && !Object.hasOwn(scope, an.slice(0, c))) fail("malformed-xml", `prefijo no declarado en el atributo ${an}`, src, pos);
|
|
244
|
+
}
|
|
245
|
+
let ns;
|
|
246
|
+
if (prefix === null) ns = scope[""] ? scope[""] : null;
|
|
247
|
+
else {
|
|
248
|
+
const uri = scope[prefix];
|
|
249
|
+
if (uri === void 0) return fail("malformed-xml", `prefijo no declarado ${prefix}`, src, pos);
|
|
250
|
+
ns = uri;
|
|
251
|
+
}
|
|
252
|
+
const frame = { name, prefix, local, ns, attrs, children: [], scope };
|
|
253
|
+
if (selfClose) {
|
|
254
|
+
const el = { name, prefix, local, ns, attrs, children: [] };
|
|
255
|
+
if (parent) parent.children.push(el);
|
|
256
|
+
else root = el;
|
|
257
|
+
} else stack.push(frame);
|
|
258
|
+
}
|
|
259
|
+
if (stack.length > 0) fail("malformed-xml", `falta cerrar <${stack[stack.length - 1]?.name ?? ""}>`, src, pos);
|
|
260
|
+
if (!root) fail("malformed-xml", "sin elemento ra\xEDz", src, pos);
|
|
261
|
+
return root;
|
|
262
|
+
}
|
|
263
|
+
function elementText(el) {
|
|
264
|
+
let out = "";
|
|
265
|
+
for (const c of el.children) if (typeof c === "string") out += c;
|
|
266
|
+
return out;
|
|
267
|
+
}
|
|
268
|
+
var escapeXmlText = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/\r/g, " ");
|
|
269
|
+
var escapeXmlAttr = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/\t/g, "	").replace(/\n/g, " ").replace(/\r/g, " ");
|
|
270
|
+
|
|
271
|
+
// src/vendor/i18n-tools/xliff.ts
|
|
272
|
+
var XLIFF_NS = "urn:oasis:names:tc:xliff:document:2.1";
|
|
273
|
+
var XLIFF_NS_20 = "urn:oasis:names:tc:xliff:document:2.0";
|
|
274
|
+
var XLIFF_STATES = ["initial", "translated", "reviewed", "final"];
|
|
275
|
+
var STATE_RANK = { initial: 0, translated: 1, reviewed: 2, final: 3 };
|
|
276
|
+
var SYNTAX_NOTE = "customy:syntax";
|
|
277
|
+
var attrSafe = (s) => escapeXmlAttr(s.replace(/[^\u0009\u000A\u000D --�\u{10000}-\u{10FFFF}]/gu, "\uFFFD"));
|
|
278
|
+
function encodeText(text) {
|
|
279
|
+
let out = "";
|
|
280
|
+
let run = "";
|
|
281
|
+
const flush = () => {
|
|
282
|
+
if (run) {
|
|
283
|
+
out += escapeXmlText(run);
|
|
284
|
+
run = "";
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
for (const ch of text) {
|
|
288
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
289
|
+
const valid = cp === 9 || cp === 10 || cp === 13 || cp >= 32 && cp <= 55295 || cp >= 57344 && cp <= 65533 || cp >= 65536 && cp <= 1114111;
|
|
290
|
+
if (valid) run += ch;
|
|
291
|
+
else {
|
|
292
|
+
flush();
|
|
293
|
+
out += `<cp hex="${cp.toString(16).toUpperCase().padStart(4, "0")}"/>`;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
flush();
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
var InlineIds = class {
|
|
300
|
+
n = 0;
|
|
301
|
+
dataByRaw = /* @__PURE__ */ new Map();
|
|
302
|
+
sourceIds = /* @__PURE__ */ new Map();
|
|
303
|
+
data = [];
|
|
304
|
+
dataId(raw) {
|
|
305
|
+
let id = this.dataByRaw.get(raw);
|
|
306
|
+
if (!id) {
|
|
307
|
+
id = `d${this.dataByRaw.size + 1}`;
|
|
308
|
+
this.dataByRaw.set(raw, id);
|
|
309
|
+
this.data.push({ id, raw });
|
|
310
|
+
}
|
|
311
|
+
return id;
|
|
312
|
+
}
|
|
313
|
+
/** `side` "source" reserva ids nuevos; "target" reutiliza los del origen por orden de aparición. */
|
|
314
|
+
phId(raw, side, seen) {
|
|
315
|
+
if (side === "source") {
|
|
316
|
+
this.n += 1;
|
|
317
|
+
const id = String(this.n);
|
|
318
|
+
const list = this.sourceIds.get(raw) ?? [];
|
|
319
|
+
list.push(id);
|
|
320
|
+
this.sourceIds.set(raw, list);
|
|
321
|
+
return id;
|
|
322
|
+
}
|
|
323
|
+
const k = seen.get(raw) ?? 0;
|
|
324
|
+
seen.set(raw, k + 1);
|
|
325
|
+
const existing = this.sourceIds.get(raw)?.[k];
|
|
326
|
+
if (existing) return existing;
|
|
327
|
+
this.n += 1;
|
|
328
|
+
return String(this.n);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
function encodeInline(tokens, ids, side) {
|
|
332
|
+
const seen = /* @__PURE__ */ new Map();
|
|
333
|
+
let out = "";
|
|
334
|
+
for (const t of tokens) {
|
|
335
|
+
if (t.kind === "text") {
|
|
336
|
+
out += encodeText(t.text);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const dataId = ids.dataId(t.raw);
|
|
340
|
+
out += `<ph id="${ids.phId(t.raw, side, seen)}" dataRef="${dataId}" equiv="${attrSafe(t.raw)}"/>`;
|
|
341
|
+
}
|
|
342
|
+
return out;
|
|
343
|
+
}
|
|
344
|
+
function exportXliff(options) {
|
|
345
|
+
const { source, target } = options;
|
|
346
|
+
if (!CATALOG_LOCALE.test(source.locale)) throw new I18nToolsError("invalid-locale", `idioma de origen no v\xE1lido \xAB${source.locale}\xBB`);
|
|
347
|
+
const trgLang = target?.locale ?? options.targetLocale ?? null;
|
|
348
|
+
if (trgLang !== null && !CATALOG_LOCALE.test(trgLang)) throw new I18nToolsError("invalid-locale", `idioma de destino no v\xE1lido \xAB${trgLang}\xBB`);
|
|
349
|
+
if (target && target.namespace !== source.namespace) throw new I18nToolsError("invalid-input", "origen y destino son de espacios de nombres distintos");
|
|
350
|
+
const dnt = new Set(options.doNotTranslate ?? []);
|
|
351
|
+
const keys = Object.keys(source.messages).sort(compareCodeUnits);
|
|
352
|
+
const out = [];
|
|
353
|
+
out.push('<?xml version="1.0" encoding="UTF-8"?>');
|
|
354
|
+
out.push(`<xliff xmlns="${XLIFF_NS}" version="2.1" srcLang="${escapeXmlAttr(source.locale)}"${trgLang ? ` trgLang="${escapeXmlAttr(trgLang)}"` : ""}>`);
|
|
355
|
+
out.push(` <file id="f1" original="${escapeXmlAttr(source.namespace)}">`);
|
|
356
|
+
out.push(` <notes><note category="${SYNTAX_NOTE}">${escapeXmlText(source.syntax)}</note></notes>`);
|
|
357
|
+
for (const key of keys) {
|
|
358
|
+
if (!CATALOG_KEY.test(key)) throw new I18nToolsError("invalid-key", `clave no v\xE1lida \xAB${key}\xBB`);
|
|
359
|
+
const src = source.messages[key] ?? "";
|
|
360
|
+
const trg = target ? target.messages[key] : void 0;
|
|
361
|
+
const ids = new InlineIds();
|
|
362
|
+
const srcXml = encodeInline(tokenizeIcu(src), ids, "source");
|
|
363
|
+
const trgXml = trg !== void 0 ? encodeInline(tokenizeIcu(trg), ids, "target") : null;
|
|
364
|
+
const state = options.states?.[key] ?? (trg !== void 0 ? "translated" : "initial");
|
|
365
|
+
if (!XLIFF_STATES.includes(state)) throw new I18nToolsError("invalid-state", `estado no v\xE1lido \xAB${state}\xBB para ${key}`);
|
|
366
|
+
out.push(` <unit id="${escapeXmlAttr(key)}"${dnt.has(key) ? ' translate="no"' : ""} xml:space="preserve">`);
|
|
367
|
+
const notes = [];
|
|
368
|
+
const ctx = options.contexts?.[key];
|
|
369
|
+
if (ctx) notes.push(`<note category="context">${encodeText(ctx)}</note>`);
|
|
370
|
+
for (const note of options.notes?.[key] ?? []) notes.push(`<note>${encodeText(note)}</note>`);
|
|
371
|
+
if (notes.length > 0) out.push(` <notes>${notes.join("")}</notes>`);
|
|
372
|
+
if (ids.data.length > 0) out.push(` <originalData>${ids.data.map((d) => `<data id="${d.id}">${encodeText(d.raw)}</data>`).join("")}</originalData>`);
|
|
373
|
+
out.push(` <segment state="${state}" canResegment="no">`);
|
|
374
|
+
out.push(` <source>${srcXml}</source>`);
|
|
375
|
+
if (trgXml !== null) out.push(` <target>${trgXml}</target>`);
|
|
376
|
+
out.push(" </segment>");
|
|
377
|
+
out.push(" </unit>");
|
|
378
|
+
}
|
|
379
|
+
out.push(" </file>", "</xliff>", "");
|
|
380
|
+
return out.join("\n");
|
|
381
|
+
}
|
|
382
|
+
var inXliff = (el, local) => el.local === local && (el.ns === XLIFF_NS || el.ns === XLIFF_NS_20);
|
|
383
|
+
var kids = (el, local) => el.children.filter((c) => typeof c !== "string" && inXliff(c, local));
|
|
384
|
+
function decodeContent(el, data, what) {
|
|
385
|
+
let out = "";
|
|
386
|
+
for (const c of el.children) {
|
|
387
|
+
if (typeof c === "string") {
|
|
388
|
+
out += c;
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
if (c.ns !== XLIFF_NS && c.ns !== XLIFF_NS_20) continue;
|
|
392
|
+
if (c.local === "cp") {
|
|
393
|
+
const hex = c.attrs["hex"] ?? "";
|
|
394
|
+
if (!/^[0-9A-Fa-f]{4,6}$/.test(hex)) throw new I18nToolsError("invalid-xliff", `<cp hex> no v\xE1lido en ${what}`);
|
|
395
|
+
const cp = Number.parseInt(hex, 16);
|
|
396
|
+
if (cp > 1114111) throw new I18nToolsError("invalid-xliff", `<cp hex> fuera de rango en ${what}`);
|
|
397
|
+
out += String.fromCodePoint(cp);
|
|
398
|
+
} else if (c.local === "ph") {
|
|
399
|
+
if (c.children.length > 0) throw new I18nToolsError("invalid-xliff", `<ph> debe ser un elemento vac\xEDo (${what})`);
|
|
400
|
+
const ref = c.attrs["dataRef"];
|
|
401
|
+
if (ref !== void 0) {
|
|
402
|
+
const raw = data.get(ref);
|
|
403
|
+
if (raw === void 0) throw new I18nToolsError("invalid-xliff", `dataRef \xAB${ref}\xBB sin <data> en ${what}`);
|
|
404
|
+
out += raw;
|
|
405
|
+
} else if (c.attrs["equiv"] !== void 0) out += c.attrs["equiv"];
|
|
406
|
+
else throw new I18nToolsError("invalid-xliff", `<ph> sin dataRef ni equiv en ${what}`);
|
|
407
|
+
} else throw new I18nToolsError("unsupported-inline", `elemento en l\xEDnea <${c.local}> no admitido en ${what} (solo <ph> y <cp>)`);
|
|
408
|
+
}
|
|
409
|
+
return out;
|
|
410
|
+
}
|
|
411
|
+
function importXliff(input, options = {}) {
|
|
412
|
+
const root = parseXml(input, options.limits);
|
|
413
|
+
if (!inXliff(root, "xliff")) throw new I18nToolsError("invalid-xliff", "la ra\xEDz no es <xliff> de XLIFF 2.x");
|
|
414
|
+
const version = root.attrs["version"];
|
|
415
|
+
if (version !== "2.1" && version !== "2.0") throw new I18nToolsError("unsupported-version", `solo se admite XLIFF 2.0/2.1 (version=${version ?? "ausente"})`);
|
|
416
|
+
const srcLang = root.attrs["srcLang"];
|
|
417
|
+
if (!srcLang || !CATALOG_LOCALE.test(srcLang)) throw new I18nToolsError("invalid-locale", `srcLang no v\xE1lido \xAB${srcLang ?? ""}\xBB`);
|
|
418
|
+
const trgLang = root.attrs["trgLang"] ?? null;
|
|
419
|
+
if (trgLang !== null && !CATALOG_LOCALE.test(trgLang)) throw new I18nToolsError("invalid-locale", `trgLang no v\xE1lido \xAB${trgLang}\xBB`);
|
|
420
|
+
const files = kids(root, "file");
|
|
421
|
+
if (files.length !== 1) throw new I18nToolsError("invalid-xliff", `se espera exactamente un <file> (hay ${files.length})`);
|
|
422
|
+
const file = files[0];
|
|
423
|
+
const original = file.attrs["original"];
|
|
424
|
+
const namespace = original && CATALOG_NAMESPACE.test(original) ? original : options.namespace;
|
|
425
|
+
if (!namespace || !CATALOG_NAMESPACE.test(namespace)) throw new I18nToolsError("invalid-input", "falta un espacio de nombres v\xE1lido (file/@original o options.namespace)");
|
|
426
|
+
let syntax = "icu1";
|
|
427
|
+
for (const notes of kids(file, "notes")) {
|
|
428
|
+
for (const note of kids(notes, "note")) {
|
|
429
|
+
if (note.attrs["category"] === SYNTAX_NOTE) {
|
|
430
|
+
const value = elementText(note).trim();
|
|
431
|
+
if (value !== "icu1" && value !== "mf2") throw new I18nToolsError("invalid-xliff", `sintaxis desconocida \xAB${value}\xBB`);
|
|
432
|
+
syntax = value;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
const strict = options.strictKeys !== false;
|
|
437
|
+
const units = [];
|
|
438
|
+
const seen = /* @__PURE__ */ new Set();
|
|
439
|
+
const elementChildren = (el) => el.children.filter((c) => typeof c !== "string");
|
|
440
|
+
const order = [];
|
|
441
|
+
const walk = elementChildren(file).reverse();
|
|
442
|
+
while (walk.length > 0) {
|
|
443
|
+
const c = walk.pop();
|
|
444
|
+
if (inXliff(c, "unit")) order.push(c);
|
|
445
|
+
else if (inXliff(c, "group")) walk.push(...elementChildren(c).reverse());
|
|
446
|
+
}
|
|
447
|
+
for (const u of order) {
|
|
448
|
+
const key = u.attrs["id"];
|
|
449
|
+
if (!key) throw new I18nToolsError("invalid-xliff", "<unit> sin id");
|
|
450
|
+
if (strict && !CATALOG_KEY.test(key)) throw new I18nToolsError("invalid-key", `clave no v\xE1lida \xAB${key}\xBB`);
|
|
451
|
+
if (seen.has(key)) throw new I18nToolsError("duplicate-key", `clave repetida \xAB${key}\xBB`);
|
|
452
|
+
seen.add(key);
|
|
453
|
+
const data = /* @__PURE__ */ new Map();
|
|
454
|
+
for (const od of kids(u, "originalData")) {
|
|
455
|
+
for (const d of kids(od, "data")) {
|
|
456
|
+
const id = d.attrs["id"];
|
|
457
|
+
if (!id) throw new I18nToolsError("invalid-xliff", `<data> sin id en ${key}`);
|
|
458
|
+
data.set(id, decodeContent(d, /* @__PURE__ */ new Map(), `${key}/data`));
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
let context = null;
|
|
462
|
+
const notes = [];
|
|
463
|
+
for (const group of kids(u, "notes")) {
|
|
464
|
+
for (const note of kids(group, "note")) {
|
|
465
|
+
const text = decodeContent(note, /* @__PURE__ */ new Map(), `${key}/note`);
|
|
466
|
+
if (note.attrs["category"] === "context" && context === null) context = text;
|
|
467
|
+
else notes.push(text);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
let source = "";
|
|
471
|
+
let target = null;
|
|
472
|
+
let hasSegment = false;
|
|
473
|
+
let state = "final";
|
|
474
|
+
let sawTarget = false;
|
|
475
|
+
for (const c of u.children) {
|
|
476
|
+
if (typeof c === "string" || !inXliff(c, "segment") && !inXliff(c, "ignorable")) continue;
|
|
477
|
+
const src = kids(c, "source")[0];
|
|
478
|
+
if (!src) throw new I18nToolsError("invalid-xliff", `<segment> sin <source> en ${key}`);
|
|
479
|
+
source += decodeContent(src, data, `${key}/source`);
|
|
480
|
+
const trg = kids(c, "target")[0];
|
|
481
|
+
if (trg) {
|
|
482
|
+
sawTarget = true;
|
|
483
|
+
target = (target ?? "") + decodeContent(trg, data, `${key}/target`);
|
|
484
|
+
}
|
|
485
|
+
if (inXliff(c, "segment")) {
|
|
486
|
+
hasSegment = true;
|
|
487
|
+
const st = c.attrs["state"] ?? "initial";
|
|
488
|
+
if (!XLIFF_STATES.includes(st)) throw new I18nToolsError("invalid-state", `estado no v\xE1lido \xAB${st}\xBB en ${key}`);
|
|
489
|
+
if (STATE_RANK[st] < STATE_RANK[state]) state = st;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (!hasSegment) throw new I18nToolsError("invalid-xliff", `<unit> sin <segment> en ${key}`);
|
|
493
|
+
if (!sawTarget || target === "" && state === "initial") target = null;
|
|
494
|
+
units.push({ key, source, target, state, context, notes, translate: u.attrs["translate"] !== "no" });
|
|
495
|
+
}
|
|
496
|
+
return { srcLang, trgLang, namespace, syntax, units };
|
|
497
|
+
}
|
|
498
|
+
function xliffToCatalogs(doc) {
|
|
499
|
+
const sourceMessages = {};
|
|
500
|
+
const targetMessages = {};
|
|
501
|
+
const states = {};
|
|
502
|
+
for (const u of doc.units) {
|
|
503
|
+
sourceMessages[u.key] = u.source;
|
|
504
|
+
states[u.key] = u.state;
|
|
505
|
+
if (u.target !== null) targetMessages[u.key] = u.target;
|
|
506
|
+
}
|
|
507
|
+
const source = { version: CATALOG_DOC_VERSION, namespace: doc.namespace, locale: doc.srcLang, syntax: doc.syntax, messages: sourceMessages };
|
|
508
|
+
const target = doc.trgLang ? { version: CATALOG_DOC_VERSION, namespace: doc.namespace, locale: doc.trgLang, syntax: doc.syntax, messages: targetMessages } : null;
|
|
509
|
+
return { source, target, states };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
export { XLIFF_NS, XLIFF_STATES, exportXliff, importXliff, xliffToCatalogs };
|