Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

Commit

Permalink
Added markdown example creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Dec 19, 2014
1 parent 869a26f commit 9188097
Show file tree
Hide file tree
Showing 3 changed files with 318 additions and 9 deletions.
263 changes: 263 additions & 0 deletions example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
use colSpan to span columns - (colSpan above normal cell)
--------
┌───────────────┐
│ greetings │
├───────────────┤
│ greetings │
├───────┬───────┤
│ hello │ howdy │
└───────┴───────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
[{colSpan:2,content:'greetings'}],
[{colSpan:2,content:'greetings'}],
['hello','howdy']
);

```


use colSpan to span columns - (colSpan below normal cell)
--------
┌───────┬───────┐
│ hello │ howdy │
├───────┴───────┤
│ greetings │
├───────────────┤
│ greetings │
└───────────────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
['hello','howdy'],
[{colSpan:2,content:'greetings'}],
[{colSpan:2,content:'greetings'}]
);

```


use rowSpan to span rows - (rowSpan on the left side)
--------
┌───────────┬───────────┬───────┐
│ greetings │ │ hello │
│ │ greetings ├───────┤
│ │ │ howdy │
└───────────┴───────────┴───────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
[{rowSpan:2,content:'greetings'},{rowSpan:2,content:'greetings',vAlign:'center'},'hello'],
['howdy']
);

```


use rowSpan to span rows - (rowSpan on the right side)
--------
┌───────┬───────────┬───────────┐
│ hello │ greetings │ │
├───────┤ │ │
│ howdy │ │ greetings │
└───────┴───────────┴───────────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
['hello',{rowSpan:2,content:'greetings'},{rowSpan:2,content:'greetings',vAlign:'bottom'}],
['howdy']
);

```


mix rowSpan and colSpan together for complex table layouts
--------
┌───────┬─────┬────┐
│ hello │ sup │ hi │
├───────┤ │ │
│ howdy │ │ │
├───┬───┼──┬──┤ │
│ o │ k │ │ │ │
└───┴───┴──┴──┴────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
[{content:'hello',colSpan:2},{rowSpan:2, colSpan:2,content:'sup'},{rowSpan:3,content:'hi'}],
[{content:'howdy',colSpan:2}],
['o','k','','']
);

```


multi-line content will flow across rows in rowSpan cells
--------
┌───────┬───────────┬───────────┐
│ hello │ greetings │ greetings │
├───────┤ friends │ friends │
│ howdy │ │ │
└───────┴───────────┴───────────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
['hello',{rowSpan:2,content:'greetings\nfriends'},{rowSpan:2,content:'greetings\nfriends'}],
['howdy']
);

```


multi-line content will flow across rows in rowSpan cells - (complex layout)
--------
┌───────┬─────┬────┐
│ hello │ sup │ hi │
├───────┤ man │ yo │
│ howdy │ hey │ │
├───┬───┼──┬──┤ │
│ o │ k │ │ │ │
└───┴───┴──┴──┴────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
[{content:'hello',colSpan:2},{rowSpan:2, colSpan:2,content:'sup\nman\nhey'},{rowSpan:3,content:'hi\nyo'}],
[{content:'howdy',colSpan:2}],
['o','k','','']
);

```


rowSpan cells can have a staggered layout
--------
┌───┬───┐
│ a │ b │
│ ├───┤
│ │ c │
├───┤ │
│ d │ │
└───┴───┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
[{content:'a',rowSpan:2},'b'],
[{content:'c',rowSpan:2}],
['d']
);

```


the layout manager automatically create empty cells to fill in the table
--------
┌───┬───┬──┐
│ a │ b │ │
│ ├───┤ │
│ │ │ │
│ ├───┴──┤
│ │ c │
├───┤ │
│ │ │
└───┴──────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

