better readme, CLI

This commit is contained in:
Pierre
2023-12-02 20:50:26 +01:00
parent 0af8fa25d4
commit cdc0b7d00a
4 changed files with 67 additions and 20 deletions
+50
View File
@@ -0,0 +1,50 @@
# autotranslate doc
A tool that automatically transalte the doc (every .md files) in a github repo using OPEN AI GPT 3.5
*** Disclaimer: This tool is still experiemental ***
## Usage
First install
```
npm install -g autotranslatedoc
```
Then make sure you set up the following environment variables:
```GITHUB_PERSONAL_ACCESS_TOKEN``` and ```OPENAI_API_KEY```
Then translate the doc of a target repo in french and spanish
```
autotranslatedoc translate run-llama llama_index -l fr,es
```
This will create a llama_index.json file it the run-lama directory of the specified output directory (default=./save)
```
ls ./save
run-llama
llama_index.json
```
This save file contain all the translation and can be used to generate the do to a target format (currently only .md)
```
autotranslatedoc build run-llama llama_index -l fr,es
```
This will generate a fr and es repository in the buildPath (default ./build) that will contain the translated doc files
```
autotranslatedoc update run-llama llama_index -l fr,es
```
This will look at pre-existing translation in the existing doc save file and will perform a differential translation of every file that have been updated.
```
autotranslatedoc serve run-llama llama_index
```
Will launch a server allowing the easy review of the doc on port 3000 or env.PORT. EXPERIMENTAL
+4 -7
View File
@@ -39,10 +39,6 @@ const program = commander
.parse(process.argv);
const options = program.opts();
console.log(options);
if (options.githubtoken) {
process.env.GITHUB_PERSONAL_ACCESS_TOKEN = options.githubtoken;
@@ -53,10 +49,11 @@ if (options.openaikey) {
async function run() {
const translate = require('./translate');
switch (program.processedArgs[0]) {
case 'translate':
const translate = require('./translate');
let loadFile = true;
for (let langCode of options.language) {
@@ -76,7 +73,6 @@ async function run() {
break;
case 'update':
const translate = require('./translate');
for (let langCode of options.language) {
console.log("Updating translation in " + langCode);
@@ -97,7 +93,8 @@ async function run() {
require('./serve');
break;
case 'build':
for (let langCode of options.language) {
for (let langCode of options.language) {
console.log("Building translation Md " + langCode + " to " + options.outputPath);
translate.buildDoc(
program.args[1],
program.args[2],
+2 -2
View File
@@ -6,6 +6,6 @@ const app = express();
app.use(express.static('public'));
app.use(express.static('save'));
app.listen(process.env.PORT || 3000, (port) => {
console.log(`Example app listening on port ${port}!`);
app.listen(process.env.PORT || 3000, () => {
console.log(`Example app listening on port ${process.env.PORT || 3000}!`);
});
+11 -11
View File
@@ -105,7 +105,6 @@ function parseMdStrToTree(file) {
// a function that take a document hierarchical object and return a md file as a string
function parseTreeToMdStr(doc, code='') {
let str = '';
console.log(JSON.stringify(doc, null, 2));
for (let block of doc) {
if (code) {
str += '#'.repeat(block.level) + ' ' + block[`title_${code}`][0] + '\n';
@@ -306,8 +305,8 @@ async function correctLinkInFile(file, languageCode, docDir) {
continue;
}
if (matchesNonTranslated?.length) {
if (matchesNonTranslated?.length && matchesTranslated?.length) {
for (let [index, nonTranslatedMatch] of matchesNonTranslated.entries()) {
let translatedMatch = matchesTranslated[index];
@@ -327,9 +326,9 @@ async function correctLinkInFile(file, languageCode, docDir) {
urlText = urlText.substring(1, urlText.length - 1);
urlText = `[${urlText}]`;
console.log('Replace by', urlText + newUrl, 'match', translatedMatch);
// console.log('Replace by', urlText + newUrl, 'match', translatedMatch);
console.log( block[`content_${languageCode}`])
// console.log( block[`content_${languageCode}`])
block[`content_${languageCode}`][0] = block[`content_${languageCode}`][0].replace(translatedMatch, urlText + newUrl);
@@ -342,8 +341,8 @@ async function correctLinkInFile(file, languageCode, docDir) {
}
async function buildOutputMd(files, languageCode, targetDir) {
let targetDir = `${targetDir}/${languageCode}`;
async function buildOutputMd(files, languageCode, rootTargetDir) {
let targetDir = `${rootTargetDir}/${languageCode}`;
for (let file of files) {
// check if file is translated in target language
@@ -354,7 +353,8 @@ async function buildOutputMd(files, languageCode, targetDir) {
await correctLinkInFile(file, languageCode);
let translatedMd = parseTreeToMdStr(file.doc, languageCode);
let path = file.path.replace(repoDocDir, targetDir);
let path = `${targetDir}/${file.path}`;
// check if every directory in path exists and create if not
let dirs = path.split('/');
let dir = '';
@@ -420,7 +420,7 @@ async function translateDoc(owner, repoName, repoDocDir, language, code, savePat
await translateFiles(files, language, code, savepath);
}
async function buildeDoc(owner, repoName, code, savePath, outputPath) {
async function buildDoc(owner, repoName, code, savePath, outputPath) {
// const files = await listDocFiles(owner, repoName, repoDocDir);
let files = [];
@@ -433,7 +433,7 @@ async function buildeDoc(owner, repoName, code, savePath, outputPath) {
}
let outPath = `outputPath`;
let outPath = outputPath;
try {
await fs.access(outPath);
} catch {
@@ -461,5 +461,5 @@ async function buildeDoc(owner, repoName, code, savePath, outputPath) {
module.exports =
{
translateDoc: translateDoc,
buildeDoc: buildeDoc
buildDoc: buildDoc
}