prettyprinter-1.1.1: A modern, easy to use, well-documented, extensible prettyprinter.

Safe HaskellSafe
LanguageHaskell2010

Data.Text.Prettyprint.Doc.Render.Tutorials.TreeRenderingTutorial

Description

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 Data.Text.Prettyprint.Doc.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.

Synopsis

Documentation

The type of available markup

First, we define a set of valid annotations must be defined, with the goal of defining a Doc SimpleHtml. We will later define how to convert this to the output format (Text).

data Color Source #

Constructors

Red 
Green 
Blue 

Conveinence definitions

render :: SimpleDocStream SimpleHtml -> Text Source #

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.

renderTree :: SimpleDocTree SimpleHtml -> Builder Source #

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.

encloseInTagFor :: SimpleHtml -> Builder -> Builder Source #

Convert a SimpleHtml to a function that encloses a Builder in HTML tags. This is where the translation of style to raw output happens.

Example invocation

We can now render an example document using our definitions:

>>> :set -XOverloadedStrings
>>> import qualified Data.Text.Lazy.IO as TL
>>> :{
>>> let go = TL.putStrLn . render . layoutPretty defaultLayoutOptions
>>> in go (vsep
>>> [ headline "Example document"
>>> , paragraph ("This is a" <+> color Red "paragraph" <> comma)
>>> , paragraph ("and" <+> bold "this text is bold.")
>>> ])
>>> :}
<h1>Example document</h1>
<p>This is a <span style="color: #f00">paragraph</span>,</p>
<p>and <strong>this text is bold.</strong></p>