kanji-sheets/jishoScrape/src/chapterTabular.js

58 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2021-01-13 16:39:19 +01:00
const makeNumberRow = (length) => {
const numberRow = [ ' ',
...[...Array(length).keys()]
.map(num => `{\\large ${num+1}}`),
' ' ].join(' & ');
2020-06-11 00:01:44 +02:00
return `
2021-01-13 16:39:19 +01:00
${numberRow} \\\\
2020-06-11 00:01:44 +02:00
\\hline
\\endhead\n`;
2020-06-09 15:19:20 +02:00
}
2020-05-28 22:03:03 +02:00
2021-01-13 16:39:19 +01:00
const lastNonEmptyChar = (chars) => {
let index = chars.length - 1;
while (chars[index] == '') index--;
return chars[index]
2020-06-09 15:19:20 +02:00
}
2021-01-13 16:39:19 +01:00
const makeKanjiRow = (index, chars) =>
[ `{\\large ${index}}`,
...chars.map(chara => `\\hyperref[${chara}]{${chara}}`),
`p.\\pageref{${lastNonEmptyChar(chars)}}` ].join(' & ');
2020-05-27 16:54:57 +02:00
2021-01-13 16:39:19 +01:00
const splitBy = (array, num) => {
let results = [];
while (array.length) {
results.push(array.splice(0, num));
}
2021-01-13 16:39:19 +01:00
results[results.length-1].length = num;
results[results.length-1] = Array.from(results[results.length-1], chara => chara || '');
return results;
2020-06-09 15:19:20 +02:00
}
2021-01-13 16:39:19 +01:00
const makeRows = (length, kanjiArray) =>
[ ...splitBy(kanjiArray, length)
.map((row, i) => makeKanjiRow(i*length,row)),
'' ].join(' \\\\\n');
2020-06-09 15:19:20 +02:00
/**
* 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
*/
2021-01-13 16:39:19 +01:00
function chapterTabular(kanjiArray, length) {
// const height = Math.ceil(kanjiArray.length/length);
2020-06-09 15:19:20 +02:00
let tabularString = '';
2021-01-13 16:39:19 +01:00
tabularString += makeNumberRow(length);
tabularString += makeRows(length, kanjiArray);
2020-06-09 15:19:20 +02:00
2021-01-13 16:39:19 +01:00
return `\\begin{chapterTabular}{ ${' l | ' + 'l '.repeat(length + 1)}}
2020-06-09 15:19:20 +02:00
${tabularString}\\end{chapterTabular}`
}
2021-01-13 16:39:19 +01:00
exports.chapterTabular = chapterTabular;