From 1341d1bbfaab47dbfb238eaf3b568f007102d10a Mon Sep 17 00:00:00 2001 From: Quildreen Motta Date: Sun, 19 Apr 2015 14:40:38 -0300 Subject: [PATCH] Updates documentation. --- .gitignore | 2 +- .gitmodules | 3 + Makefile | 7 +- docs.md | 771 ------------------ docs/Makefile | 177 +++++ docs/make.bat | 242 ++++++ docs/source/caffeine-theme | 1 + docs/source/conf.py | 265 +++++++ docs/source/index.rst | 1541 ++++++++++++++++++++++++++++++++++++ 9 files changed, 2235 insertions(+), 774 deletions(-) create mode 100644 .gitmodules delete mode 100644 docs.md create mode 100644 docs/Makefile create mode 100644 docs/make.bat create mode 160000 docs/source/caffeine-theme create mode 100644 docs/source/conf.py create mode 100644 docs/source/index.rst diff --git a/.gitignore b/.gitignore index 83c34cc..eaa15f7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ node_modules/ dist/ lib/ npm-debug.log -docs/ +docs/build/ examples/*.js diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a70cc7f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "docs/source/caffeine-theme"] + path = docs/source/caffeine-theme + url = git://github.com/robotlolita/caffeine-theme diff --git a/Makefile b/Makefile index 231ed8a..ddf0605 100644 --- a/Makefile +++ b/Makefile @@ -67,6 +67,9 @@ $(TEST_BLD)/%.js: $(TEST_DIR)/%.sjs --output $@ \ $< +docs/source/index.rst: src/index.sjs + $(dollphie) --input javascript --output sphinx src/index.sjs > docs/source/index.rst + # -- Tasks ------------------------------------------------------------- source: $(TGT) @@ -78,8 +81,8 @@ bundle: dist/$(PACKAGE).umd.js minify: dist/$(PACKAGE).umd.min.js -documentation: - $(dollphie) --input javascript --output markdown src/index.sjs > docs.md +documentation: docs/source/index.rst + cd docs && $(MAKE) html clean: rm -rf dist build $(LIB_DIR) diff --git a/docs.md b/docs.md deleted file mode 100644 index 34d7831..0000000 --- a/docs.md +++ /dev/null @@ -1,771 +0,0 @@ -# module: `Text.PrettyPrinting` - -- **Stability**: [![3. Stable](https://img.shields.io/badge/stability-3._Stable-green.svg?style=flat-square)](https://nodejs.org/api/documentation.html#documentation_stability_index) -- **Portability**: Portable - - - This module is an implementation of Wadler's [Pretty Printer][pp]. As described in the paper, the pretty printer is an efficient algebra that supports multiple adaptable layouts according to the available space. - -[pp]: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf - -## Dependencies - - - - - - -```js -var { Base } = require('adt-simple'); -var { curry } = require('core.lambda'); -var { trampoline, done, ternary } = require('./tramp') - -``` - - -## Data structures - - - - - -The pretty printer uses two algebras: - -The simple pretty printer is an algebra for documents that represents them as a concatenation of items. Each item may be one of the following: -```js -union Doc { - Nil, // Nothing, used as identity; - Text(String, Doc), // Some text; - Line(Number, Doc) // A line break, indented by a given amount; -} deriving (Base) - -``` - - -To allow (efficient) alternative layouts, a different algebra for documents is used, where we see things as a set of possible layouts for the same document. - -In this version we have more tags, since we add explicit representations for concatenation and nesting, and make some internal assumptions for the sake efficiency. These assumptions must be preserved whenever documents are built, and to enforce that, users are not given these structures to work with directly. -```js -union DOC { - NIL, // Nothing, used as identity; - CONCAT(DOC, DOC), // Joins two documents horizontally; - NEST(Number, DOC), // Indents a document by a given amount; - TEXT(String), // Represents plain text; - LINE, // Represents explicit line breaks; - UNION(DOC, DOC) // A set of possible layouts for a document; -} deriving (Base) - -DOC::concat = function(aDOC) { - return CONCAT(this, aDOC) -} -DOC::nest = function(indent) { - return NEST(indent, this) -} - - -``` - - -## Helpers - - - - - -### private function: `Repeat` - - -```hs -Int, String → String -``` - - - - - -Returns a String with `s` repeated `n` times. - - -```js -function repeat(n, s) { - return Array(n + 1).join(s) -} - -``` - - -### private function: `flatten` - - -```hs -DOC → DOC -``` - - - - - -Flatten replaces line breaks in a set of layouts by a single whitespace. It's defined privately so the invariants may hold. - - -```js -function flatten { - NIL => NIL, - CONCAT(a, b) => CONCAT(flatten(a), flatten(b)), - NEST(depth, a) => NEST(depth, flatten(a)), - TEXT(s) => TEXT(s), - LINE => TEXT(" "), - UNION(a, b) => flatten(a), - a => (function(){ throw new Error("No match: " + a) })(); -} - -``` - - -### private function: `best` - - -```hs -Int, Int, DOC → DOC -``` - - - - - -Best chooses the best-looking alternative from a set of possible layouts a document may have. It takes into account the available layout for the rest of the document when doing so. - - -```js -function best(width, indentation, doc) { - return trampoline(go(width, indentation, [[0, doc]])); - -``` - - -#### private function: `go` - - -```hs -Int, Int, (Int, DOC) → Doc -``` - - - - - - -```js - function go(w, k, x) { - return match x { - [] => done(Nil), - [[i, NIL], ...xs] => ternary(go, w, k, xs), - [[i, CONCAT(x, y)], ...xs] => ternary(go, w, k, [[i, x], [i, y]] +++ xs), - [[i, NEST(j, x)], ...xs] => ternary(go, w, k, [[i + j, x]] +++ xs), - [[i, TEXT(s)], ...xs] => done(Text(s, trampoline(go(w, k + s.length, xs)))), - [[i, LINE], ...xs] => done(Line(i, trampoline(go(w, i, xs)))), - [[i, UNION(x, y)], ...xs] => done(better(w, k, - trampoline(go(w, k, [[i, x]] +++ xs)), - λ[trampoline(go(w, k, [[i, y]] +++ xs))] - )) - } - } - -``` - - -#### private function: `better` - - -```hs -Int, Int, Doc, (Unit → Doc) → Doc -``` - - - - - - -```js - function better(w, k, x, y) { - return fits(w - k, x)? x : y() - } - -``` - - -#### private function: `fits` - - -```hs -Int, Doc → Boolean -``` - - - - - - -```js - function fits { - (w, x) if w < 0 => false, - (w, Nil) => true, - (w, Text(s, x)) => fits(w - s.length, x), - (w, Line(i, x)) => true - } -} - -``` - - -### private function: `layout` - - -```hs -Doc → String -``` - - - - - -Converts a simple document to a string. - - -```js -function layout { - Nil => "", - Text(s, a) => s + layout(a), - Line(i, a) => '\n' + repeat(i, ' ') + layout(a) -} - -``` - - -### private function: `horizontalConcat` - - -```hs -DOC, DOC → DOC -``` - - - - - -Concatenates two documents horizontally, separated by a single space. - - -```js -function horizontalConcat(x, y) { - return x +++ text(" ") +++ y -} - -``` - - -### private function: `verticalConcat` - - -```hs -DOC, DOC → DOC -``` - - - - - -Concatenates two documents vertically, separated by a new line. - - -```js -function verticalConcat(x, y) { - return x +++ line() +++ y -} - -``` - - -### private function: `words` - - -```hs -String → Array(String) -``` - - - - - -Converts a string into a list of words. - - -```js -function words(s) { - return s.split(/\s+/) -} - - -``` - - -## Primitives - - - - - -Wadler's pretty printer define several primitive functions for working with the two aforementioned algebras. Combinators can be easily derived from these basic building blocks (and a few area also provided). - -### function: `nil` - - -```hs -Unit → DOC -``` - - - - - -Constructs an empty document. - -**Example**: -```js - - pretty(80, nil()) // => "" - -``` - - - -```js -function nil() { - return NIL -} - -``` - - -### function: `concat` - - -```hs -DOC → DOC → DOC -``` - - - - - -Joins two documents horizontally, without any separation between them. - -**Example**: -```js - - pretty(80, concat(text('a'), text('b'))) // => "ab" - pretty(80, concat(text('a'), nil())) // => "a" - -``` - - - -```js -function concat(a, b) { - return CONCAT(a, b) -} - -``` - - -### function: `nest` - - -```hs -Int → DOC → DOC -``` - - - - - -Indents a document a given amount of spaces. - -**Example**: -```js - - pretty(80, stack([ - text('a'), - text('b'), - text('c') - ]) - // => "a\n b\n c" - -``` - - - -```js -function nest(depth, a) { - return NEST(depth, a) -} - -``` - - -### function: `text` - - -```hs -String → DOC -``` - - - - - -Represents plain text in a document. - -**Example**: -```js - - pretty(80, text("a")) // => "a" - -``` - - - -```js -function text(s) { - return TEXT(s) -} - -``` - - -### function: `line` - - -```hs -Unit → DOC -``` - - - - - -Represents a line break in a document. - -**Example**: -```js - - pretty(80, concat(concat(text("a"), line()), text("b")) - // => "a\nb" - -``` - - - -```js -function line() { - return LINE -} - -``` - - -### function: `group` - - -```hs -DOC → DOC -``` - - - - - -Creates a set of alternative layouts for the document. - -**Example**: -```js - - pretty(5, group(stack([text('foo'), text('bar')]))) - // => "foo\nbar" - - pretty(10, group(stack([text('foo'), text('bar')]))) - // => "foo bar" - -``` - - - -```js -function group(a) { - return UNION(flatten(a), a) -} - -``` - - -## Conversions - - - - - -### function: `pretty` - - -```hs -Int → DOC → String -``` - - - - - -Returns the best representation of a document for the given amount of horizontal space available, as a String. - -**Example**: -```js - - pretty(80, spread([text('hello'), text('world')])) - // => "hello world" - -``` - - - -```js -function pretty(width, doc) { - return layout(best(width, 0, doc)) -} - -``` - - -## Combinators - - - - - -### function: `foldDoc` - - -```hs -(DOC, DOC → DOC) → Array(DOC) → DOC -``` - - - - - -Allows folding over pairs of documents (similar to a catamorphism). - - -```js -function foldDoc { - (f, []) => nil(), - (f, [x]) => x, - (f, [x, ...xs]) => f(x, foldDoc(f, xs)) -} - -``` - - -### function: `spread` - - -```hs -Array(DOC) → DOC -``` - - - - - -Lays out a series of documents horizontally, with each document separated by a single space. - -**Example**: -```js - - pretty(80, spread([text('foo'), text('bar')])) - // => "foo bar" - -``` - - - -```js -function spread(xs) { - return foldDoc(horizontalConcat, xs) -} - -``` - - -### function: `stack` - - -```hs -Array(DOC) → DOC -``` - - - - - -Lays out a series of documents vertically, with each document separated by a single new line. - -**Example**: -```js - - pretty(80, stack([text('foo'), text('bar')])) - // => "foo\nbar" - -``` - - - -```js -function stack(xs) { - return foldDoc(verticalConcat, xs) -} - -``` - - -### function: `bracket` - - -```hs -Int → DOC → DOC → DOC → DOC -``` - - - - - -**Example**: -```js - - pretty(5, bracket(2, '[', stack([ - text('a'), text('b'), text('c') - ]), ']')) - // => "[ \n a\n b\n c \n ]" - -``` - - - -```js -function bracket(indent, left, x, right) { - return group(text(left) - +++ nest(indent, line() +++ x) - +++ line() - +++ text(right)) -} - -``` - - -### function: `join` - - -```hs -DOC → DOC → DOC -``` - - - - - -Joins two documents together, either by separating with a single horizontal space or a single new line. - - -```js -function join(x, y) { - return x +++ UNION(text(" "), line()) +++ y -} - -``` - - -### function: `fillWords` - - -```hs -String → DOC -``` - - - - - -Makes the best use of the available space for laying out words, either separated by a space or a new line. - - -```js -function fillWords(s) { - return foldDoc(join, words(s).map(text)) -} - -``` - - -### function: `fill` - - -```hs -Array(DOC) → DOC -``` - - - - - -Makes the best use of the available space for layout out a series of documents, either separated by a space or a new line. - - -```js -function fill { - [] => nil(), - [x] => x, - [x, y, ...zs] => UNION(horizontalConcat(flatten(x), fill([flatten(y)] +++ zs)), - verticalConcat(x, fill([y] +++ zs))) -} - - -``` - - -## Exports - - - - - - -```js -module.exports = { - nil : nil, - concat : curry(2, concat), - nest : curry(2, nest), - text : text, - line : line, - group : group, - pretty : curry(2, pretty), - foldDoc : curry(2, foldDoc), - spread : spread, - stack : stack, - bracket : curry(4, bracket), - join : curry(2, join), - fillWords : fillWords, - fill : fill -} - -``` - - diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..812b68d --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,177 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = build + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/TextPrettyPrinting.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/TextPrettyPrinting.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/TextPrettyPrinting" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/TextPrettyPrinting" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..b7d94fa --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,242 @@ +@ECHO OFF + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set BUILDDIR=build +set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% source +set I18NSPHINXOPTS=%SPHINXOPTS% source +if NOT "%PAPER%" == "" ( + set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% + set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% +) + +if "%1" == "" goto help + +if "%1" == "help" ( + :help + echo.Please use `make ^` where ^ is one of + echo. html to make standalone HTML files + echo. dirhtml to make HTML files named index.html in directories + echo. singlehtml to make a single large HTML file + echo. pickle to make pickle files + echo. json to make JSON files + echo. htmlhelp to make HTML files and a HTML help project + echo. qthelp to make HTML files and a qthelp project + echo. devhelp to make HTML files and a Devhelp project + echo. epub to make an epub + echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter + echo. text to make text files + echo. man to make manual pages + echo. texinfo to make Texinfo files + echo. gettext to make PO message catalogs + echo. changes to make an overview over all changed/added/deprecated items + echo. xml to make Docutils-native XML files + echo. pseudoxml to make pseudoxml-XML files for display purposes + echo. linkcheck to check all external links for integrity + echo. doctest to run all doctests embedded in the documentation if enabled + goto end +) + +if "%1" == "clean" ( + for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i + del /q /s %BUILDDIR%\* + goto end +) + + +%SPHINXBUILD% 2> nul +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "html" ( + %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/html. + goto end +) + +if "%1" == "dirhtml" ( + %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. + goto end +) + +if "%1" == "singlehtml" ( + %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. + goto end +) + +if "%1" == "pickle" ( + %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the pickle files. + goto end +) + +if "%1" == "json" ( + %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the JSON files. + goto end +) + +if "%1" == "htmlhelp" ( + %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run HTML Help Workshop with the ^ +.hhp project file in %BUILDDIR%/htmlhelp. + goto end +) + +if "%1" == "qthelp" ( + %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run "qcollectiongenerator" with the ^ +.qhcp project file in %BUILDDIR%/qthelp, like this: + echo.^> qcollectiongenerator %BUILDDIR%\qthelp\TextPrettyPrinting.qhcp + echo.To view the help file: + echo.^> assistant -collectionFile %BUILDDIR%\qthelp\TextPrettyPrinting.ghc + goto end +) + +if "%1" == "devhelp" ( + %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. + goto end +) + +if "%1" == "epub" ( + %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The epub file is in %BUILDDIR%/epub. + goto end +) + +if "%1" == "latex" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "latexpdf" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + cd %BUILDDIR%/latex + make all-pdf + cd %BUILDDIR%/.. + echo. + echo.Build finished; the PDF files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "latexpdfja" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + cd %BUILDDIR%/latex + make all-pdf-ja + cd %BUILDDIR%/.. + echo. + echo.Build finished; the PDF files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "text" ( + %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The text files are in %BUILDDIR%/text. + goto end +) + +if "%1" == "man" ( + %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The manual pages are in %BUILDDIR%/man. + goto end +) + +if "%1" == "texinfo" ( + %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. + goto end +) + +if "%1" == "gettext" ( + %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The message catalogs are in %BUILDDIR%/locale. + goto end +) + +if "%1" == "changes" ( + %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes + if errorlevel 1 exit /b 1 + echo. + echo.The overview file is in %BUILDDIR%/changes. + goto end +) + +if "%1" == "linkcheck" ( + %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck + if errorlevel 1 exit /b 1 + echo. + echo.Link check complete; look for any errors in the above output ^ +or in %BUILDDIR%/linkcheck/output.txt. + goto end +) + +if "%1" == "doctest" ( + %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest + if errorlevel 1 exit /b 1 + echo. + echo.Testing of doctests in the sources finished, look at the ^ +results in %BUILDDIR%/doctest/output.txt. + goto end +) + +if "%1" == "xml" ( + %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The XML files are in %BUILDDIR%/xml. + goto end +) + +if "%1" == "pseudoxml" ( + %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. + goto end +) + +:end diff --git a/docs/source/caffeine-theme b/docs/source/caffeine-theme new file mode 160000 index 0000000..639f5c1 --- /dev/null +++ b/docs/source/caffeine-theme @@ -0,0 +1 @@ +Subproject commit 639f5c166fc49579dd6ff9de1b8d61a9b0d3a8ee diff --git a/docs/source/conf.py b/docs/source/conf.py new file mode 100644 index 0000000..7d02097 --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,265 @@ +# -*- coding: utf-8 -*- +# +# Text.PrettyPrinting documentation build configuration file, created by +# sphinx-quickstart on Sun Apr 19 11:52:07 2015. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys +import os + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +#sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ------------------------------------------------ + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'sphinx.ext.mathjax', +] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'Text.PrettyPrinting' +copyright = u'2015, Quildreen Motta' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '1.0.1' +# The full version, including alpha/beta/rc tags. +release = '1.0.1' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +#language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = [] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'autumn' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +#keep_warnings = False + + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'caffeine' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] +html_theme_path = ['caffeine-theme'] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +#html_extra_path = [] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} +html_sidebars = { "**": ['searchbox.html', 'localtoc.html'] + , 'search': [] + , 'genindex': [] + } + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'TextPrettyPrintingdoc' + + +# -- Options for LaTeX output --------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +#'preamble': '', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + ('index', 'TextPrettyPrinting.tex', u'Text.PrettyPrinting Documentation', + u'Quildreen Motta', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'textprettyprinting', u'Text.PrettyPrinting Documentation', + [u'Quildreen Motta'], 1) +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'TextPrettyPrinting', u'Text.PrettyPrinting Documentation', + u'Quildreen Motta', 'TextPrettyPrinting', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +#texinfo_no_detailmenu = False diff --git a/docs/source/index.rst b/docs/source/index.rst new file mode 100644 index 0000000..2e45820 --- /dev/null +++ b/docs/source/index.rst @@ -0,0 +1,1541 @@ +.. This file is auto-generated from Dollphie. + + + + +Module: ``Text.PrettyPrinting`` +******************************* + + + + +.. module:: Text.PrettyPrinting + :synopsis: + Compositional pretty printer supporting multiple layouts. + :platform: + ECMAScript 5 + + + + + +:Stability: + stable +:Portability: + Portable + + + + +This module is an implementation of Wadler's `Pretty Printer`_. +As described in the paper, the pretty printer is an efficient +algebra that supports multiple adaptable layouts according to the +available space. + + + +.. _`Pretty Printer`: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf + + + + + +Dependencies +============ + + + +.. code-block:: js + + + var { Base } = require('adt-simple'); + var { curry } = require('core.lambda'); + var { Done, trampoline, done, ternary, binary, nary } = require('./tramp') + + + + + + + +Data structures +=============== + +The pretty printer uses two algebras: + + +The simple pretty printer is an algebra for documents that +represents them as a concatenation of items. Each item may be one +of the following: + + +.. code-block:: js + + + union Doc { + Nil, // Nothing, used as identity; + Text(String, Doc), // Some text; + Line(Number, Doc) // A line break, indented by a given amount; + } deriving (Base) + + + + + +To allow (efficient) alternative layouts, a different algebra +for documents is used, where we see things as a set of possible +layouts for the same document. + + +In this version we have more tags, since we add explicit representations +for concatenation and nesting, and make some internal assumptions +for the sake efficiency. These assumptions must be preserved whenever +documents are built, and to enforce that, users are not given these +structures to work with directly. + + +.. code-block:: js + + + union DOC { + NIL, // Nothing, used as identity; + CONCAT(DOC, DOC), // Joins two documents horizontally; + NEST(Number, DOC), // Indents a document by a given amount; + TEXT(String), // Represents plain text; + LINE, // Represents explicit line breaks; + UNION(DOC, DOC) // A set of possible layouts for a document; + } deriving (Base) + + DOC::concat = function(aDOC) { + return CONCAT(this, aDOC) + } + DOC::nest = function(indent) { + return NEST(indent, this) + } + + + + + + + + +Helpers +======= + +.. rst-class:: hidden-heading + + + + +Repeat() +-------- + + + + +.. function:: Repeat() + + + + + .. code-block:: haskell + + + Int, String → String + + + + + + + + Returns a String with `s` repeated `n` times. + + + + + + .. code-block:: js + + + function repeat(n, s) { + return Array(n + 1).join(s) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +flatten() +--------- + + + + +.. function:: flatten() + + + + + .. code-block:: haskell + + + DOC → DOC + + + + + + + + Flatten replaces line breaks in a set of layouts by a single + whitespace. It's defined privately so the invariants may hold. + + + + + + .. code-block:: js + + + function flatten { + NIL => NIL, + CONCAT(a, b) => CONCAT(flatten(a), flatten(b)), + NEST(depth, a) => NEST(depth, flatten(a)), + TEXT(s) => TEXT(s), + LINE => TEXT(" "), + UNION(a, b) => flatten(a), + a => (function(){ throw new Error("No match: " + a) })(); + } + + + + + + + +.. rst-class:: hidden-heading + + + + +best() +------ + + + + +.. function:: best() + + + + + .. code-block:: haskell + + + Int, Int, DOC → DOC + + + + + + + + Best chooses the best-looking alternative from a set of possible + layouts a document may have. It takes into account the available + layout for the rest of the document when doing so. + + + + + + .. code-block:: js + + + function best(width, indentation, doc) { + return trampoline(go(width, indentation, [[0, doc]])); + + + + + + .. rst-class:: hidden-heading + + + + + .. rubric:: go() + + + + + .. function:: go() + + + + + .. code-block:: haskell + + + Int, Int, (Int, DOC) → Doc + + + + + + + + + + + .. code-block:: js + + + function go(w, k, x) { + return match x { + [] => done(Nil), + [[i, NIL], ...xs] => ternary(go, w, k, xs), + [[i, CONCAT(x, y)], ...xs] => ternary(go, w, k, [[i, x], [i, y]] +++ xs), + [[i, NEST(j, x)], ...xs] => ternary(go, w, k, [[i + j, x]] +++ xs), + [[i, TEXT(s)], ...xs] => binary(_text, s, go(w, k + s.length, xs)), + [[i, LINE], ...xs] => binary(_line, i, go(w, i, xs)), + [[i, UNION(x, y)], ...xs] => better(w, k, + ternary(go, w, k, [[i, x]] +++ xs), + λ[ternary(go, w, k, [[i, y]] +++ xs)] + ) + } + } + + + + + + + + .. rst-class:: hidden-heading + + + + + .. rubric:: _text() + + + + + .. function:: _text() + + + + + .. code-block:: haskell + + + String, Continuation + + + + + + + + Wraps the Text() constructor for trampolining. + + + + .. code-block:: js + + + function _text(s, g) { + if (g instanceof Done) { + return done(Text(s, g.value)) + } else { + return binary(_text, s, g.apply()) + } + } + + + + + + + + .. rst-class:: hidden-heading + + + + + .. rubric:: _line() + + + + + .. function:: _line() + + + + + .. code-block:: haskell + + + Int, Continuation + + + + + + + + Wraps the Line() constructor for trampolining. + + + + .. code-block:: js + + + function _line(i, g) { + if (g instanceof Done) { + return done(Line(i, g.value)) + } else { + return binary(_line, i, g.apply()) + } + } + + + + + + + + .. rst-class:: hidden-heading + + + + + .. rubric:: go() + + + + + .. function:: go() + + + + + .. code-block:: haskell + + + Int, Int, Doc, (Unit → Doc) → Doc + + + + + + + + Chooses the best-looking of two styles. ``y`` is thunked to avoid + costly computations. + + + + .. code-block:: js + + + function better(w, k, x, y) { + if (x instanceof Done) { + return fits(w - k, x.value)? done(x.value) : y() + } else { + return nary(better, [w, k, x.apply(), y]) + } + } + + + + + + + + .. rst-class:: hidden-heading + + + + + .. rubric:: fits() + + + + + .. function:: fits() + + + + + .. code-block:: haskell + + + Int, Doc → Boolean + + + + + + + + Checks if some document fits in the rest of the line. + + + + .. code-block:: js + + + function fits { + (w, x) if w < 0 => false, + (w, Nil) => true, + (w, Text(s, x)) => fits(w - s.length, x), + (w, Line(i, x)) => true, + (w, x) => (function(){ throw new Error("No match: " + show(w) + ", " + show(x)) })() + } + } + + + + + + + + + +.. rst-class:: hidden-heading + + + + +layout() +-------- + + + + +.. function:: layout() + + + + + .. code-block:: haskell + + + Doc → String + + + + + + + + Converts a simple document to a string. + + + + + + .. code-block:: js + + + function layout { + Nil => "", + Text(s, a) => s + layout(a), + Line(i, a) => '\n' + repeat(i, ' ') + layout(a) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +horizontalConcat() +------------------ + + + + +.. function:: horizontalConcat() + + + + + .. code-block:: haskell + + + DOC, DOC → DOC + + + + + + + + Concatenates two documents horizontally, separated by a single space. + + + + + + .. code-block:: js + + + function horizontalConcat(x, y) { + return x +++ text(" ") +++ y + } + + + + + + + +.. rst-class:: hidden-heading + + + + +verticalConcat() +---------------- + + + + +.. function:: verticalConcat() + + + + + .. code-block:: haskell + + + DOC, DOC → DOC + + + + + + + + Concatenates two documents vertically, separated by a new line. + + + + + + .. code-block:: js + + + function verticalConcat(x, y) { + return x +++ line() +++ y + } + + + + + + + +.. rst-class:: hidden-heading + + + + +words() +------- + + + + +.. function:: words() + + + + + .. code-block:: haskell + + + String → Array(String) + + + + + + + + Converts a string into a list of words. + + + + + + .. code-block:: js + + + function words(s) { + return s.split(/\s+/) + } + + + + + + + + + + +Primitives +========== + +Wadler's pretty printer define several primitive functions for working +with the two aforementioned algebras. Combinators can be easily +derived from these basic building blocks (and a few area also provided). + + +.. rst-class:: hidden-heading + + + + +nil() +----- + + + + +.. function:: nil() + + + + + .. code-block:: haskell + + + Unit → DOC + + + + + + + + Constructs an empty document. + + + **Example**: + + + .. code-block:: js + + + + pretty(80, nil()) // => "" + + + + + + + + .. code-block:: js + + + function nil() { + return NIL + } + + + + + + + +.. rst-class:: hidden-heading + + + + +concat() +-------- + + + + +.. function:: concat() + + + + + .. code-block:: haskell + + + DOC → DOC → DOC + + + + + + + + Joins two documents horizontally, without any separation between them. + + + **Example**: + + + .. code-block:: js + + + + pretty(80, concat(text('a'), text('b'))) // => "ab" + pretty(80, concat(text('a'), nil())) // => "a" + + + + + + + + .. code-block:: js + + + function concat(a, b) { + return CONCAT(a, b) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +nest() +------ + + + + +.. function:: nest() + + + + + .. code-block:: haskell + + + Int → DOC → DOC + + + + + + + + Indents a document a given amount of spaces. + + + **Example**: + + + .. code-block:: js + + + + pretty(80, stack([ + text('a'), + text('b'), + text('c') + ]) + // => "a\n b\n c" + + + + + + + + .. code-block:: js + + + function nest(depth, a) { + return NEST(depth, a) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +text() +------ + + + + +.. function:: text() + + + + + .. code-block:: haskell + + + String → DOC + + + + + + + + Represents plain text in a document. + + + **Example**: + + + .. code-block:: js + + + + pretty(80, text("a")) // => "a" + + + + + + + + .. code-block:: js + + + function text(s) { + return TEXT(s) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +line() +------ + + + + +.. function:: line() + + + + + .. code-block:: haskell + + + Unit → DOC + + + + + + + + Represents a line break in a document. + + + **Example**: + + + .. code-block:: js + + + + pretty(80, concat(concat(text("a"), line()), text("b")) + // => "a\nb" + + + + + + + + .. code-block:: js + + + function line() { + return LINE + } + + + + + + + +.. rst-class:: hidden-heading + + + + +group() +------- + + + + +.. function:: group() + + + + + .. code-block:: haskell + + + DOC → DOC + + + + + + + + Creates a set of alternative layouts for the document. + + + **Example**: + + + .. code-block:: js + + + + pretty(5, group(stack([text('foo'), text('bar')]))) + // => "foo\nbar" + + pretty(10, group(stack([text('foo'), text('bar')]))) + // => "foo bar" + + + + + + + + .. code-block:: js + + + function group(a) { + return UNION(flatten(a), a) + } + + + + + + + + + +Conversions +=========== + +.. rst-class:: hidden-heading + + + + +pretty() +-------- + + + + +.. function:: pretty() + + + + + .. code-block:: haskell + + + Int → DOC → String + + + + + + + + Returns the best representation of a document for the given amount + of horizontal space available, as a String. + + + **Example**: + + + .. code-block:: js + + + + pretty(80, spread([text('hello'), text('world')])) + // => "hello world" + + + + + + + + .. code-block:: js + + + function pretty(width, doc) { + return layout(best(width, 0, doc)) + } + + + + + + + + + +Combinators +=========== + +.. rst-class:: hidden-heading + + + + +foldDoc() +--------- + + + + +.. function:: foldDoc() + + + + + .. code-block:: haskell + + + (DOC, DOC → DOC) → Array(DOC) → DOC + + + + + + + + Allows folding over pairs of documents (similar to a catamorphism). + + + + + .. code-block:: js + + + function foldDoc { + (f, []) => nil(), + (f, [x]) => x, + (f, [x, ...xs]) => f(x, foldDoc(f, xs)) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +spread() +-------- + + + + +.. function:: spread() + + + + + .. code-block:: haskell + + + Array(DOC) → DOC + + + + + + + + Lays out a series of documents horizontally, with each document + separated by a single space. + + + **Example**: + + + .. code-block:: js + + + + pretty(80, spread([text('foo'), text('bar')])) + // => "foo bar" + + + + + + + + .. code-block:: js + + + function spread(xs) { + return foldDoc(horizontalConcat, xs) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +stack() +------- + + + + +.. function:: stack() + + + + + .. code-block:: haskell + + + Array(DOC) → DOC + + + + + + + + Lays out a series of documents vertically, with each document + separated by a single new line. + + + **Example**: + + + .. code-block:: js + + + + pretty(80, stack([text('foo'), text('bar')])) + // => "foo\nbar" + + + + + + + + .. code-block:: js + + + function stack(xs) { + return foldDoc(verticalConcat, xs) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +bracket() +--------- + + + + +.. function:: bracket() + + + + + .. code-block:: haskell + + + Int → DOC → DOC → DOC → DOC + + + + + + + + **Example**: + + + .. code-block:: js + + + + pretty(5, bracket(2, '[', stack([ + text('a'), text('b'), text('c') + ]), ']')) + // => "[\n a\n b\n c \n]" + + + + + + + + .. code-block:: js + + + function bracket(indent, left, x, right) { + return group(text(left) + +++ nest(indent, line() +++ x) + +++ line() + +++ text(right)) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +join() +------ + + + + +.. function:: join() + + + + + .. code-block:: haskell + + + DOC → DOC → DOC + + + + + + + + Joins two documents together, either by separating with a single + horizontal space or a single new line. + + + + + .. code-block:: js + + + function join(x, y) { + return x +++ UNION(text(" "), line()) +++ y + } + + + + + + + +.. rst-class:: hidden-heading + + + + +fillWords() +----------- + + + + +.. function:: fillWords() + + + + + .. code-block:: haskell + + + String → DOC + + + + + + + + Makes the best use of the available space for laying out words, + either separated by a space or a new line. + + + + + .. code-block:: js + + + function fillWords(s) { + return foldDoc(join, words(s).map(text)) + } + + + + + + + +.. rst-class:: hidden-heading + + + + +fill() +------ + + + + +.. function:: fill() + + + + + .. code-block:: haskell + + + Array(DOC) → DOC + + + + + + + + Makes the best use of the available space for layout out a series + of documents, either separated by a space or a new line. + + + + + .. code-block:: js + + + function fill { + [] => nil(), + [x] => x, + [x, y, ...zs] => UNION(horizontalConcat(flatten(x), fill([flatten(y)] +++ zs)), + verticalConcat(x, fill([y] +++ zs))) + } + + + + + + + + + + +Exports +======= + + + +.. code-block:: js + + + module.exports = { + nil : nil, + concat : curry(2, concat), + nest : curry(2, nest), + text : text, + line : line, + group : group, + pretty : curry(2, pretty), + foldDoc : curry(2, foldDoc), + spread : spread, + stack : stack, + bracket : curry(4, bracket), + join : curry(2, join), + fillWords : fillWords, + fill : fill + } + + + + +