Restructure tex conversion functions

This commit is contained in:
Oystein Kristoffer Tveit 2020-06-10 20:22:35 +02:00
parent 84bceda6fe
commit 1c7ffaebc3
5 changed files with 80 additions and 88 deletions

View File

@ -3,7 +3,7 @@ const fs = require('fs');
/* Import local files */ /* Import local files */
const { fetchJishoResults } = require('./src/dataFetching.js'); const { fetchJishoResults } = require('./src/dataFetching.js');
const { getKanjiTexData } = require('./src/texConversion.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 */ /* Encapsulate main process in async function */
async function main(grade) { async function main(grade) {

View File

@ -1,5 +1,3 @@
function makeNumberRow(rowLength) { function makeNumberRow(rowLength) {
let numberRow = [...Array(rowLength).keys()]; // Array containing numbers 0 to rowLength -1 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 numberRow = numberRow.map((number) => (number + 1).toString()); // Correct numbers and convert to string
@ -36,7 +34,6 @@ function makeRows(rowLength, columnLength, kanjiArray) {
return result; return result;
} }
/** /**
* Turns an array of kanji into a tabular for a chapter overview * 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 * @param {string[]} kanjiArray An array of kanji characters to put into the tabular

View File

@ -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); 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) { function getKanjiTexData(jishoResults) {
return jishoResults.map(jishoResult => { return jishoResults.map(jishoResult => {
const meaning = textFiltration.convertMeaning(jishoResult); const meaning = convertMeaning(jishoResult);
const kunyomi = textFiltration.convertKunyomi(jishoResult); const kunyomi = convertKunyomi(jishoResult);
const onyomi = textFiltration.convertOnyomi(jishoResult); const onyomi = convertOnyomi(jishoResult);
jishoResult.taughtIn = jishoResult.taughtIn ? makeFirstLetterUppercase(jishoResult.taughtIn) : ''; jishoResult.taughtIn = jishoResult.taughtIn ? makeFirstLetterUppercase(jishoResult.taughtIn) : '';

View File

@ -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;

View File