Skip to content

Commit

Permalink
use default language in config if arg not specified - post doc:gen fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRichi3 committed May 13, 2024
1 parent 57bfbe4 commit 7b3612b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 53 deletions.
20 changes: 11 additions & 9 deletions src/fixes/fix_crowdin_translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import fs from "fs";
* Move the updated crowdin translations from translations folder to the output directory
*/
export async function moveTranslatedFilesToOutputDir() {
const translationsFolderPath = path.join(__dirname, "/../../" + CONFIG.translationsDirectory);
const translationsFolderPath = path.join(
__dirname,
"/../../" + CONFIG.translationsDirectory,
);

async function copyAllFoldersAndFiles(source: string, destination: string) {
const files = await fs.promises.readdir(source);
Expand Down Expand Up @@ -182,20 +185,19 @@ async function start() {
}

if (require.main === module) {
const languages = process.argv
const langs = process.argv
.find((arg) => arg.startsWith("languages"))
?.split("=")[1]
?.split(",");

if (!languages) {
console.log(
"Please provide languages to create localized docs for using the languages flag",
if (!langs) {
console.warn(
"Languages not specified in cli arguments, setting languages to default",
);
process.exit(1);
}

languages.shift();
CONFIG.languages = languages;
CONFIG.languages = langs ?? CONFIG.languages;
CONFIG.languages.shift(); // Remove the default language from the list of Languages

start();
}

51 changes: 34 additions & 17 deletions src/fixes/fix_duplicate_language_refs.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,68 @@
import * as Cheerio from "cheerio";
import fs from 'fs'
import fs from "fs";
import logger from "../utils/logger";
import path from "path";
import CONFIG from "../config";

export async function fixDuplicateLanguageReferences(languages: string[]) {
export async function fixDuplicateLanguageReferences() {
const languages = CONFIG.languages;
// Get the index files for each language
for (const lang of languages.slice(1)) {
const directoryForHtmlFiles = path.join(CONFIG.outputDirectory, `_book/${lang}/`);
const directoryForHtmlFiles = path.join(
CONFIG.outputDirectory,
`_book/${lang}/`,
);

logger.info('Reading index file for language: ' + lang)
const indexFile = await fs.promises.readFile(directoryForHtmlFiles + `index.${lang}.html`, "utf-8");
logger.info('Finished reading index file for language: ' + lang)
logger.info("Reading index file for language: " + lang);
const indexFile = await fs.promises.readFile(
directoryForHtmlFiles + `index.${lang}.html`,
"utf-8",
);
logger.info("Finished reading index file for language: " + lang);

// Get the html elements (anchor elements) with classname 'dropdown-item' from each index file
const $ = Cheerio.load(indexFile);
const dropdownItems = $('.dropdown-item');
const dropdownItems = $(".dropdown-item");
const uniqueItems = new Set();

// Remove duplicate elements from index file (use the href as the unique idendifier)
dropdownItems.each((index, element) => {
logger.info('Removing duplicate items from index file for language: ' + lang)
const href = $(element).attr('href');
logger.info(
"Removing duplicate items from index file for language: " +
lang,
);
const href = $(element).attr("href");
if (uniqueItems.has(href)) {
$(element).remove();
}
uniqueItems.add(href);
});

await fs.promises.writeFile(directoryForHtmlFiles + `index.${lang}.html`, $.html());
logger.info('Finished removing duplicate items from index file for language: ' + lang)
await fs.promises.writeFile(
directoryForHtmlFiles + `index.${lang}.html`,
$.html(),
);
logger.info(
"Finished removing duplicate items from index file for language: " +
lang,
);
}
}


if (require.main === module) {
const langs = process.argv
.find((arg) => arg.startsWith("languages"))
?.split("=")[1]
?.split(",");

if (!langs) {
console.log(
"Please provide languages to create localized docs for using the languages flag",
console.warn(
"Languages not specified in cli arguments, setting languages to default",
);
process.exit(1);
}

fixDuplicateLanguageReferences(langs)
}
CONFIG.languages = langs ?? CONFIG.languages;

fixDuplicateLanguageReferences();
}

18 changes: 11 additions & 7 deletions src/fixes/fix_localized_index_file.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import CONFIG from "../config";
import Writer from "../utils/writer";

export async function fixLocalizedIndexFiles(langs: string[]) {
await Writer.fixMissingLocalizedIndexFiles(langs)
export async function fixLocalizedIndexFiles() {
const langs = CONFIG.languages;
await Writer.fixMissingLocalizedIndexFiles(langs);
}

if (require.main === module) {
Expand All @@ -11,11 +13,13 @@ if (require.main === module) {
?.split(",");

if (!langs) {
console.log(
"Please provide languages to create localized docs for using the languages flag",
console.warn(
"Languages not specified in cli arguments, setting languages to default",
);
process.exit(1);
}

fixLocalizedIndexFiles(langs)
}
CONFIG.languages = langs ?? CONFIG.languages;

fixLocalizedIndexFiles();
}

62 changes: 42 additions & 20 deletions src/fixes/fix_wrong_language_refs.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
import * as Cheerio from "cheerio";
import fs from 'fs'
import fs from "fs";
import logger from "../utils/logger";
import path from "path";
import CONFIG from "../config";

export async function fixWrongLanguageReferences(languages: string[]) {
export async function fixWrongLanguageReferences() {
const languages = CONFIG.languages;

// Get the index files for each language
for (const lang of languages.slice(1)) {
const directoryForHtmlFiles = path.join(CONFIG.outputDirectory, `/_book/${lang}/`);
const directoryForHtmlFiles = path.join(
CONFIG.outputDirectory,
`/_book/${lang}/`,
);

logger.info('Reading index file for language: ' + lang)
const indexFile = await fs.promises.readFile(directoryForHtmlFiles + `index.${lang}.html`, "utf-8");
logger.info('Finished reading index file for language: ' + lang)
logger.info("Reading index file for language: " + lang);
const indexFile = await fs.promises.readFile(
directoryForHtmlFiles + `index.${lang}.html`,
"utf-8",
);
logger.info("Finished reading index file for language: " + lang);

// Get the html elements (anchor elements) with classname 'dropdown-item' from each index file
const $ = Cheerio.load(indexFile);

// Get the html element referencing the main language
const mainLang = $(`#language-link-${languages[0]}`)
const mainLang = $(`#language-link-${languages[0]}`);

// Correct the href attribute of the main language element
mainLang.attr('href', `../index.html`);
mainLang.attr("href", `../index.html`);

// Get the html elements referencing the other languages
const otherLangs = languages.slice(1)
.map((code) => ({ code: code, htmlRef: $(`#language-link-${code}`) }))
const otherLangs = languages.slice(1).map((code) => ({
code: code,
htmlRef: $(`#language-link-${code}`),
}));

// Correct the href attribute of the other language elements
logger.info('Correcting href attribute of other language elements')
otherLangs.forEach((language) => language.htmlRef.attr('href', `../${language.code}/index.${language.code}.html`))
logger.info('Finished correcting href attribute of other language elements')
logger.info("Correcting href attribute of other language elements");
otherLangs.forEach((language) =>
language.htmlRef.attr(
"href",
`../${language.code}/index.${language.code}.html`,
),
);
logger.info(
"Finished correcting href attribute of other language elements",
);

await fs.promises.writeFile(directoryForHtmlFiles + `index.${lang}.html`, $.html());
logger.info('Updated index file for language: ' + lang)
await fs.promises.writeFile(
directoryForHtmlFiles + `index.${lang}.html`,
$.html(),
);
logger.info("Updated index file for language: " + lang);
}
}

Expand All @@ -43,11 +63,13 @@ if (require.main === module) {
?.split(",");

if (!langs) {
console.log(
"Please provide languages to create localized docs for using the languages flag",
console.warn(
"Languages not specified in cli arguments, setting languages to default",
);
process.exit(1);
}

fixWrongLanguageReferences(langs)
}
CONFIG.languages = langs ?? CONFIG.languages;

fixWrongLanguageReferences();
}

0 comments on commit 7b3612b

Please sign in to comment.