text.pretty-printing
An implementation of Wadler's Pretty Printer
Last updated 6 years ago by killdream .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install text.pretty-printing 
SYNC missed versions from official npm registry.

text.pretty-printing

Documentation status Build status NPM version Dependencies status Licence Stability: Stable

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 } = require('text.pretty-printing');
var { Extractor } = require('adt-simple');

// We define a simple binary tree
union Tree {
  Leaf(*),
  Branch(Tree, Tree)
} deriving (Extractor)

// And a toString method that returns the prettified document
Tree::toString = function(width, indent) {
  return pretty(width, this.toDoc(indent))
}

// Transforming a tree into a document is just regular structural
// recursion. Here `a +++ b` is shorthand for `a.concat(b)`.
Tree::toDoc = function(i) {
  return match this {
    Leaf(v)      =>
      text('Leaf(') +++ text(v.toString()) +++ text(')'),

    Branch(l, r) =>
      bracket(i, 'Branch(', l.toDoc(i) +++ text(', ') +++ r.toDoc(i), ')')
  }
}

var tree = new Branch(
  new Branch(new Leaf(1), new Branch(new Leaf(2), new Leaf(3))),
  new Leaf(4)
)

console.log(tree.toString(30, 4))
// => "Branch(
//        Branch(
//            Leaf(1), Branch(
//                Leaf(2), Leaf(3)
//            )
//        ), Leaf(4)
//    )"

console.log(tree.toString(80, 4))
// => "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.

Current Tags

  • 1.0.2                                ...           latest (6 years ago)

2 Versions

  • 1.0.2                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (12)

Copyright 2014 - 2017 © taobao.org |