correct decision how to present table
parent
877bd0d840
commit
62a73aeaad
|
@ -2,14 +2,15 @@ const htmlEntities = require('html-entities');
|
||||||
const justify = require('justify-text');
|
const justify = require('justify-text');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
max_width: 96,
|
||||||
|
|
||||||
max_width: 100,
|
clean: function (str) {
|
||||||
clean(str) {
|
|
||||||
str = str.replace(/<\/?[^>]+(>|$)/g, "");
|
str = str.replace(/<\/?[^>]+(>|$)/g, "");
|
||||||
str = str.replace(/(\r\n|\n|\r)/gm, "");
|
str = str.replace(/(\r\n|\n|\r)/gm, "");
|
||||||
str = htmlEntities.decode(str);
|
str = htmlEntities.decode(str);
|
||||||
return str;
|
return str;
|
||||||
},
|
},
|
||||||
|
|
||||||
convert: function (table) {
|
convert: function (table) {
|
||||||
let result = "\n";
|
let result = "\n";
|
||||||
|
|
||||||
|
@ -30,6 +31,10 @@ module.exports = {
|
||||||
items.push(item_cols);
|
items.push(item_cols);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// is this a proper table?
|
||||||
|
if (n_rows < 2)
|
||||||
|
return "";
|
||||||
|
|
||||||
// find number of columns
|
// find number of columns
|
||||||
let n_cols=0;
|
let n_cols=0;
|
||||||
for (let r=0;r<n_rows;r++) {
|
for (let r=0;r<n_rows;r++) {
|
||||||
|
@ -61,18 +66,21 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let total_width = column_widths.reduce((p, a) => p + a, 0);
|
// decide how to present
|
||||||
|
let total_width = 0;
|
||||||
|
for (let c=0;c<n_cols;c++)
|
||||||
|
total_width = total_width + column_widths[c];
|
||||||
|
|
||||||
if (total_width < this.max_width) {
|
if (total_width < this.max_width) {
|
||||||
|
// present as table
|
||||||
|
|
||||||
// pad
|
// pad cells
|
||||||
for (let r=0;r<n_rows;r++) {
|
for (let r=0;r<n_rows;r++) {
|
||||||
for (let c=0;c<n_cols;c++) {
|
for (let c=0;c<n_cols;c++) {
|
||||||
items[r][c] = justify.ljust(items[r][c], column_widths[c], " ");
|
items[r][c] = justify.ljust(items[r][c], column_widths[c], " ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// output table
|
|
||||||
if (n_rows >0 && n_cols > 0) {
|
if (n_rows >0 && n_cols > 0) {
|
||||||
if (n_rows > 1) {
|
if (n_rows > 1) {
|
||||||
result += "|";
|
result += "|";
|
||||||
|
@ -98,7 +106,7 @@ module.exports = {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// too wide so output indented instead
|
// present as indented list
|
||||||
|
|
||||||
result += "\n";
|
result += "\n";
|
||||||
for (let r=1;r<n_rows;r++) {
|
for (let r=1;r<n_rows;r++) {
|
||||||
|
|
Loading…
Reference in New Issue