render package:prettyprinter

We can now wrap our stack machine definition from renderStackMachine in a nicer interface; on successful conversion, we run the builder to give us the final Text, and before we do that we check that the style stack is empty (i.e. there are no unmatched style applications) after the machine is run. This function does only a bit of plumbing around renderStackMachine, and is the main API function of a stack machine renderer. The tree renderer equivalent to this is render.
To render the HTML, we first convert the SimpleDocStream to the SimpleDocTree format, which makes enveloping sub-documents in markup easier. This function is the entry main API function of the renderer; as such, it is only glue for the internal functions. This is similar to render from the stack machine tutorial in its purpose.
Render a SimpleDocStream to a ShowS, useful to write Show instances based on the prettyprinter.
instance Show MyType where
showsPrec _ = renderShowS . layoutPretty defaultLayoutOptions . pretty
Render a SimpleDocStream to a String.
(renderIO h sdoc) writes sdoc to the file h.
>>> renderIO System.IO.stdout (layoutPretty defaultLayoutOptions "hello\nworld")
hello
world
This function is more efficient than hPutStr h (renderStrict sdoc), since it writes to the handle directly, skipping the intermediate Text representation.
(renderLazy sdoc) takes the output sdoc from a rendering function and transforms it to lazy text.
>>> let render = TL.putStrLn . renderLazy . layoutPretty defaultLayoutOptions

>>> let doc = "lorem" <+> align (vsep ["ipsum dolor", parens "foo bar", "sit amet"])

>>> render doc
lorem ipsum dolor
(foo bar)
sit amet
(renderStrict sdoc) takes the output sdoc from a rendering function and transforms it to strict text.
The StackMachine type defines a stack machine suitable for many rendering needs. It has two auxiliary parameters: the type of the end result, and the type of the document’s annotations. Most StackMachine creations will look like this definition: a recursive walk through the SimpleDocStream, pushing styles on the stack and popping them off again, and writing raw output. The equivalent to this in the tree based rendering approach is renderTree.
Render a SimpleDocTree to a Builder; this is the workhorse of the tree-based rendering approach, and equivalent to renderStackMachine in the stack machine rendering tutorial.
Simplest possible tree-based renderer. For example, here is a document annotated with (), and the behaviour is to surround annotated regions with »>>>« and »<<<«:
>>> let doc = "hello" <+> annotate () "world" <> "!"

>>> let stdoc = treeForm (layoutPretty defaultLayoutOptions doc)

>>> T.putStrLn (renderSimplyDecorated id (\() x -> ">>>" <> x <> "<<<") stdoc)
hello >>>world<<<!
Version of renderSimplyDecoratedA that allows for Applicative effects.
Simplest possible stack-based renderer. For example, here is a document annotated with (), and the behaviour is to write »>>>« at the beginning, and »<<<« at the end of the annotated region:
>>> let doc = "hello" <+> annotate () "world" <> "!"

>>> let sdoc = layoutPretty defaultLayoutOptions doc

>>> T.putStrLn (renderSimplyDecorated id (\() -> ">>>") (\() -> "<<<") sdoc)
hello >>>world<<<!
The monoid will be concatenated in a right associative fashion.
Version of renderSimplyDecoratedA that allows for Applicative effects.
This module shows how to write a custom prettyprinter backend, based on a tree representation of a SimpleDocStream. For a stack machine approach, which may be more suitable for certain output formats, see Prettyprinter.Render.Tutorials.StackMachineTutorial. Rendering to HTML, particularly using libraries such as blaze-html or lucid, is one important use case of tree-based rendering. The module is written to be readable top-to-bottom in both Haddock and raw source form.