diff --git a/jishoScrape/index.js b/jishoScrape/index.js index 02e6fdd..02e6415 100644 --- a/jishoScrape/index.js +++ b/jishoScrape/index.js @@ -3,7 +3,7 @@ const fs = require('fs'); /* Import local files */ const { fetchJishoResults } = require('./src/dataFetching.js'); const { getKanjiTexData } = require('./src/texConversion.js'); -const { chapterTabular } = require('./src/kanjiTables.js'); +const { chapterTabular } = require('./src/chapterTabular.js'); /* Encapsulate main process in async function */ async function main(grade) { diff --git a/jishoScrape/src/kanjiTables.js b/jishoScrape/src/chapterTabular.js similarity index 99% rename from jishoScrape/src/kanjiTables.js rename to jishoScrape/src/chapterTabular.js index e24dc73..14a6994 100644 --- a/jishoScrape/src/kanjiTables.js +++ b/jishoScrape/src/chapterTabular.js @@ -1,5 +1,3 @@ - - function makeNumberRow(rowLength) { let numberRow = [...Array(rowLength).keys()]; // Array containing numbers 0 to rowLength -1 numberRow = numberRow.map((number) => (number + 1).toString()); // Correct numbers and convert to string @@ -36,7 +34,6 @@ function makeRows(rowLength, columnLength, kanjiArray) { return result; } - /** * Turns an array of kanji into a tabular for a chapter overview * @param {string[]} kanjiArray An array of kanji characters to put into the tabular diff --git a/jishoScrape/src/texConversion.js b/jishoScrape/src/texConversion.js index ca418e4..d386bb0 100644 --- a/jishoScrape/src/texConversion.js +++ b/jishoScrape/src/texConversion.js @@ -1,4 +1,79 @@ -const textFiltration = require('./textFiltration.js') +const stylingBrackets = { + "start": '\\textbf{\\textcolor{kanjiColor!80!black}{', + "end": '}}' +}; +const yomiConnector = '、 '; +const yomiDash = '—'; + +const styleText = (string) => stylingBrackets.start + string + stylingBrackets.end; + +function styleCharactersBeforeDot(string) { + const words = string.split('.'); + words[0] = styleText(words[0]); + return words.join(''); +} + +function styleEverythingExceptDash(string) { + const words = string.split(/(?<=\-)/); + if (words[0] === '-') { // ['-', 'word'] + words[0] = yomiDash; + words[1] = styleText(words[1]); + } else { // ['Word-', ''] + words[1] = yomiDash; + words[0] = words[0].slice(0, words[0].length-1); + words[0] = styleText(words[0]); + } + return words.join(''); +} + +function convertKunyomi(jishoResult) { + + if (jishoResult.kunyomi.length === 0) return ''; + + const kunyomi = JSON.stringify(jishoResult.kunyomi) + .replace(/"|\[|\]/g, '') + .replace(/\\/g, '\\\\') + .replace(/%/g, '\\%') + .replace(/&/g, '\\&') + .split(','); + + for (const i in kunyomi) { + instance = kunyomi[i]; + + if (instance.includes('.') && instance.includes('-')) { + //TODO: Apply combinated logic here + } + else if (instance.includes('.')) { + kunyomi[i] = styleCharactersBeforeDot(instance); + } + else if (instance.includes('-')) { + kunyomi[i] = styleEverythingExceptDash(instance); + } + else { + kunyomi[i] = styleText(instance); + } + } + + return kunyomi.join(yomiConnector); +} + +function convertOnyomi(jishoResult) { + return JSON.stringify(jishoResult.onyomi) + .replace(/"|\[|\]/g, '') + .replace(/\\/g, '\\\\') + .replace(/%/g, '\\%') + .replace(/,/g, yomiConnector) + .replace(/&/g, '\\&'); + + //TODO: Style only the words, and not the yomiConnector inbetween +} + +function convertMeaning(jishoResult) { + return jishoResult.meaning + .replace(/\\/g, '\\\\') + .replace(/%/g, '\\%') + .replace(/&/g, '\\&'); +} const makeFirstLetterUppercase = (string) => string.charAt(0).toUpperCase() + string.slice(1); @@ -10,9 +85,9 @@ const makeFirstLetterUppercase = (string) => string.charAt(0).toUpperCase() + st function getKanjiTexData(jishoResults) { return jishoResults.map(jishoResult => { - const meaning = textFiltration.convertMeaning(jishoResult); - const kunyomi = textFiltration.convertKunyomi(jishoResult); - const onyomi = textFiltration.convertOnyomi(jishoResult); + const meaning = convertMeaning(jishoResult); + const kunyomi = convertKunyomi(jishoResult); + const onyomi = convertOnyomi(jishoResult); jishoResult.taughtIn = jishoResult.taughtIn ? makeFirstLetterUppercase(jishoResult.taughtIn) : ''; diff --git a/jishoScrape/src/textFiltration.js b/jishoScrape/src/textFiltration.js deleted file mode 100644 index 6b8aa6b..0000000 --- a/jishoScrape/src/textFiltration.js +++ /dev/null @@ -1,80 +0,0 @@ -const stylingBrackets = { - "start": '\\textbf{\\textcolor{kanjiColor!80!black}{', - "end": '}}' -}; -const yomiConnector = '、 '; -const yomiDash = '—'; - -const styleText = (string) => stylingBrackets.start + string + stylingBrackets.end; - -function styleCharactersBeforeDot(string) { - const words = string.split('.'); - words[0] = styleText(words[0]); - return words.join(''); -} - -function styleEverythingExceptDash(string) { - const words = string.split(/(?<=\-)/); - if (words[0] === '-') { // ['-', 'word'] - words[0] = yomiDash; - words[1] = styleText(words[1]); - } else { // ['Word-', ''] - words[1] = yomiDash; - words[0] = words[0].slice(0, words[0].length-1); - words[0] = styleText(words[0]); - } - return words.join(''); -} - -function convertKunyomi(jishoResult) { - - if (jishoResult.kunyomi.length === 0) return ''; - - const kunyomi = JSON.stringify(jishoResult.kunyomi) - .replace(/"|\[|\]/g, '') - .replace(/\\/g, '\\\\') - .replace(/%/g, '\\%') - .replace(/&/g, '\\&') - .split(','); - - for (const i in kunyomi) { - instance = kunyomi[i]; - - if (instance.includes('.') && instance.includes('-')) { - //TODO: Apply combinated logic here - } - else if (instance.includes('.')) { - kunyomi[i] = styleCharactersBeforeDot(instance); - } - else if (instance.includes('-')) { - kunyomi[i] = styleEverythingExceptDash(instance); - } - else { - kunyomi[i] = styleText(instance); - } - } - - return kunyomi.join(yomiConnector); -} - -function convertOnyomi(jishoResult) { - return JSON.stringify(jishoResult.onyomi) - .replace(/"|\[|\]/g, '') - .replace(/\\/g, '\\\\') - .replace(/%/g, '\\%') - .replace(/,/g, yomiConnector) - .replace(/&/g, '\\&'); - - //TODO: Style only the words, and not the yomiConnector inbetween -} - -function convertMeaning(jishoResult) { - return jishoResult.meaning - .replace(/\\/g, '\\\\') - .replace(/%/g, '\\%') - .replace(/&/g, '\\&'); -} - -exports.convertKunyomi = convertKunyomi; -exports.convertOnyomi = convertOnyomi; -exports.convertMeaning = convertMeaning; \ No newline at end of file diff --git a/main.synctex(busy) b/main.synctex(busy) deleted file mode 100644 index e69de29..0000000