chore: Apply linting fixes
This commit is contained in:
parent
5499042ce6
commit
f45c3d4fa5
@ -45,7 +45,7 @@ function textToData(sectionIds) {
|
||||
const lines = child.value.split('\n');
|
||||
if (lines.filter(Boolean).length > 0) {
|
||||
lines.forEach((line, index) => {
|
||||
if (/^\s*$/.test(line)) {
|
||||
if ((/^\s*$/).test(line)) {
|
||||
currentParagraph = null;
|
||||
} else {
|
||||
if (!currentParagraph || index > 0) {
|
||||
|
@ -5,7 +5,7 @@ const { createRedirects } = require('./createRedirects');
|
||||
const testLocations = {
|
||||
api: 'https://api.example.com',
|
||||
news: 'https://news.example.com',
|
||||
forum: 'https://forum.example.com',
|
||||
forum: 'https://forum.example.com'
|
||||
};
|
||||
|
||||
describe('createRedirects', () => {
|
||||
|
@ -53,6 +53,7 @@ fs.access(migrationMapPath, err => {
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
// eslint-disable-next-line
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
@ -25,5 +25,6 @@ getChallengesForLang('english')
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
// eslint-disable-next-line
|
||||
process.exit(1);
|
||||
});
|
||||
|
@ -5,7 +5,7 @@ const ObjectId = require('mongodb').ObjectID;
|
||||
const debug = require('debug');
|
||||
|
||||
const log = debug('fcc:tools:seedLocalAuthUser');
|
||||
const { MONGOHQ_URL, LOCALE: lang } = process.env;
|
||||
const { MONGOHQ_URL } = process.env;
|
||||
|
||||
function handleError(err, client) {
|
||||
if (err) {
|
||||
|
@ -9,7 +9,14 @@ var dir = dir1 + '/' + dir2;
|
||||
fs.readdirSync('../../curriculum/challenges/english/' + dir).forEach(file => {
|
||||
if (file.includes('.md') && dir) {
|
||||
let originalFileName =
|
||||
'../../curriculum/challenges/' + langFull + '/' + dir + '/' + file.slice(0, -10) + langFull + '.md';
|
||||
'../../curriculum/challenges/' +
|
||||
langFull +
|
||||
'/' +
|
||||
dir +
|
||||
'/' +
|
||||
file.slice(0, -10) +
|
||||
langFull +
|
||||
'.md';
|
||||
|
||||
fs.exists(originalFileName, function(exists) {
|
||||
if (!exists) {
|
||||
@ -28,10 +35,12 @@ fs.readdirSync('../../curriculum/challenges/english/' + dir).forEach(file => {
|
||||
|
||||
// Load in full text, description, instructions, and title
|
||||
function getFile(file, dir) {
|
||||
let originalFileName = '../../curriculum/challenges/english/' + dir + '/' + file;
|
||||
let originalFileName =
|
||||
'../../curriculum/challenges/english/' + dir + '/' + file;
|
||||
let fileString = fs.readFileSync(originalFileName).toString();
|
||||
|
||||
// Add 'notranslate' class to code so Google Translate API will not translate code segments.
|
||||
// Add 'notranslate' class to code so Google Translate API
|
||||
// will not translate code segments.
|
||||
fileString = fileString.replace(/<code>/g, '<code class="notranslate">');
|
||||
fileString = fileString.replace(
|
||||
/<blockquote>/g,
|
||||
@ -67,7 +76,7 @@ function processFile(
|
||||
) {
|
||||
const translateText = (text, target) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (typeof text == 'object' && Object.keys(text).length === 0) {
|
||||
if (typeof text === 'object' && Object.keys(text).length === 0) {
|
||||
resolve(['']);
|
||||
} else {
|
||||
// Imports the Google Cloud client library
|
||||
@ -87,8 +96,6 @@ function processFile(
|
||||
})
|
||||
.catch(err => {
|
||||
reject(console.log('!!!!!', err));
|
||||
if (err) {
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -106,21 +113,23 @@ function processFile(
|
||||
let testsArray = tests.split('\n');
|
||||
let testsToTranslate = [];
|
||||
|
||||
testsArray.forEach((test, index) => {
|
||||
testsArray.forEach(test => {
|
||||
if (test.includes('- text: ')) {
|
||||
testsToTranslate.push(test.slice(10));
|
||||
}
|
||||
});
|
||||
translateText(testsToTranslate, lang).then(translation => {
|
||||
let transIndex = 0;
|
||||
testsArray.forEach((test, index) => {
|
||||
if (test.includes('- text')) {
|
||||
testsArray[index] = ' - text: ' + translation[transIndex];
|
||||
transIndex++;
|
||||
}
|
||||
});
|
||||
resolve(testsArray.join('\n'));
|
||||
});
|
||||
translateText(testsToTranslate, lang)
|
||||
.then(translation => {
|
||||
let transIndex = 0;
|
||||
testsArray.forEach((test, index) => {
|
||||
if (test.includes('- text')) {
|
||||
testsArray[index] = ' - text: ' + translation[transIndex];
|
||||
transIndex++;
|
||||
}
|
||||
});
|
||||
resolve(testsArray.join('\n'));
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
||||
|
||||
@ -147,7 +156,7 @@ function processFile(
|
||||
/<section id='tests'>(.|\n)*?<\/section>/,
|
||||
translations[3]
|
||||
);
|
||||
fileString = fileString.replace(/ class=\"notranslate\"/g, ''); // remove 'notranslate' class
|
||||
fileString = fileString.replace(/ class=\"notranslate\"/g, '');
|
||||
writeFile(fileString, file, dir);
|
||||
});
|
||||
}
|
||||
@ -163,7 +172,9 @@ function writeFile(fileString, file, dir) {
|
||||
langFull +
|
||||
'.md';
|
||||
fs.writeFile(fullFileName, fileString, function(err) {
|
||||
if (err) throw err;
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
console.log('Saved!');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -8,10 +8,7 @@ const stringify2 = require('remark-stringify');
|
||||
const remark = require('rehype-remark');
|
||||
const path = require('path');
|
||||
const readDirP = require('readdirp-walk');
|
||||
const {
|
||||
Translate
|
||||
} = require('@google-cloud/translate');
|
||||
|
||||
const { Translate } = require('@google-cloud/translate');
|
||||
|
||||
const lang = 'es';
|
||||
const langFull = 'spanish';
|
||||
@ -27,112 +24,123 @@ const htmlProcessor = unified()
|
||||
.use(stringify2);
|
||||
|
||||
readDirP({
|
||||
root: path.resolve(__dirname, `./test`)
|
||||
})
|
||||
.on('data', translateChallenge);
|
||||
root: path.resolve(__dirname, './test')
|
||||
}).on('data', translateChallenge);
|
||||
|
||||
async function translateChallenge(file) {
|
||||
const {
|
||||
name,
|
||||
depth,
|
||||
path: filePath,
|
||||
fullPath,
|
||||
fullParentDir,
|
||||
stat
|
||||
} = file;
|
||||
if (stat.isDirectory() || name === '.DS_Store' || file.depth == 1) return null;
|
||||
const { name, fullPath, fullParentDir, stat } = file;
|
||||
if (stat.isDirectory() || name === '.DS_Store' || file.depth === 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
const pathIndex = fullPath.indexOf('guide') + 6;
|
||||
const outputDir = fullParentDir.substring(0, pathIndex) + `${langFull}/` + fullParentDir.substring(pathIndex + 5);
|
||||
const outputPath = fullPath.substring(0, pathIndex) + `${langFull}/` + fullPath.substring(pathIndex + 5);
|
||||
if (fs.existsSync(outputPath)) return null;
|
||||
const outputDir =
|
||||
fullParentDir.substring(0, pathIndex) +
|
||||
`${langFull}/` +
|
||||
fullParentDir.substring(pathIndex + 5);
|
||||
const outputPath =
|
||||
fullPath.substring(0, pathIndex) +
|
||||
`${langFull}/` +
|
||||
fullPath.substring(pathIndex + 5);
|
||||
if (fs.existsSync(outputPath)) {
|
||||
return null;
|
||||
}
|
||||
fs.ensureDirSync(outputDir);
|
||||
|
||||
const fileString = fs.readFileSync(fullPath).toString();
|
||||
var i = fileString.indexOf('---', 4)
|
||||
const meta = fileString.substring(0, i+4)
|
||||
var i = fileString.indexOf('---', 4);
|
||||
const meta = fileString.substring(0, i + 4);
|
||||
const title = fileString.split('\n')[1].split(': ')[1];
|
||||
var article = fileString.substring(i+4)
|
||||
|
||||
mdToHtml(article).then((htmlArticle) => {
|
||||
htmlArticle = htmlArticle.replace(/\n/g, '<br>')
|
||||
htmlArticle = htmlArticle.replace(/ /g, ' ')
|
||||
htmlArticle = htmlArticle.replace(/ /g, ' ')
|
||||
htmlArticle = htmlArticle.replace(/ /g, ' ')
|
||||
var article = fileString.substring(i + 4);
|
||||
|
||||
return mdToHtml(article).then(htmlArticle => {
|
||||
htmlArticle = htmlArticle.replace(/\n/g, '<br>');
|
||||
htmlArticle = htmlArticle.replace(
|
||||
/ {8}/g,
|
||||
' '
|
||||
);
|
||||
htmlArticle = htmlArticle.replace(/ {4}/g, ' ');
|
||||
htmlArticle = htmlArticle.replace(/ {2}/g, ' ');
|
||||
translate(htmlArticle, title, meta, outputPath);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function translate(htmlArticle, title, meta, outputPath) {
|
||||
Promise.all([
|
||||
translateText(title),
|
||||
translateText(htmlArticle)
|
||||
]).then(function(translations) {
|
||||
// Replace English with translation
|
||||
let translatedTitle = translations[0][0];
|
||||
let tempArticle = translations[1][0]
|
||||
tempArticle = tempArticle.replace(/<br>/g, '\n')
|
||||
tempArticle = tempArticle.replace(/'/g, `'`)
|
||||
|
||||
// tempArticle = tempArticle.replace(/language-html">/g, 'language-html">\n')
|
||||
// tempArticle = tempArticle.replace(/<pre> <code/g, '<pre><code')
|
||||
// tempArticle = tempArticle.replace(/<\/code> <\/pre/g, '</code></pre')
|
||||
tempArticle = tempArticle.replace(/ /g, ` `)
|
||||
Promise.all([translateText(title), translateText(htmlArticle)]).then(function(
|
||||
translations
|
||||
) {
|
||||
// Replace English with translation
|
||||
let translatedTitle = translations[0][0];
|
||||
let tempArticle = translations[1][0];
|
||||
tempArticle = tempArticle.replace(/<br>/g, '\n');
|
||||
tempArticle = tempArticle.replace(/'/g, "'");
|
||||
|
||||
htmlToMd(tempArticle).then((translatedArticle) => {
|
||||
const i = meta.indexOf('---', 4);
|
||||
let translatedFile = meta.slice(0, i) + `localeTitle: ${translatedTitle}\n` + meta.slice(i) + translatedArticle;
|
||||
writeFile(translatedFile, outputPath);
|
||||
});
|
||||
});
|
||||
// tempArticle = tempArticle.replace(/language-html">/g,'language-html">\n')
|
||||
// tempArticle = tempArticle.replace(/<pre> <code/g, '<pre><code')
|
||||
// tempArticle = tempArticle.replace(/<\/code> <\/pre/g, '</code></pre')
|
||||
tempArticle = tempArticle.replace(/ /g, ' ');
|
||||
|
||||
htmlToMd(tempArticle).then(translatedArticle => {
|
||||
const i = meta.indexOf('---', 4);
|
||||
let translatedFile =
|
||||
meta.slice(0, i) +
|
||||
`localeTitle: ${translatedTitle}\n` +
|
||||
meta.slice(i) +
|
||||
translatedArticle;
|
||||
writeFile(translatedFile, outputPath);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const createTranslateText = target => (text) => {
|
||||
if (!text) return '';
|
||||
const translate = new Translate();
|
||||
|
||||
return translate
|
||||
.translate(text, target)
|
||||
.then(results => {
|
||||
let translations = results[0];
|
||||
translations = Array.isArray(translations) ?
|
||||
translations : [translations];
|
||||
return translations;
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
const createTranslateText = target => text => {
|
||||
if (!text) {
|
||||
return '';
|
||||
}
|
||||
const translate = new Translate();
|
||||
|
||||
return translate
|
||||
.translate(text, target)
|
||||
.then(results => {
|
||||
let translations = results[0];
|
||||
translations = Array.isArray(translations)
|
||||
? translations
|
||||
: [translations];
|
||||
return translations;
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
};
|
||||
|
||||
const translateText = createTranslateText(lang);
|
||||
|
||||
|
||||
function writeFile(fileString, outputPath) {
|
||||
fs.writeFile(outputPath, fileString, function(err) {
|
||||
if (err) throw err;
|
||||
console.log('Saved:' + outputPath);
|
||||
});
|
||||
fs.writeFile(outputPath, fileString, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
console.log('Saved:' + outputPath);
|
||||
});
|
||||
}
|
||||
|
||||
function mdToHtml(file) {
|
||||
return new Promise((resolve, reject) =>
|
||||
mdProcessor.process(file, function(err, file) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
return resolve(file.contents);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
function htmlToMd(file) {
|
||||
return new Promise((resolve, reject) =>
|
||||
htmlProcessor.process(file, function(err, file) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
return resolve(file.contents);
|
||||
})
|
||||
);
|
||||
};
|
||||
return new Promise((resolve, reject) =>
|
||||
mdProcessor.process(file, function(err, file) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
return resolve(file.contents);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function htmlToMd(file) {
|
||||
return new Promise((resolve, reject) =>
|
||||
htmlProcessor.process(file, function(err, file) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
return resolve(file.contents);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -1,102 +1,114 @@
|
||||
const fs = require('fs-extra');
|
||||
var showdown = require('showdown');
|
||||
var showdown = require('showdown');
|
||||
const path = require('path');
|
||||
const readDirP = require('readdirp-walk');
|
||||
const {
|
||||
Translate
|
||||
} = require('@google-cloud/translate');
|
||||
const { Translate } = require('@google-cloud/translate');
|
||||
|
||||
var TurndownService = require('turndown')
|
||||
var TurndownService = require('turndown');
|
||||
|
||||
var turndownService = new TurndownService({'codeBlockStyle': 'fenced', 'headingStyle': 'atx'})
|
||||
var turndownService = new TurndownService({
|
||||
codeBlockStyle: 'fenced',
|
||||
headingStyle: 'atx'
|
||||
});
|
||||
|
||||
const converter = new showdown.Converter();
|
||||
|
||||
const lang = 'pt';
|
||||
const langFull = 'portuguese';
|
||||
|
||||
|
||||
readDirP({
|
||||
root: path.resolve(__dirname, `./english`)
|
||||
})
|
||||
.on('data', translateChallenge);
|
||||
root: path.resolve(__dirname, './english')
|
||||
}).on('data', translateChallenge);
|
||||
|
||||
async function translateChallenge(file) {
|
||||
const {
|
||||
name,
|
||||
depth,
|
||||
path: filePath,
|
||||
fullPath,
|
||||
fullParentDir,
|
||||
stat
|
||||
} = file;
|
||||
if (stat.isDirectory() || name === '.DS_Store' || file.depth == 1) return null;
|
||||
const { name, fullPath, fullParentDir, stat } = file;
|
||||
if (stat.isDirectory() || name === '.DS_Store' || file.depth === 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
const pathIndex = fullPath.indexOf('guide') + 6;
|
||||
const outputDir = fullParentDir.substring(0, pathIndex) + `${langFull}/` + fullParentDir.substring(pathIndex + 8);
|
||||
const outputPath = fullPath.substring(0, pathIndex) + `${langFull}/` + fullPath.substring(pathIndex + 8);
|
||||
if (fs.existsSync(outputPath)) return null;
|
||||
const outputDir =
|
||||
fullParentDir.substring(0, pathIndex) +
|
||||
`${langFull}/` +
|
||||
fullParentDir.substring(pathIndex + 8);
|
||||
const outputPath =
|
||||
fullPath.substring(0, pathIndex) +
|
||||
`${langFull}/` +
|
||||
fullPath.substring(pathIndex + 8);
|
||||
if (fs.existsSync(outputPath)) {
|
||||
return null;
|
||||
}
|
||||
fs.ensureDirSync(outputDir);
|
||||
|
||||
const fileString = fs.readFileSync(fullPath).toString();
|
||||
var i = fileString.indexOf('---', 4)
|
||||
const meta = fileString.substring(0, i+4)
|
||||
var i = fileString.indexOf('---', 4);
|
||||
const meta = fileString.substring(0, i + 4);
|
||||
const title = fileString.split('\n')[1].split(': ')[1];
|
||||
var article = fileString.substring(i+4)
|
||||
|
||||
var article = fileString.substring(i + 4);
|
||||
|
||||
var htmlArticle = converter.makeHtml(article);
|
||||
htmlArticle = htmlArticle.replace(/\n/g, '<br>')
|
||||
htmlArticle = htmlArticle.replace(/ /g, ' ')
|
||||
htmlArticle = htmlArticle.replace(/ /g, ' ')
|
||||
htmlArticle = htmlArticle.replace(/ /g, ' ')
|
||||
Promise.all([
|
||||
translateText(title),
|
||||
translateText(htmlArticle)
|
||||
]).then(function(translations) {
|
||||
// Replace English with translation
|
||||
let translatedTitle = translations[0][0];
|
||||
let tempArticle = translations[1][0]
|
||||
tempArticle = tempArticle.replace(/<br>/g, '\n')
|
||||
tempArticle = tempArticle.replace(/'/g, `'`)
|
||||
|
||||
tempArticle = tempArticle.replace(/language-html">/g, 'language-html">\n')
|
||||
tempArticle = tempArticle.replace(/<pre> <code/g, '<pre><code')
|
||||
tempArticle = tempArticle.replace(/<\/pre> <\/code/g, '</pre></code')
|
||||
tempArticle = tempArticle.replace(/ /g, ` `)
|
||||
let translatedArticle = turndownService.turndown(tempArticle);
|
||||
translatedArticle = translatedArticle.replace(/\n\n\`\`\`\n/g, '\n\`\`\`\n')
|
||||
let translatedFile = meta.slice(0, i) + `localeTitle: ${translatedTitle}\n` + meta.slice(i) + translatedArticle;
|
||||
htmlArticle = htmlArticle.replace(/\n/g, '<br>');
|
||||
htmlArticle = htmlArticle.replace(
|
||||
/ {8}/g,
|
||||
' '
|
||||
);
|
||||
htmlArticle = htmlArticle.replace(/ {4}/g, ' ');
|
||||
htmlArticle = htmlArticle.replace(/ {2}/g, ' ');
|
||||
return Promise.all([translateText(title), translateText(htmlArticle)]).then(
|
||||
function(translations) {
|
||||
// Replace English with translation
|
||||
let translatedTitle = translations[0][0];
|
||||
let tempArticle = translations[1][0];
|
||||
tempArticle = tempArticle.replace(/<br>/g, '\n');
|
||||
tempArticle = tempArticle.replace(/'/g, "'");
|
||||
|
||||
writeFile(translatedFile, outputPath);
|
||||
});
|
||||
tempArticle = tempArticle.replace(
|
||||
/language-html">/g,
|
||||
'language-html">\n'
|
||||
);
|
||||
tempArticle = tempArticle.replace(/<pre> <code/g, '<pre><code');
|
||||
tempArticle = tempArticle.replace(/<\/pre> <\/code/g, '</pre></code');
|
||||
tempArticle = tempArticle.replace(/ /g, ' ');
|
||||
let translatedArticle = turndownService.turndown(tempArticle);
|
||||
translatedArticle = translatedArticle.replace(/\n\n\`\`\`\n/g, '\n```\n');
|
||||
let translatedFile =
|
||||
meta.slice(0, i) +
|
||||
`localeTitle: ${translatedTitle}\n` +
|
||||
meta.slice(i) +
|
||||
translatedArticle;
|
||||
|
||||
writeFile(translatedFile, outputPath);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const createTranslateText = target => text => {
|
||||
if (!text) {
|
||||
return '';
|
||||
}
|
||||
const translate = new Translate();
|
||||
|
||||
return translate
|
||||
.translate(text, target)
|
||||
.then(results => {
|
||||
let translations = results[0];
|
||||
translations = Array.isArray(translations)
|
||||
? translations
|
||||
: [translations];
|
||||
return translations;
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
};
|
||||
|
||||
const createTranslateText = target => (text) => {
|
||||
if (!text) return '';
|
||||
const translate = new Translate();
|
||||
|
||||
return translate
|
||||
.translate(text, target)
|
||||
.then(results => {
|
||||
let translations = results[0];
|
||||
translations = Array.isArray(translations) ?
|
||||
translations : [translations];
|
||||
return translations;
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
const translateText = createTranslateText(lang);
|
||||
|
||||
|
||||
function writeFile(fileString, outputPath) {
|
||||
fs.writeFile(outputPath, fileString, function(err) {
|
||||
if (err) throw err;
|
||||
console.log('Saved:' + outputPath);
|
||||
});
|
||||
}
|
||||
fs.writeFile(outputPath, fileString, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
console.log('Saved:' + outputPath);
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user