Skip to content

Commit 5429e4a

Browse files
committed
update readme
1 parent 21a47f2 commit 5429e4a

13 files changed

+335
-59
lines changed

README.md

+46-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# Ascii Tree
22

3-
convert indented line block to an ascii directory tree.
3+
convert indented line block to an ascii directory tree.[中文](README_ZH.md)
44

5-
## usage
5+
## sync
66

77
convert one tree.
88
```javascript
9-
var convertedLines = new AsciiTree(lines, "-").convert(); // "-" is optional, AsciiTree will guess the leading char.
9+
var tree = new AsciiTree(BytesLine.getArray("hello")).convert();
10+
assert.equal("└── hello", tree.toString());
11+
// or use builder helper.
12+
var tree = new AsciiTreeBuilder().withString("hello").withEncode("UTF-8").build().convert();
13+
assert.equal("└── hello", tree.toString());
14+
15+
tree.toBufferArray();
16+
tree.toStringArray();
17+
tree.toString();
1018
```
1119

1220
convert mutilple blocks in a file. block is surround by startTag and endTag, which can be a string or a regex. prepend will be the first line of block result, append will be last line or result.
@@ -23,9 +31,20 @@ package.json
2331
```
2432

2533
```javascript
26-
var convertedLines = new AsciiTrees(lines, startTag, endTag,prepend, append, "-").convert();
34+
var convertor = new Convertor(string, /^{%\s+asciitree\s+%}$/, "{% endasciitree %}", '<pre>', '</pre>').convert();
35+
//or use builder helper.
36+
37+
var convertorBuilder = new ConvertorBuilder()
38+
.withContent(string)
39+
.withStartTag("{% asciitree %}")
40+
.withEndTag("{% endasciitree %}")
41+
.withPrepend("<pre>")
42+
.withAppend("</pre>");
2743

28-
var convertedLines = new AsciiTrees(lines,/^{%\s+asciitree\s+%}$/, "{% endasciitree %}", '<pre>', '</pre>');
44+
var convertor = convertorBuilder.build().convert();
45+
convertor.toStringArray();
46+
convertor.toBufferArray();
47+
convertor.toString();
2948
```
3049
results:
3150

@@ -60,3 +79,25 @@ out:
6079
| └── Brocfile.js
6180
└── package.json
6281
```
82+
83+
## stream
84+
85+
```javascript
86+
var src = fs.createReadStream('fixtures/tagfile.txt');
87+
var dst = fs.createWriteStream('file.txt');
88+
src.pipe(splitterStream()) //to BytesLine
89+
.pipe(blockStream("{% asciitree %}", "{% endasciitree %}")) //produce one line or block of lines.
90+
.pipe(treeStream()) // bypass oneline, process block of lines.
91+
.pipe(unwrapStream()) // flatten block lines to Buffer.
92+
.pipe(dst);
93+
94+
//or define a function
95+
function treepipe(src) {
96+
return src.pipe(splitterStream()) //to BytesLine
97+
.pipe(blockStream("{% asciitree %}", "{% endasciitree %}")) //produce one line or block of lines.
98+
.pipe(treeStream()) // bypass oneline, process block of lines.
99+
.pipe(unwrapStream()); // flatten block lines to Buffer.
100+
}
101+
102+
treepipe(src).pipe(dst);
103+
```

