prettyprinter-1.2.1: A modern, easy to use, well-documented, extensible pretty-printer.

Safe Haskell | Safe
Language | Haskell2010

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

- data Color
  - = Red
  - | Green
  - | Blue
- data SimpleHtml
  - = Bold
  - | Italics
  - | Color Color
  - | Paragraph
  - | Headline
- bold :: Doc SimpleHtml -> Doc SimpleHtml
- italics :: Doc SimpleHtml -> Doc SimpleHtml
- paragraph :: Doc SimpleHtml -> Doc SimpleHtml
- headline :: Doc SimpleHtml -> Doc SimpleHtml
- color :: Color -> Doc SimpleHtml -> Doc SimpleHtml
- render :: SimpleDocStream SimpleHtml -> Text
- renderTree :: SimpleDocTree SimpleHtml -> Builder
- encloseInTagFor :: SimpleHtml -> Builder -> Builder

# Documentation

data Color Source #

Constructors

Red |
Green |
Blue |

data SimpleHtml Source #

Constructors

Bold |
Italics |
Color Color |
Paragraph |
Headline |

bold :: Doc SimpleHtml -> Doc SimpleHtml Source #

italics :: Doc SimpleHtml -> Doc SimpleHtml Source #

paragraph :: Doc SimpleHtml -> Doc SimpleHtml Source #

headline :: Doc SimpleHtml -> Doc SimpleHtml Source #

color :: Color -> Doc SimpleHtml -> Doc SimpleHtml Source #

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.