packages feed

lucid-xstatic-0.1.0: src/Lucid/XStatic.hs

{-# LANGUAGE OverloadedStrings #-}

{- | Add XStaticFile to Html document.

@
indexHtml :: Html ()
indexHtml = do
    doctypehtml_ do
        head_ do
            meta_ [charset_ "utf-8"]
            meta_ [name_ "viewport", content_ "width=device-width, initial-scale=1.0"]
            xstaticScripts xfiles
  where
    xfiles = XStatic.htmx : XStatic.xterm
@
-}
module Lucid.XStatic (xstaticScripts) where

import Data.Foldable (traverse_)
import Data.Text.Encoding
import Lucid
import XStatic

-- | Adds 'script_' and 'link_' for javascript and css files.
xstaticScripts :: [XStaticFile] -> Html ()
xstaticScripts = traverse_ xrender
  where
    xrender :: XStaticFile -> Html ()
    xrender xf =
        let src = "/xstatic" <> decodeUtf8 (xfPath xf)
         in case xfType xf of
                "application/javascript" -> with (script_ mempty) [src_ src]
                "text/css" -> link_ [href_ src, rel_ "stylesheet"]
                _ -> pure ()