//notice we only create 3 cells here, but the table ends up having 6.
table.push(
[{content:'a',rowSpan:3,colSpan:2},'b'],
[],
[{content:'c',rowSpan:2,colSpan:2}],
[]
);
```


use table `rowHeights` option to fix row height. The truncation symbol will be shown on the last line.
--------
┌───────┐
│ hello │
│ hi… │
└───────┘
```javascript
var table = new Table({rowHeights:[2],style:{head:[],border:[]}});

table.push(['hello\nhi\nsup']);

```


if `colWidths` is not specified, the layout manager will automatically widen rows to fit the content
--------
┌─────────────┐
│ hello there │
├──────┬──────┤
│ hi │ hi │
└──────┴──────┘
```javascript
var table = new Table({style:{head:[],border:[]}});

table.push(
[{colSpan:2,content:'hello there'}],
['hi', 'hi']
);

```


you can specify a column width for only the first row, other rows will be automatically widened to fit content
--------
┌─────────────┐
│ hello there │
├────┬────────┤
│ hi │ hi │
└────┴────────┘
```javascript
var table = new Table({colWidths:[4],style:{head:[],border:[]}});

table.push(
[{colSpan:2,content:'hello there'}],
['hi',{hAlign:'center',content:'hi'}]
);

```


a column with a null column width will be automatically widened to fit content
--------
┌─────────────┐
│ hello there │
├────────┬────┤
│ hi │ hi │
└────────┴────┘
```javascript
var table = new Table({colWidths:[null, 4],style:{head:[],border:[]}});

table.push(
[{colSpan:2,content:'hello there'}],
[{hAlign:'right',content:'hi'}, 'hi']
);

```


feel free to use colors in your content strings, column widths will be calculated correctly
--------
┌─────┐
│ he… │
└─────┘
```javascript
var table = new Table({colWidths:[5],style:{head:[],border:[]}});

table.push([colors.red('hello')]);

```

6 changes: 5 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ gulp.task('watch-mocha',function(){
});

gulp.task('example',function(){
printExample.printExample(require('./examples/examples'),console.log);
printExample.logExample(require('./examples/examples'));
});

gulp.task('example-md',function(){
printExample.mdExample(require('./examples/examples'),'example.md');
});

function coverage(opts){
Expand Down
58 changes: 50 additions & 8 deletions lib/print-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,60 @@ var utils = require('../src/utils');
var chai = require('chai');
var expect = chai.expect;
var colors = require('colors/safe');
var fs = require('fs');

function runExample(fn,callback){
function printExample(name,fn,expected){
callback(colors.gray('========= ') + name + colors.gray(' ================'));
callback(fn().toString());
var message = fn.toString().split('\n').slice(1,-2).join('\n');
callback(message);
callback('\n');
function runExample(fn,logName,logTable,logCode,logSeparator){
function printExample(name,makeTable,expected){
var code = makeTable.toString().split('\n').slice(1,-2).join('\n');

logName(name);
logTable(makeTable().toString());
logCode(code);
logSeparator('\n');
}

fn(printExample);
}

function logExample(fn){
runExample(fn,
function(name){
console.log(colors.gray('========= ') + name + colors.gray(' ================'));
},
console.log,
console.log,
console.log
)
}

function stripColors(str){
return str.split( /\u001b\[(?:\d*;){0,5}\d*m/g).join('');
}

function mdExample(fn,file){
var buffer = [];

runExample(fn,
function(name){
buffer.push(name);
buffer.push('--------');
},
function(table){
buffer.push(stripColors(table));
},
function(code){
buffer.push('```javascript');
buffer.push(stripColors(code));
buffer.push('```');
},
function(sep){
buffer.push(stripColors(sep));
}
);

fs.writeFileSync(file,buffer.join('\n'));
}

function testExample(name,fn,expected){
it(name,function(){
expect(fn().toString()).to.equal(expected.join('\n'));
Expand All @@ -28,6 +69,7 @@ function runTest(fn){
}

module.exports = {
printExample:runExample,
mdExample:mdExample,
logExample:logExample,
runTest:runTest
};

0 comments on commit 9188097

Please sign in to comment.