kanji-sheets/jishoScrape/src/chapterTabular.js

55 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-06-10 16:48:24 +02:00
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
numberRow = numberRow.map((number) => `{\\large ${number}}`); // Encapsulate numbers in TeX code
2020-05-28 22:03:03 +02:00
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
2020-06-10 16:48:24 +02:00
* @param {number} rowLength The length of each row
2020-06-09 15:19:20 +02:00
* @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;