README_ZH.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Ascii Tree
2+
3+
将锯齿状的行块转换成一个Ascii树。 [English](README.md)
4+
5+
## 同步版本
6+
7+
转换一棵树,不包含树的分隔行。换行符在转换过程中保持不变。
8+
```javascript
9+
var tree = new AsciiTree(BytesLine.getArray("hello")).convert();
10+
assert.equal("└── hello", tree.toString());
11+
// or use builder helper.
12+
var tree = new AsciiTreeBuilder().withString("hello").withEncode("UTF-8").build().convert();
13+
assert.equal("└── hello", tree.toString());
14+
15+
tree.toBufferArray();
16+
tree.toStringArray();
17+
tree.toString();
18+
```
19+
将文本中的所有树作转换,树行块通过分割行定义开始和结束。比如下面的文本块。
20+
21+
```
22+
{% asciitree %}
23+
app
24+
-main.js
25+
-helper.js
26+
-others
27+
--Brocfile.js
28+
package.json
29+
{% endasciitree %}
30+
```
31+
32+
```javascript
33+
//识别分割行的可以是字符串或正则表达式,字符的话是trim首尾之后的相等。
34+
var convertor = new Convertor(string, /^{%\s+asciitree\s+%}$/, "{% endasciitree %}", '<pre>', '</pre>').convert();
35+
36+
//or use builder helper.
37+
var convertorBuilder = new ConvertorBuilder()
38+
.withContent(string)
39+
.withStartTag("{% asciitree %}")
40+
.withEndTag("{% endasciitree %}")
41+
.withPrepend("<pre>")
42+
.withAppend("</pre>");
43+
44+
var convertor = convertorBuilder.build().convert();
45+
convertor.toStringArray();
46+
convertor.toBufferArray();
47+
convertor.toString();
48+
```
49+
results:
50+
51+
```
52+
<pre>
53+
├── app
54+
| ├── main.js
55+
| ├── helper.js
56+
| └── others
57+
| └── Brocfile.js
58+
└── package.json
59+
</pre>
60+
```
61+
62+
63+
in:
64+
```
65+
app
66+
-main.js
67+
-helper.js
68+
-others
69+
--Brocfile.js
70+
package.json
71+
```
72+
73+
out:
74+
```
75+
├── app
76+
| ├── main.js
77+
| ├── helper.js
78+
| └── others
79+
| └── Brocfile.js
80+
└── package.json
81+
```
82+
83+
## 流版本
84+
85+
```javascript
86+
var src = fs.createReadStream('fixtures/tagfile.txt');
87+
var dst = fs.createWriteStream('file.txt');
88+
src.pipe(splitterStream()) //将输入流变成BytesLine输出流
89+
.pipe(blockStream("{% asciitree %}", "{% endasciitree %}")) //产生单一的行(不在树中间的话),行块(对应一棵树)
90+
.pipe(treeStream()) // 将单一行直接传递下去,将树块转换之后传递
91+
.pipe(unwrapStream()) // 扁平化成Buffer
92+
.pipe(dst);
93+
94+
//or define a function
95+
function treepipe(src) {
96+
return src.pipe(splitterStream()) //to BytesLine
97+
.pipe(blockStream("{% asciitree %}", "{% endasciitree %}")) //produce one line or block of lines.
98+
.pipe(treeStream()) // bypass oneline, process block of lines.
99+
.pipe(unwrapStream()); // flatten block lines to Buffer.
100+
}
101+
102+
treepipe(src).pipe(dst);
103+
```

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var TreeLine = require('./lib/tree-line');
22
var AsciiTree = require('./lib/ascii-tree');
3-
var BufferConvertor = require('./lib/buffer-convertor');
3+
var Convertor = require('./lib/convertor');
44
var LineUtil = require('./lib/line-util');
55
var Char = require('./lib/char');
66

77
module.exports.TreeLine = TreeLine;
88
module.exports.AsciiTree = AsciiTree;
9-
module.exports.BufferConvertor = BufferConvertor;
9+
module.exports.Convertor = Convertor;
1010
module.exports.LineUtil = LineUtil;
1111
module.exports.Char = Char;

lib/asciitree-builder.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ function AsciiTreeBuilder() {
88
}
99

