text.pretty-printing
An implementation of Wadler's Pretty Printer; an efficient algebra that supports multiple layouts, choosing the best one for the available screen space.
Example
var pretty text bracket = ;var Extractor = ; // We define a simple binary treeunion Tree // And a toString method that returns the prettified documentTree:: { return } // Transforming a tree into a document is just regular structural// recursion. Here `a +++ b` is shorthand for `a.concat(b)`.Tree:: { return match this +++ +++ } var tree = 1 2 3 4 console// => "Branch(// Branch(// Leaf(1), Branch(// Leaf(2), Leaf(3)// )// ), Leaf(4)// )" console// => "Branch( Branch( Leaf(1), Branch( Leaf(2), Leaf(3) ) ), Leaf(4) )"
See the examples/
folder for additional examples.
Installing
Grab the latest release from npm:
$ npm install text.pretty-printing
Compiling from source
If you want to compile this library from the source, you'll need Git, Make, Node.js, and run the following commands:
$ git clone git://github.com/folktale/text.pretty-printing.git
$ cd text.pretty-printing
$ npm install
$ make bundle
This will generate the dist/text.pretty-printing.umd.js
file, which you can load in
any JavaScript environment.
Documentation
You'll need Sphinx to generate the documentation:
$ git clone git://github.com/folktale/text.pretty-printing.git
$ cd text.pretty-printing
$ npm install
$ make documentation
Then open the file docs/build/html/index.html
.
Platform support
This library assumes an ES5 environment, but can be easily supported in ES3 platforms by the use of shims. Just include es5-shim :)
Licence
Copyright (c) 2015 Quildreen Motta.
Released under the MIT licence.