DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:bookmarklets

Table cell prettification

The following script pads the table cells with spaces so that all are of equal width. The bookmarkable link is here FIXME

The source code:

source.js
(function() {
var editor = window.document.getElementById('wiki__text');
var lines = editor.value.split('\n');
var next = function(l, n) {
  var a = lines[l].substr(n).indexOf('^');
  var b = lines[l].substr(n).indexOf('|');
  if(a === -1 && b === -1) {
    return -1;
  }
  var r;
  if(b === -1) {
    r = a + n;
  } else if(a === -1) {
    r = b + n;
  } else {
    r = (b < a ? b : a) + n;
  }
  if(r > 0 && lines[l].charAt(r - 1) === '|' || lines[l].charAt(r - 1) === '^') {
    while(lines[l].charAt(n) !== '|' && lines[l].charAt(n) !== '^') {
      n++;
    }
    return next(l, n+1);
  } else {
    return r;
  }
};
var insert = function(c, l, n, p) {
  var s = '';
  for(var i=0;i<n;i++) {
    s+=c;
  }
  lines[l] = lines[l].substr(0, p) + s + lines[l].substr(p);
};
 
var tl, cols = null, c = -1, x = -1, dx, px = -1;
for(var l = 0; l < lines.length;) {
  x = next(l, x + 1);
  if(x === -1) {
    l++;
    c = -1;
    if(px === -1 || l === lines.length) {
      if(cols !== null) {
        for(var i = tl; i < l-1;i++) {
          x = next(i, 0);
          px = x;
          for(var j = 0; j < cols.length;j++) {
            x = next(i, x + 1);
            if(px !== -1) {
              if(x === -1) {
                insert(' ', i, cols[j], px + 1);
                x = px + 1 + cols[j];
                insert(lines[j].charAt(px), i, 1, x);
              } else {
                insert(' ', i, cols[j] - x + px + 1, x);
                x += cols[j] - x + px + 1;
              }
            }
            px = x;
          }
        }
      }
      px = -1;
      x = -1;
      cols = null;
    } else {
      px = -1;
    }
  } else if(px !== -1) {
    if(cols === null) {
       cols = [];
       tl = l;
    }
    c++;
    dx = x - px - 1;
    if(typeof cols[c] === 'undefined' || cols[c] < dx) {
      cols[c] = dx;
    }
  }
  px = x;
}
editor.value = lines.join('\n');
})();
tips/bookmarklets.txt · Last modified: by Aleksandr

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki