the-sett/elm-pretty-printer/3.3.1

Pretty.Renderer

Custom rendering for syntax highlighting or structured output.

Use Pretty.pretty for plain text. Use this module to:

  • Render to HTML with syntax highlighting
  • Handle tagged strings differently based on their tags
  • Produce any accumulator-based output format
type alias Renderer t a b =
{ init : a
, tagged : t -> String -> a -> a
, untagged : String -> a -> a
, newline : a -> a
, outer : a -> b
}

Accumulator-based renderer, similar to foldl.

  • init: Starting accumulator value
  • tagged: Handle tagged text (for syntax highlighting)
  • untagged: Handle untagged text and indentation
  • newline: Handle line breaks (add \n here if needed)
  • outer: Transform final accumulator to output type

Text is processed left-to-right; reverse lists if accumulating in order.

htmlRenderer : Renderer Tag (List (Html msg)) (Html msg)
htmlRenderer =
    { init = []
    , tagged = \tag text acc -> styledSpan tag text :: acc
    , untagged = \text acc -> Html.text text :: acc
    , newline = \acc -> Html.br [] [] :: acc
    , outer = \acc -> Html.pre [] (List.reverse acc)
    }
pretty : Int -> Renderer t a b -> Doc t -> b

Renders a document using a custom Renderer.

pretty 80 htmlRenderer doc
{ pageWidth : Int
, ribbonWidth : Int
}
-> Renderer t a b
-> Doc t
-> b

Pretty prints with both page width and ribbon width constraints.

The ribbon width limits non-indentation characters per line. At indentation level i, the effective width is min(pageWidth, i + ribbonWidth).