prettyprinter-lucid: A prettyprinter backend for lucid

[ bsd3, library, web ] [ Propose Tags ]

Provides utility functions for rendering pretty HTML.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1, 0.2.0, 0.2.0.1 (info)
Change log CHANGELOG.md
Dependencies base (>=4.12 && <4.21), lucid (>=2.9.12 && <2.12), prettyprinter (>=1.7.0 && <1.8), text (>=1.2.3 && <1.3 || >=2.0 && <2.2) [details]
License BSD-3-Clause
Author George Thomas
Maintainer georgefsthomas@gmail.com
Category Web
Home page https://github.com/georgefst/prettyprinter-lucid
Source repo head: git clone git://github.com/georgefst/prettyprinter-lucid.git
Uploaded by GeorgeThomas at 2024-09-14T19:29:14Z
Distributions NixOS:0.1.0.1
Downloads 401 total (49 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-09-14 [all 1 reports]

Readme for prettyprinter-lucid-0.2.0.1

[back to package description]

Pretty HTML

This is just a tiny library enabling lucid to be used as a backend for prettyprinter. This makes it easy to programmatically generate HTML documents with complex nested styling.

Example

This code, adapted from prettyprinter's main example, produces the following:

png

{- cabal:
build-depends: base, text, lucid, prettyprinter, prettyprinter-lucid
-}
{-# LANGUAGE GHC2021 #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wall #-}

module Main (main) where

import Data.Text (Text)
import Lucid
import Lucid.Base
import Prettyprinter
import Prettyprinter.Lucid
import Prettyprinter.Render.Util.SimpleDocTree (treeForm)

main :: IO ()
main = renderToFile "example.html" $ render 20 doc

render :: Int -> Doc (Html () -> Html ()) -> Html ()
render width' =
    div_ [style "background-color: #1F1F1F; padding: 0.2rem; padding-left: 0.4rem"]
        . renderHtml
        . treeForm
        . layoutPretty
            defaultLayoutOptions{layoutPageWidth = AvailablePerLine width' 1}

doc :: Doc (Html () -> Html ())
doc =
    annotate (span_ [style "font-weight: bold"]) $
        prettyprintDeclaration
            "example"
            ["Int", "Bool", "Char", "IO ()"]
  where
    prettyprintDeclaration n tys =
        colour "#DCDCAA" (pretty @Text n) <+> prettyprintType tys
    prettyprintType =
        align
            . sep
            . zipWith (<+>) (map (colour "#CC524B") $ "::" : repeat "->")
            . map (colour "569CD6")
    colour c =
        annotate (span_ [style $ "color: " <> c])

style :: Text -> Attribute
style = makeAttribute "style"