kanji-sheets/jishoScrape/src/kanjiTables.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-06-09 15:19:20 +02:00
function makeNumberRow(xLength) {
2020-05-28 22:03:03 +02:00
let numberRow = [...Array(xLength).keys()];
numberRow = numberRow.map((number) => (number + 1).toString());
numberRow = numberRow.map((number) => `{\\large ${number}}`);
numberRow = [' ', ...numberRow];
2020-06-09 15:19:20 +02:00
return `${numberRow.join(' & ')} \\\\\n\\hline\n\\endhead\n`;
}
2020-05-28 22:03:03 +02:00
2020-06-09 18:24:49 +02:00
function kanjiRow(index, rowLength, kanjiArray) {
2020-06-09 15:19:20 +02:00
let result = [];
for (let rowIndex = 0; rowIndex < rowLength; rowIndex++) {
const currentIndex = index + rowIndex;
result.push(kanjiArray[currentIndex] ? kanjiArray[currentIndex] : '');
}
return result;
}
2020-06-09 15:19:20 +02:00
function makeRows(rowLength, columnLength, kanjiArray) {
let result = '';
for (let columnIndex = 0; columnIndex < columnLength; columnIndex++) {
2020-06-09 18:24:49 +02:00
let line = new Array;
2020-06-09 15:19:20 +02:00
const index = columnIndex * rowLength;
2020-06-09 15:19:20 +02:00
// Add the number of current character
line.push(`{\\large ${index}}`);
2020-05-27 16:54:57 +02:00
2020-06-09 15:19:20 +02:00
// Concatenate the number with the rest of the row
2020-06-09 18:24:49 +02:00
line = [line, kanjiRow(index, rowLength, kanjiArray)];
2020-06-09 15:19:20 +02:00
// Convert the line array into a tex row and add it to result.
result += `${line.join(' & ')} \\\\\n`;
}
2020-06-09 15:19:20 +02:00
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
* @returns {string} A tex tabular
*/
function chapterTabular(kanjiArray, rowLength) {
const columnLength = Math.ceil(kanjiArray.length/rowLength);
let tabularString = '';
tabularString += makeNumberRow(rowLength);
tabularString += makeRows(rowLength, columnLength, kanjiArray);
return `\\begin{chapterTabular}{ ${'l | ' + 'l '.repeat(rowLength)}}
${tabularString}\\end{chapterTabular}`
}
2020-06-09 15:19:20 +02:00
exports.chapterTabular = chapterTabular;