fix(tools): update translation parser

Since we're adding more validation we can simplify the parser and
make sure it does catch all the comments.  Rather than worry about a
load of edge cases that do not appear in our challenges.
This commit is contained in:
Oliver Eyton-Williams
2020-09-28 15:13:18 +02:00
committed by Mrugesh Mohapatra
parent 793fa8fb52
commit 92a60f8bce
3 changed files with 63 additions and 39 deletions

View File

@@ -6,11 +6,10 @@ exports.translateComments = (text, lang, dict, codeLang) => {
const config = { knownComments, dict, lang };
switch (codeLang) {
case 'js':
return transMultiline(transInline(text, config), config);
case 'jsx':
return transJSX(text, config);
return transMultiline(transInline(text, config), config);
case 'html':
return transHTML(transCSS(text, config), config);
return transScript(transHTML(transCSS(text, config), config), config);
default:
return text;
}
@@ -18,18 +17,20 @@ exports.translateComments = (text, lang, dict, codeLang) => {
exports.translateCommentsInChallenge = (challenge, lang, dict) => {
const challClone = clone(challenge);
Object.keys(challClone.files).forEach(key => {
if (challClone.files[key].contents) {
challClone.files[key].contents = this.translateComments(
challenge.files[key].contents,
lang,
dict,
challClone.files[key].ext
);
}
});
if (!challClone.files) {
console.warn(`Challenge ${challClone.title} has no comments to translate`);
} else {
Object.keys(challClone.files).forEach(key => {
if (challClone.files[key].contents) {
challClone.files[key].contents = this.translateComments(
challenge.files[key].contents,
lang,
dict,
challClone.files[key].ext
);
}
});
}
return challClone;
};
@@ -93,12 +94,7 @@ exports.mergeChallenges = (engChal, transChal) => {
// bare urls could be interpreted as comments, so we have to lookbehind for
// http:// or https://
function transInline(text, config) {
return translateGeneric(
text,
config,
'(^[^\'"`]*?(?<!https?:)//\\s*)',
'(\\s*$)'
);
return translateGeneric(text, config, '((?<!https?:)//\\s*)', '(\\s*$)');
}
function transMultiline(text, config) {
@@ -116,8 +112,17 @@ function transCSS(text, config) {
return text;
}
function transJSX(text, config) {
return translateGeneric(text, config, '({[^}]*/\\*\\s*)', '(\\s*\\*/[^{]*})');
function transScript(text, config) {
const regex = /<script>.*?<\/script>/gms;
const matches = text.matchAll(regex);
for (const [match] of matches) {
text = text.replace(
match,
transMultiline(transInline(match, config), config)
);
}
return text;
}
function transHTML(text, config) {