0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-13 17:22:49 +00:00

Merge pull request #1522 from naturalcrit/tableColSpan

Tables now support row spans
This commit is contained in:
Trevor Buckner
2021-08-06 16:59:25 -04:00
committed by GitHub
2 changed files with 40 additions and 13 deletions

View File

@@ -70,9 +70,10 @@ module.exports = {
let cantrips = 3; let cantrips = 3;
let spells = 1; let spells = 1;
let slots = 2; let slots = 2;
return `<div class='classTable wide'>\n##### The ${classname}\n` + return `{{classTable,wide\n##### The ${classname}\n` +
`| Level | Proficiency Bonus | Features | Cantrips Known | Spells Known | 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | 9th |\n`+ `| Level | Proficiency | Features | Cantrips | Spells | --- Spell Slots Per Level --- |||||||||\n`+
`|:---:|:---:|:---|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|\n${ `| ^| Bonus ^| ^| Known ^| Known ^| 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | 9th |\n`+
`|:-----:|:-----------:|:---------|:--------:|:------:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|\n${
_.map(levels, function(levelName, level){ _.map(levels, function(levelName, level){
const res = [ const res = [
levelName, levelName,
@@ -88,7 +89,7 @@ module.exports = {
slots += _.random(0, 2); slots += _.random(0, 2);
return `| ${res} |`; return `| ${res} |`;
}).join('\n')}\n</div>\n\n`; }).join('\n')}\n}}\n\n`;
}, },
half : function(){ half : function(){

View File

@@ -287,13 +287,13 @@ const spanTable = {
// Get any remaining header rows // Get any remaining header rows
l = item.header.length; l = item.header.length;
for (i = 1; i < l; i++) { for (i = 1; i < l; i++) {
item.header[i] = splitCells(item.header[i], colCount); item.header[i] = splitCells(item.header[i], colCount, item.header[i-1]);
} }
// Get main table cells // Get main table cells
l = item.rows.length; l = item.rows.length;
for (i = 0; i < l; i++) { for (i = 0; i < l; i++) {
item.rows[i] = splitCells(item.rows[i], colCount); item.rows[i] = splitCells(item.rows[i], colCount, item.rows[i-1]);
} }
// header child tokens // header child tokens
@@ -330,7 +330,7 @@ const spanTable = {
for (j = 0; j < row.length; j++) { for (j = 0; j < row.length; j++) {
cell = row[j]; cell = row[j];
text = this.parseInline(cell.tokens); text = this.parseInline(cell.tokens);
output += getTableCell(text, cell.colspan, 'th', token.align[col]); output += getTableCell(text, cell, 'th', token.align[col]);
col += cell.colspan; col += cell.colspan;
} }
output += `</tr>`; output += `</tr>`;
@@ -345,7 +345,7 @@ const spanTable = {
for (j = 0; j < row.length; j++) { for (j = 0; j < row.length; j++) {
cell = row[j]; cell = row[j];
text = this.parseInline(cell.tokens); text = this.parseInline(cell.tokens);
output += getTableCell(text, cell.colspan, 'td', token.align[col]); output += getTableCell(text, cell, 'td', token.align[col]);
col += cell.colspan; col += cell.colspan;
} }
output += `</tr>`; output += `</tr>`;
@@ -357,14 +357,18 @@ const spanTable = {
} }
}; };
const getTableCell = (text, colspan, type, align)=>{ const getTableCell = (text, cell, type, align)=>{
if(!cell.rowspan) {
return '';
}
const tag = `<${type}` const tag = `<${type}`
+ `${colspan > 1 ? ` colspan=${colspan}` : ''}` + `${cell.colspan > 1 ? ` colspan=${cell.colspan}` : ''}`
+ `${cell.rowspan > 1 ? ` rowspan=${cell.rowspan}` : ''}`
+ `${align ? ` align=${align}` : ''}>`; + `${align ? ` align=${align}` : ''}>`;
return `${tag + text}</${type}>\n`; return `${tag + text}</${type}>\n`;
}; };
const splitCells = (tableRow, count)=>{ const splitCells = (tableRow, count, prevRow = [])=>{
// ensure that every cell-delimiting pipe has a space // ensure that every cell-delimiting pipe has a space
// before it to distinguish it from an escaped pipe // before it to distinguish it from an escaped pipe
const row = tableRow.replace(/(\|+)/g, (match, p1, offset, str)=>{ const row = tableRow.replace(/(\|+)/g, (match, p1, offset, str)=>{
@@ -391,12 +395,34 @@ const splitCells = (tableRow, count)=>{
let numCols = 0; let numCols = 0;
for (; i < cells.length; i++) { for (; i < cells.length; i++) {
const trimmedCell = cells[i].split(/(?<!\\)\|+$/)[0]; const trimmedCell = cells[i].split(/ \|+$/)[0];
cells[i] = { cells[i] = {
colspan : 1 + cells[i].length - trimmedCell.length, rowspan : 1,
colspan : Math.max(cells[i].length - trimmedCell.length, 1),
text : trimmedCell.trim().replace(/\\\|/g, '|') text : trimmedCell.trim().replace(/\\\|/g, '|')
// leading or trailing whitespace is ignored per the gfm spec // leading or trailing whitespace is ignored per the gfm spec
}; };
// Handle Rowspan
if(trimmedCell.slice(-1) == '^' && prevRow.length) {
// Find matching cell in previous row
let prevCols = 0;
let j, prevCell;
for (j = 0; j < prevRow.length; j++) {
prevCell = prevRow[j];
if((prevCols == numCols) && (prevCell.colspan == cells[i].colspan)) {
cells[i].rowSpanTarget = prevCell.rowSpanTarget ?? prevCell;
cells[i].rowSpanTarget.text += ` ${cells[i].text.slice(0, -1)}`;
cells[i].rowSpanTarget.rowspan += 1;
cells[i].rowspan = 0;
break;
}
prevCols += prevCell.colspan;
if(prevCols > numCols)
break;
}
}
numCols += cells[i].colspan; numCols += cells[i].colspan;
} }