mirror of
https://github.com/9001/copyparty.git
synced 2025-08-17 09:02:15 -06:00
more lenient md table formatter
This commit is contained in:
parent
9c309b1498
commit
0c53de6767
|
@ -602,7 +602,7 @@ function fmt_table(e) {
|
||||||
//o0 = txt.lastIndexOf('\n\n', ofs),
|
//o0 = txt.lastIndexOf('\n\n', ofs),
|
||||||
//o1 = txt.indexOf('\n\n', ofs);
|
//o1 = txt.indexOf('\n\n', ofs);
|
||||||
o0 = reLastIndexOf(txt, /\n\s*\n/m, ofs),
|
o0 = reLastIndexOf(txt, /\n\s*\n/m, ofs),
|
||||||
o1 = txt.slice(ofs).search(/\n\s*\n/m);
|
o1 = txt.slice(ofs).search(/\n\s*\n|\n\s*$/m);
|
||||||
// note \s contains \n but its fine
|
// note \s contains \n but its fine
|
||||||
|
|
||||||
if (o0 < 0)
|
if (o0 < 0)
|
||||||
|
@ -625,10 +625,18 @@ function fmt_table(e) {
|
||||||
re_lpipe = lpipe ? /^\s*\|\s*/ : /^\s*/,
|
re_lpipe = lpipe ? /^\s*\|\s*/ : /^\s*/,
|
||||||
re_rpipe = rpipe ? /\s*\|\s*$/ : /\s*$/;
|
re_rpipe = rpipe ? /\s*\|\s*$/ : /\s*$/;
|
||||||
|
|
||||||
|
// the second row defines the table,
|
||||||
|
// need to process that first
|
||||||
|
var tmp = tab[0];
|
||||||
|
tab[0] = tab[1];
|
||||||
|
tab[1] = tmp;
|
||||||
|
|
||||||
for (var a = 0; a < tab.length; a++) {
|
for (var a = 0; a < tab.length; a++) {
|
||||||
|
var row_name = (a == 1) ? 'header' : 'row#' + (a + 1);
|
||||||
|
|
||||||
var ind2 = tab[a].match(re_ind)[0];
|
var ind2 = tab[a].match(re_ind)[0];
|
||||||
if (ind != ind2 && a > 0) // the table can be a list entry or something, ignore [0]
|
if (ind != ind2 && a != 1) // the table can be a list entry or something, ignore [0]
|
||||||
return alert(err + 'indentation mismatch on row 2 and ' + (a + 1) + ',\n' + tab[a]);
|
return alert(err + 'indentation mismatch on row#2 and ' + row_name + ',\n' + tab[a]);
|
||||||
|
|
||||||
var t = tab[a].slice(ind.length);
|
var t = tab[a].slice(ind.length);
|
||||||
t = t.replace(re_lpipe, "");
|
t = t.replace(re_lpipe, "");
|
||||||
|
@ -637,17 +645,25 @@ function fmt_table(e) {
|
||||||
|
|
||||||
if (a == 0)
|
if (a == 0)
|
||||||
ncols = tab[a].length;
|
ncols = tab[a].length;
|
||||||
|
else if (ncols < tab[a].length)
|
||||||
|
return alert(err + 'num.columns(' + row_name + ') exceeding row#2; ' + ncols + ' < ' + tab[a].length);
|
||||||
|
|
||||||
if (ncols != tab[a].length)
|
// if row has less columns than row2, fill them in
|
||||||
return alert(err + 'num.columns mismatch on row 2 and ' + (a + 1) + '; ' + ncols + ' != ' + tab[a].length);
|
while (tab[a].length < ncols)
|
||||||
|
tab[a].push('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// aight now swap em back
|
||||||
|
tmp = tab[0];
|
||||||
|
tab[0] = tab[1];
|
||||||
|
tab[1] = tmp;
|
||||||
|
|
||||||
var re_align = /^ *(:?)-+(:?) *$/;
|
var re_align = /^ *(:?)-+(:?) *$/;
|
||||||
var align = [];
|
var align = [];
|
||||||
for (var col = 0; col < tab[1].length; col++) {
|
for (var col = 0; col < tab[1].length; col++) {
|
||||||
var m = tab[1][col].match(re_align);
|
var m = tab[1][col].match(re_align);
|
||||||
if (!m)
|
if (!m)
|
||||||
return alert(err + 'invalid column specification, row 2, col ' + (col + 1) + ', [' + tab[1][col] + ']');
|
return alert(err + 'invalid column specification, row#2, col ' + (col + 1) + ', [' + tab[1][col] + ']');
|
||||||
|
|
||||||
if (m[2]) {
|
if (m[2]) {
|
||||||
if (m[1])
|
if (m[1])
|
||||||
|
|
|
@ -139,6 +139,10 @@ a newline toplevel
|
||||||
a table | big text in this | aaakbfddd
|
a table | big text in this | aaakbfddd
|
||||||
second row | centred | bbb
|
second row | centred | bbb
|
||||||
|
|
||||||
|
||
|
||||||
|
--|--|--
|
||||||
|
foo
|
||||||
|
|
||||||
* list entry
|
* list entry
|
||||||
* [x] yes
|
* [x] yes
|
||||||
* [ ] no
|
* [ ] no
|
||||||
|
@ -227,3 +231,7 @@ unrelated neat stuff:
|
||||||
awk '/./ {printf "%s %d\n", $0, NR; next} 1' <test.md >ln.md
|
awk '/./ {printf "%s %d\n", $0, NR; next} 1' <test.md >ln.md
|
||||||
gawk '{print gensub(/([a-zA-Z\.])/,NR" \\1","1")}' <test.md >ln.md
|
gawk '{print gensub(/([a-zA-Z\.])/,NR" \\1","1")}' <test.md >ln.md
|
||||||
```
|
```
|
||||||
|
|
||||||
|
a|b|c
|
||||||
|
--|--|--
|
||||||
|
foo
|
||||||
|
|
Loading…
Reference in a new issue