fix: add api challenges back (#24794)
This commit is contained in:
committed by
mrugesh mohapatra
parent
91cfdcfefc
commit
175d521c68
138
tools/translation/translate-guide-old.js
Normal file
138
tools/translation/translate-guide-old.js
Normal file
@@ -0,0 +1,138 @@
|
||||
const fs = require('fs-extra');
|
||||
const unified = require('unified');
|
||||
const parse = require('remark-parse');
|
||||
const parse2 = require('rehype-parse');
|
||||
const rehype = require('remark-rehype');
|
||||
const stringify = require('rehype-stringify');
|
||||
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 lang = 'es';
|
||||
const langFull = 'spanish';
|
||||
|
||||
const mdProcessor = unified()
|
||||
.use(parse)
|
||||
.use(rehype, { allowDangerousHTML: true })
|
||||
.use(stringify);
|
||||
|
||||
const htmlProcessor = unified()
|
||||
.use(parse2)
|
||||
.use(remark)
|
||||
.use(stringify2);
|
||||
|
||||
readDirP({
|
||||
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 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;
|
||||
fs.ensureDirSync(outputDir);
|
||||
|
||||
const fileString = fs.readFileSync(fullPath).toString();
|
||||
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, ' ')
|
||||
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, ` `)
|
||||
|
||||
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 translateText = createTranslateText(lang);
|
||||
|
||||
|
||||
function writeFile(fileString, 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);
|
||||
})
|
||||
);
|
||||
};
|
102
tools/translation/translate-guide.js
Normal file
102
tools/translation/translate-guide.js
Normal file
@@ -0,0 +1,102 @@
|
||||
const fs = require('fs-extra');
|
||||
var showdown = require('showdown');
|
||||
const path = require('path');
|
||||
const readDirP = require('readdirp-walk');
|
||||
const {
|
||||
Translate
|
||||
} = require('@google-cloud/translate');
|
||||
|
||||
var TurndownService = require('turndown')
|
||||
|
||||
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);
|
||||
|
||||
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 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;
|
||||
fs.ensureDirSync(outputDir);
|
||||
|
||||
const fileString = fs.readFileSync(fullPath).toString();
|
||||
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 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;
|
||||
|
||||
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 translateText = createTranslateText(lang);
|
||||
|
||||
|
||||
function writeFile(fileString, outputPath) {
|
||||
fs.writeFile(outputPath, fileString, function(err) {
|
||||
if (err) throw err;
|
||||
console.log('Saved:' + outputPath);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user