convert wide tables to indented lists, and limit width of underline beneath wikipedia titles

main
Lee Hanken 2022-04-02 13:24:07 +01:00
parent b43206d0c0
commit acd7b1b6c2
2 changed files with 51 additions and 29 deletions

View File

@ -3,7 +3,7 @@ const justify = require('justify-text');
module.exports = { module.exports = {
max_column_width: 20, max_width: 100,
clean(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, "");
@ -56,46 +56,62 @@ module.exports = {
for (let c=0;c<n_cols;c++) { for (let c=0;c<n_cols;c++) {
let l = items[r][c].length; let l = items[r][c].length;
if (l>column_widths[c]) { if (l>column_widths[c]) {
if (l > this.max_column_width) { column_widths[c] = l;
column_widths[c] = this.max_column_width;
}
else {
column_widths[c] = l;
}
} }
} }
} }
// pad let total_width = column_widths.reduce((p, a) => p + a, 0);
for (let r=0;r<n_rows;r++) {
for (let c=0;c<n_cols;c++) {
items[r][c] = justify.ljust(items[r][c], column_widths[c], " ");
}
}
// output table if (total_width < this.max_width) {
if (n_rows >0 && n_cols > 0) {
if (n_rows > 1) { // pad
result += "|"; for (let r=0;r<n_rows;r++) {
for (let c=0;c<n_cols;c++) { for (let c=0;c<n_cols;c++) {
result += items[0][c]; items[r][c] = justify.ljust(items[r][c], column_widths[c], " ");
result += "|";
} }
} }
result += "\n";
result += "|"; // output table
for (let c=0;c<n_cols;c++) { if (n_rows >0 && n_cols > 0) {
result += "-".repeat(column_widths[c]) + "|"; if (n_rows > 1) {
}
result += "\n";
for (let r=1;r<n_rows;r++) {
result += "|";
for (let c=0;c<n_cols;c++) {
result += items[r][c];
result += "|"; result += "|";
for (let c=0;c<n_cols;c++) {
result += items[0][c];
result += "|";
}
} }
result += "\n"; result += "\n";
result += "|";
for (let c=0;c<n_cols;c++) {
result += "-".repeat(column_widths[c]) + "|";
}
result += "\n";
for (let r=1;r<n_rows;r++) {
result += "|";
for (let c=0;c<n_cols;c++) {
result += items[r][c];
result += "|";
}
result += "\n";
}
} }
} else {
// too wide so output indented instead
result += "\n";
for (let r=0;r<n_rows;r++) {
result += "* ";
result += items[r][0];
result += "\n";
for (let c=1;c<n_cols;c++) {
result += " * ";
result += items[r][c];
result += "\n";
}
}
} }
return result; return result;

View File

@ -25,6 +25,12 @@ module.exports = {
{ {
find: /\(https:\/\/upload.wikimedia.org\/wikipedia\/([^\/]+)\/thumb\/([^\)]+\..{3,4})\/[^\)]+\)/ig, find: /\(https:\/\/upload.wikimedia.org\/wikipedia\/([^\/]+)\/thumb\/([^\)]+\..{3,4})\/[^\)]+\)/ig,
replacement: '(https://upload.wikimedia.org/wikipedia/$1/$2)' replacement: '(https://upload.wikimedia.org/wikipedia/$1/$2)'
},
{
find: /\n\n([^\n]+)\n\-{40,}\n/ig,
replacement: (match, title) => {
return '\n\n'+title+'\n'+'-'.repeat(title.length)+'\n'
}
} }
] ]
}, },