attranslate
Version:
Semi-automated Text Translator for Websites and Apps
133 lines (132 loc) • 4.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateYmlNodes = exports.extractYmlNodes = void 0;
const yaml_generic_1 = require("./yaml-generic");
const flatten_1 = require("../../util/flatten");
const parse_utils_1 = require("../common/parse-utils");
function extractYmlNodes(args, document) {
const tSet = new Map();
const rootContext = {
partialKey: "",
node: getRootNode(document, args),
oldTargetNode: null,
};
traverseYml(rootContext, (innerContext, scalar) => {
const value = scalar.value;
if (typeof value === "string") {
tSet.set(innerContext.partialKey, value);
}
});
return tSet;
}
exports.extractYmlNodes = extractYmlNodes;
function updateYmlNodes(args) {
const rootContext = {
partialKey: "",
node: getRootNode(args.sourceYml, args.args),
oldTargetNode: args.oldTargetYml
? getRootNode(args.oldTargetYml, args.args)
: null,
};
traverseYml(rootContext, (innerContext, scalar) => {
const value = args.args.tSet.get(innerContext.partialKey);
if (value !== undefined) {
scalar.value = value;
}
});
}
exports.updateYmlNodes = updateYmlNodes;
function getRootNode(document, args) {
const root = document.contents;
if (!root) {
(0, parse_utils_1.logParseError)("root node not found", args);
}
if (!(0, yaml_generic_1.isScalar)(root) && !(0, yaml_generic_1.isCollection)(root) && !(0, yaml_generic_1.isPair)(root)) {
(0, parse_utils_1.logParseError)("root node invalid", args);
}
return root;
}
function traverseYml(context, operation) {
var _a, _b, _c, _d;
const node = context.node;
if (!node) {
return;
}
if (((_a = context.oldTargetNode) === null || _a === void 0 ? void 0 : _a.type) !== node.type) {
context.oldTargetNode = null;
}
if ((_b = context.oldTargetNode) === null || _b === void 0 ? void 0 : _b.comment) {
node.comment = context.oldTargetNode.comment;
}
if ((_c = context.oldTargetNode) === null || _c === void 0 ? void 0 : _c.commentBefore) {
node.commentBefore = context.oldTargetNode.commentBefore;
}
if ((_d = context.oldTargetNode) === null || _d === void 0 ? void 0 : _d.spaceBefore) {
node.spaceBefore = context.oldTargetNode.spaceBefore;
}
// if (context.oldTargetNode?.cstNode) {
// node.cstNode = context.oldTargetNode.cstNode;
// }
if ((0, yaml_generic_1.isScalar)(node)) {
operation(context, node);
}
if ((0, yaml_generic_1.isPair)(node)) {
const pairKey = getPairKey(node);
let partialKey;
if (context.partialKey.length) {
partialKey = context.partialKey + flatten_1.NESTED_JSON_SEPARATOR + pairKey;
}
else {
partialKey = pairKey;
}
let oldTargetPair = null;
if ((0, yaml_generic_1.isPair)(context.oldTargetNode) &&
getPairKey(context.oldTargetNode) === pairKey) {
oldTargetPair = context.oldTargetNode;
}
traverseYml({
node: node.value,
oldTargetNode: oldTargetPair
? oldTargetPair.value
: null,
partialKey,
}, operation);
}
if ((0, yaml_generic_1.isCollection)(node)) {
node.items.forEach((childNode, idx) => {
let partialKey = `${context.partialKey}`;
if ((0, yaml_generic_1.isSequence)(node)) {
partialKey += `[${idx}]`;
}
let oldTargetChild = null;
if ((0, yaml_generic_1.isCollection)(context.oldTargetNode)) {
oldTargetChild = findMatchingOldTargetChild(context.oldTargetNode.items, childNode, idx);
}
traverseYml({
node: childNode,
oldTargetNode: oldTargetChild,
partialKey: partialKey,
}, operation);
});
}
}
function findMatchingOldTargetChild(oldTargetItems, child, idx) {
if (!(0, yaml_generic_1.isPair)(child)) {
if (idx >= oldTargetItems.length) {
return null;
}
return oldTargetItems[idx];
}
else {
const candidatePairs = oldTargetItems.filter((node) => (0, yaml_generic_1.isPair)(node));
const matchingPair = candidatePairs.find((pair) => getPairKey(pair) === getPairKey(child));
return matchingPair !== null && matchingPair !== void 0 ? matchingPair : null;
}
}
function getPairKey(pair) {
var _a;
if (typeof pair.key === "string") {
return pair.key;
}
return (_a = pair.key) === null || _a === void 0 ? void 0 : _a.value;
}