Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A simple JavaScript library designed to simplify generation of tree-like structures taking strings as input
Ruby JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
config Initial import
lib
script
src
tasks
test
.gitignore
History.txt
License.txt
README.mdown
Rakefile

README.mdown

TextTree

TextTree is a simple JavaScript library designed to simplify generation of tree-like structures taking strings as input.

Node nesting is expressed by indentation (only spaces, no tabs allowed) of lines. Lines are converted to child nodes of previous line which is indented at lower level.

Example usage

Use TextTree.parse to create the tree from a string:

var tree = TextTree.parse('a\n  b\n    c\nd\n  e');
// returns a string representation of the tree, which,
// in this case, should be **identical** to the given input
tree.toString();

Yeah, that's not very useful. But if you pass a transformer function as a second argument, it will be used to create (or discard) node-like objects according to your necessities.

The transformer callback must accept three arguments: text, tabs and line number. Its return value must be null (to discard the line) or a node-like object able to handle the same interface than TextTree.Node (inherition recommended).

var F = function() {};
F.prototype = TextTree.Node.prototype;
var MyNodeType = function() {
  TextTree.Node.apply(this, arguments);
};
MyNodeType.prototype = new F();
MyNodeType.prototype.toString = function() {
  return this.text + ' { indent: ' + this.tabs + ', lineNumber: ' + this.lineNumber + ' }\n' + this.eachChild(function(node) {
    return node.toString();
  });
};
var tree = TextTree.parse('a\n  b\nc\n  # a comment\n  d', function(text, tabs, line) {
  if ('#' == text.charAt(0)) return null; // skip comments
  return new MyNodeType(text, tabs, line);
});
tree.toString();

Output from the previous script:

a { indent: 0, lineNumber: 1 }
b { indent: 1, lineNumber: 2 }
c { indent: 0, lineNumber: 3 }
d { indent: 1, lineNumber: 5 }

Still not very useful. But, hey, you can create really useful node types. You can use a transformer callback that uses one or another node type based on patterns of the input text. Heard about Sass? Haml? Cucumber? You may use TextTree as a base library to parse any text format that relies on indentation to express parent-children relationship (I do).

QOWA (Questions Oscar will ask)

How much indentation?

You can use any number of spaces as the base indentation. Once a line in the input appears indented, the number of spaces at its start will be taken as the indentation unit. Incorrect indentation will throw named exceptions. TextTree is not mindundi friendly.

Where can I find more info about TextTree?

You can't, sorry. Documentation is coming with 1.0 release, estimated date undefined.

Building

Run rake dist to build the full script (on "dist/text_tree.js").

Project website

Please don't visit http://texttree.scriptia.net for more information. It doesn't exist yet.

Author, license, source code

TextTree has been created by Choan Gálvez and is freely distributable under the terms of a MIT-style license. The source code resides in a Git repository at github.

Something went wrong with that request. Please try again.