1010
/**
11-
* @function withString
12-
* @param {string} str
11+
* @function withContent
12+
* @param {string|Buffer} content
1313
* @param {string} enc - string encoding.
1414
* @return {AsciiTreeBuilder} - return this instance.
1515
*/
16-
AsciiTreeBuilder.prototype.withString = function(str, enc) {
17-
this.isString = true;
18-
this.content = str;
16+
AsciiTreeBuilder.prototype.withContent = function(content, enc) {
17+
this.content = content;
1918
this.enc = enc;
2019
return this;
2120
};
2221

22+
2323
AsciiTreeBuilder.prototype.withEncode = function(enc) {
2424
this.enc = enc;
2525
return this;
@@ -31,9 +31,6 @@ AsciiTreeBuilder.prototype.withEncode = function(enc) {
3131
* @return {AsciiTree}
3232
*/
3333
AsciiTreeBuilder.prototype.build = function() {
34-
var bytesLines;
35-
if (this.isString) {
36-
bytesLines = BytesLine.getArray(this.content, this.enc);
37-
return new AsciiTree(bytesLines,null, this.enc);
38-
}
34+
bytesLines = BytesLine.getArray(this.content, this.enc);
35+
return new AsciiTree(bytesLines, null, this.enc);
3936
};

lib/bytes-line.js

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module.exports = BytesLine;
1313
*/
1414
function BytesLine(content, separator) {
1515
content = content || [];
16+
if (Buffer.isBuffer(content)) {
17+
content = LineUtil.buffer2bytes(content);
18+
}
1619
AbstractLine.call(this, content, separator);
1720
}
1821

@@ -97,6 +100,9 @@ BytesLine.isBytesLine = function(o) {
97100
* @return {BytesLine[]}
98101
*/
99102
BytesLine.getArray = function(src, enc) {
103+
if (!src) {
104+
return [];
105+
}
100106
var i = 0,
101107
code,
102108
lines = [],

lib/convertor-builder.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var BytesLine = require('./bytes-line');
2+
var AsciiTree = require('./ascii-tree');
3+
var Convertor = require('./convertor');
4+
5+
module.exports = ConvertorBuilder;
6+
7+
function ConvertorBuilder() {
8+
this.content = null;
9+
}
10+
11+
/**
12+
* @function withContent
13+
* @param {Buffer|string} content
14+
* @param {string} enc - string encoding.
15+
* @return {ConvertorBuilder} - return this instance.
16+
*/
17+
ConvertorBuilder.prototype.withContent = function(content, enc) {
18+
this.content = content;
19+
this.enc = enc;
20+
return this;
21+
};
22+
23+
/**
24+
* @function withStartTag
25+
* @param {string} startTag
26+
* @return {ConvertorBuilder} - return this instance.
27+
*/
28+
ConvertorBuilder.prototype.withStartTag = function(startTag) {
29+
this.startTag = startTag;
30+
return this;
31+
};
32+
33+
/**
34+
* @function withEndTag
35+
* @param {string} endTag
36+
* @return {ConvertorBuilder} - return this instance.
37+
*/
38+
ConvertorBuilder.prototype.withEndTag = function(endTag) {
39+
this.endTag = endTag;
40+
return this;
41+
};
42+
/**
43+
* @function withEncode
44+
* @param {string} enc - encoding.
45+
*/
46+
ConvertorBuilder.prototype.withEncode = function(enc) {
47+
this.enc = enc;
48+
return this;
49+
};
50+
51+
/**
52+
* @function withPrepend
53+
* @param {string|Buffer} prepend
54+
* @return {ConvertorBuilder} - return this instance.
55+
*/
56+
ConvertorBuilder.prototype.withPrepend = function(prepend) {
57+
this.prepend = prepend;
58+
return this;
59+
};
60+
61+
/**
62+
* @function withAppend
63+
* @param {string|Buffer} append
64+
* @return {ConvertorBuilder} - return this instance.
65+
*/
66+
ConvertorBuilder.prototype.withAppend = function(append) {
67+
this.append = append;
68+
return this;
69+
};
70+
71+
/**
72+
* create an AsciiTree instance.
73+
* @function build
74+
* @return {AsciiTree}
75+
*/
76+
ConvertorBuilder.prototype.build = function() {
77+
return new Convertor(this.content, this.startTag, this.endTag, this.prepend, this.append, this.enc);
78+
};

0 commit comments

Comments
 (0)