/
textile
executable file
·78 lines (71 loc) · 1.67 KB
/
textile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
var fs = require( 'fs' );
var textile;
var nodeVersion = process && process.versions && process.versions.node;
// is this node version mostly ES6 complete
if ( parseInt( nodeVersion || 0, 10 ) > 5 ) {
// serve code from src dir
textile = require( '../src' );
}
else {
// serve transpiled code
textile = require( '../dist/textile' );
}
// clean arguments
var args = [];
process.argv.slice( 2 ).forEach( function ( m, i, s ) {
if ( ( s = /^([^=]+)=(.*)$/.exec( m ) ) ) {
args.push( s[1], s[2] );
}
else {
args.push( m );
}
});
// parse arguments
var options = {};
while ( args.length ) {
var arg = args.shift();
if ( arg === '-t' || arg === '--tokens' ) {
options.tokens = true;
}
else if ( arg === '-i' || arg === '--input' ) {
options.input = args.shift();
}
else if ( arg === '-o' || arg === '--output' ) {
options.output = args.shift();
}
else {
if ( !options.files ) { options.files = []; }
options.files.shift( arg );
}
}
// input overrides file arguments
if ( options.input ) {
options.files = [ options.input ];
}
// writer
var data = '';
function writeData () {
data = options.tokens ? JSON.stringify( textile.jsonml( data ), null, 2 )
: textile( data );
if ( options.output ) {
fs.writeFileSync( options.output, data );
}
else {
process.stdout.write( data + '\n' );
}
}
// file or stdin?
if ( options.files ) {
data = fs.readFileSync( options.files[0], 'utf8' );
writeData();
}
else {
const stdin = process.stdin;
stdin.setEncoding( 'utf8' );
stdin.on( 'data', function ( text ) {
data += text;
});
stdin.on( 'end', writeData );
stdin.resume();
}