| 1 |
{-# LANGUAGE CPP #-} |
| 2 |
{-# LANGUAGE BangPatterns #-} |
| 3 |
{-# LANGUAGE FunctionalDependencies #-} |
| 4 |
{-# LANGUAGE FlexibleInstances #-} |
| 5 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 6 |
{-# LANGUAGE GADTs #-} |
| 7 |
{-# LANGUAGE TypeOperators #-} |
| 8 |
|
| 9 |
-- Search for UndecidableInstances to see why this is needed |
| 10 |
{-# LANGUAGE UndecidableInstances #-} |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
module Lucid.Base |
| 15 |
(-- * Rendering |
| 16 |
renderText |
| 17 |
,renderBS |
| 18 |
,renderTextT |
| 19 |
,renderBST |
| 20 |
,renderToFile |
| 21 |
-- * Running |
| 22 |
,execHtmlT |
| 23 |
,evalHtmlT |
| 24 |
,runHtmlT |
| 25 |
,relaxHtmlT |
| 26 |
,commuteHtmlT |
| 27 |
-- * Combinators |
| 28 |
,makeElement |
| 29 |
,makeElementNoEnd |
| 30 |
,makeXmlElementNoEnd |
| 31 |
,makeAttribute |
| 32 |
-- * Types |
| 33 |
,Html |
| 34 |
,HtmlT(HtmlT) |
| 35 |
,Attribute(..) |
| 36 |
-- * Classes |
| 37 |
,Term(..) |
| 38 |
,TermRaw(..) |
| 39 |
,ToHtml(..) |
| 40 |
,With(..)) |
| 41 |
where |
| 42 |
|
| 43 |
import Blaze.ByteString.Builder (Builder) |
| 44 |
import qualified Blaze.ByteString.Builder as Blaze |
| 45 |
import qualified Blaze.ByteString.Builder.Html.Utf8 as Blaze |
| 46 |
import Control.Applicative |
| 47 |
import Control.Monad |
| 48 |
import Control.Monad.Morph (MFunctor(..)) |
| 49 |
import Control.Monad.Reader (MonadReader(..)) |
| 50 |
import Control.Monad.IO.Class (MonadIO(..)) |
| 51 |
import Control.Monad.Fix (MonadFix(..)) |
| 52 |
import Control.Monad.Trans (MonadTrans(..)) |
| 53 |
import Control.Monad.Error.Class (MonadError(..)) |
| 54 |
import Control.Monad.State.Class (MonadState(..)) |
| 55 |
import Control.Monad.Writer.Class (MonadWriter(..)) |
| 56 |
import Data.ByteString.Lazy (ByteString) |
| 57 |
import qualified Data.ByteString.Lazy as L |
| 58 |
import qualified Data.ByteString as S |
| 59 |
import Data.Functor.Identity |
| 60 |
import qualified Data.Map.Strict as M |
| 61 |
import Data.Hashable (Hashable(..)) |
| 62 |
import Data.Semigroup (Semigroup (..)) |
| 63 |
import Data.Monoid (Monoid (..)) |
| 64 |
import Data.String |
| 65 |
import Data.Text (Text) |
| 66 |
import qualified Data.Text.Lazy as LT |
| 67 |
import qualified Data.Text.Lazy.Encoding as LT |
| 68 |
import qualified Data.Text.Encoding as T |
| 69 |
import Prelude |
| 70 |
import Data.Maybe |
| 71 |
import Data.Sequence (Seq) |
| 72 |
import qualified Data.Sequence as Seq |
| 73 |
import Data.Foldable (toList) |
| 74 |
import qualified Data.Set as Set |
| 75 |
|
| 76 |
-------------------------------------------------------------------------------- |
| 77 |
-- Types |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
data Attribute = Attribute !Text !Text |
| 83 |
deriving (Show,Eq) |
| 84 |
|
| 85 |
instance Hashable Attribute where |
| 86 |
hashWithSalt salt (Attribute a b) = salt `hashWithSalt` a `hashWithSalt` b |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
type Html = HtmlT Identity |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
newtype HtmlT m a = |
| 101 |
HtmlT {runHtmlT :: m (Seq Attribute -> Builder,a) |
| 102 |
|
| 103 |
-- finally returning an element builder and a value. You can |
| 104 |
-- pass 'mempty' for this argument for a top-level call. See |
| 105 |
-- 'evalHtmlT' and 'execHtmlT' for easier to use functions. |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
instance MFunctor HtmlT where |
| 110 |
hoist f (HtmlT xs) = HtmlT (f xs) |
| 111 |
|
| 112 |
|
| 113 |
instance (a ~ (),Applicative m) => Semigroup (HtmlT m a) where |
| 114 |
(<>) = liftA2 (<>) |
| 115 |
|
| 116 |
|
| 117 |
instance (a ~ (),Applicative m) => Monoid (HtmlT m a) where |
| 118 |
mempty = pure mempty |
| 119 |
mappend = liftA2 mappend |
| 120 |
|
| 121 |
|
| 122 |
instance Applicative m => Applicative (HtmlT m) where |
| 123 |
pure a = HtmlT (pure (mempty,a)) |
| 124 |
{-# INLINE pure #-} |
| 125 |
|
| 126 |
f <*> x = HtmlT $ mk <$> runHtmlT f <*> runHtmlT x |
| 127 |
where mk ~(g, f') ~(h, x') = (g <> h, f' x') |
| 128 |
{-# INLINE (<*>) #-} |
| 129 |
|
| 130 |
m *> n = HtmlT $ mk <$> runHtmlT m <*> runHtmlT n |
| 131 |
where mk ~(g, _) ~(h, b) = (g <> h, b) |
| 132 |
{-# INLINE (*>) #-} |
| 133 |
|
| 134 |
m <* n = HtmlT $ mk <$> runHtmlT m <*> runHtmlT n |
| 135 |
where mk ~(g, a) ~(h, _) = (g <> h, a) |
| 136 |
{-# INLINE (<*) #-} |
| 137 |
|
| 138 |
|
| 139 |
instance Functor m => Functor (HtmlT m) where |
| 140 |
fmap f = HtmlT . fmap (fmap f) . runHtmlT |
| 141 |
|
| 142 |
(<$) = fmap . const |
| 143 |
{-# INLINE (<$) #-} |
| 144 |
|
| 145 |
|
| 146 |
instance Monad m => Monad (HtmlT m) where |
| 147 |
return a = HtmlT (return (mempty,a)) |
| 148 |
{-# INLINE return #-} |
| 149 |
|
| 150 |
m >>= f = HtmlT $ do |
| 151 |
~(g,a) <- runHtmlT m |
| 152 |
~(h,b) <- runHtmlT (f a) |
| 153 |
return (g <> h,b) |
| 154 |
{-# INLINE (>>=) #-} |
| 155 |
|
| 156 |
m >> n = HtmlT $ do |
| 157 |
~(g, _) <- runHtmlT m |
| 158 |
~(h, b) <- runHtmlT n |
| 159 |
return (g <> h, b) |
| 160 |
{-# INLINE (>>) #-} |
| 161 |
|
| 162 |
|
| 163 |
instance MonadTrans HtmlT where |
| 164 |
lift m = |
| 165 |
HtmlT (do a <- m |
| 166 |
return (\_ -> mempty,a)) |
| 167 |
|
| 168 |
instance MonadFix m => MonadFix (HtmlT m) where |
| 169 |
mfix m = HtmlT $ mfix $ \ ~(_, a) -> runHtmlT $ m a |
| 170 |
|
| 171 |
-- MonadReader, MonadState etc instances need UndecidableInstances, |
| 172 |
-- because they do not satisfy the coverage condition. |
| 173 |
|
| 174 |
|
| 175 |
instance MonadReader r m => MonadReader r (HtmlT m) where |
| 176 |
ask = lift ask |
| 177 |
local f (HtmlT a) = HtmlT (local f a) |
| 178 |
|
| 179 |
|
| 180 |
instance MonadState s m => MonadState s (HtmlT m) where |
| 181 |
get = lift get |
| 182 |
put = lift . put |
| 183 |
state = lift . state |
| 184 |
|
| 185 |
|
| 186 |
instance MonadError e m => MonadError e (HtmlT m) where |
| 187 |
throwError = lift . throwError |
| 188 |
catchError (HtmlT m) h = HtmlT $ catchError m (runHtmlT . h) |
| 189 |
|
| 190 |
|
| 191 |
instance MonadWriter w m => MonadWriter w (HtmlT m) where |
| 192 |
tell = lift . tell |
| 193 |
listen (HtmlT x) = HtmlT $ fmap reassoc $ listen x |
| 194 |
where reassoc ((a, b), c) = (a, (b, c)) |
| 195 |
pass (HtmlT p) = HtmlT $ pass $ fmap assoc p |
| 196 |
where assoc (a, (b, c)) = ((a, b), c) |
| 197 |
|
| 198 |
|
| 199 |
instance MonadIO m => MonadIO (HtmlT m) where |
| 200 |
liftIO = lift . liftIO |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
instance (Monad m,a ~ ()) => IsString (HtmlT m a) where |
| 205 |
fromString = toHtml |
| 206 |
|
| 207 |
|
| 208 |
instance (m ~ Identity) => Show (HtmlT m a) where |
| 209 |
show = LT.unpack . renderText |
| 210 |
|
| 211 |
|
| 212 |
class ToHtml a where |
| 213 |
|
| 214 |
toHtml :: Monad m => a -> HtmlT m () |
| 215 |
|
| 216 |
toHtmlRaw :: Monad m => a -> HtmlT m () |
| 217 |
|
| 218 |
|
| 219 |
instance (a ~ (), m ~ Identity) => ToHtml (HtmlT m a) where |
| 220 |
toHtml = relaxHtmlT |
| 221 |
toHtmlRaw = relaxHtmlT |
| 222 |
|
| 223 |
instance ToHtml String where |
| 224 |
toHtml = build . Blaze.fromHtmlEscapedString |
| 225 |
toHtmlRaw = build . Blaze.fromString |
| 226 |
|
| 227 |
instance ToHtml Text where |
| 228 |
toHtml = build . Blaze.fromHtmlEscapedText |
| 229 |
toHtmlRaw = build . Blaze.fromText |
| 230 |
|
| 231 |
instance ToHtml LT.Text where |
| 232 |
toHtml = build . Blaze.fromHtmlEscapedLazyText |
| 233 |
toHtmlRaw = build . Blaze.fromLazyText |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
instance ToHtml S.ByteString where |
| 241 |
toHtml = build . Blaze.fromHtmlEscapedText . T.decodeUtf8 |
| 242 |
toHtmlRaw = build . Blaze.fromByteString |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
instance ToHtml L.ByteString where |
| 250 |
toHtml = build . Blaze.fromHtmlEscapedLazyText . LT.decodeUtf8 |
| 251 |
toHtmlRaw = build . Blaze.fromLazyByteString |
| 252 |
|
| 253 |
|
| 254 |
build :: Monad m => Builder -> HtmlT m () |
| 255 |
build b = HtmlT (return (const b,())) |
| 256 |
{-# INLINE build #-} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
class Term arg result | result -> arg where |
| 277 |
|
| 278 |
term :: Text |
| 279 |
-> arg |
| 280 |
-> result |
| 281 |
term = flip termWith [] |
| 282 |
{-# INLINE term #-} |
| 283 |
|
| 284 |
|
| 285 |
-- pre-prepared attributes into the element. |
| 286 |
termWith :: Text |
| 287 |
-> [Attribute] |
| 288 |
-> arg |
| 289 |
-> result |
| 290 |
|
| 291 |
|
| 292 |
instance (Applicative m,f ~ HtmlT m a) => Term [Attribute] (f -> HtmlT m a) where |
| 293 |
termWith name f = with (makeElement name) . (<> f) |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
instance (Applicative m) => Term (HtmlT m a) (HtmlT m a) where |
| 298 |
termWith name f = with (makeElement name) f |
| 299 |
{-# INLINE termWith #-} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
instance Term Text Attribute where |
| 304 |
termWith key _ value = makeAttribute key value |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
class TermRaw arg result | result -> arg where |
| 310 |
|
| 311 |
termRaw :: Text |
| 312 |
-> arg |
| 313 |
-> result |
| 314 |
termRaw = flip termRawWith [] |
| 315 |
|
| 316 |
-- pre-prepared attributes into the element. |
| 317 |
termRawWith :: Text |
| 318 |
-> [Attribute] |
| 319 |
-> arg |
| 320 |
-> result |
| 321 |
|
| 322 |
|
| 323 |
instance (Monad m,ToHtml f, a ~ ()) => TermRaw [Attribute] (f -> HtmlT m a) where |
| 324 |
termRawWith name f attrs = with (makeElement name) (attrs <> f) . toHtmlRaw |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
instance (Monad m,a ~ ()) => TermRaw Text (HtmlT m a) where |
| 329 |
termRawWith name f = with (makeElement name) f . toHtmlRaw |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
instance TermRaw Text Attribute where |
| 334 |
termRawWith key _ value = makeAttribute key value |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
class With a where |
| 340 |
|
| 341 |
with :: a |
| 342 |
-> [Attribute] |
| 343 |
-> a |
| 344 |
|
| 345 |
|
| 346 |
instance (Functor m) => With (HtmlT m a) where |
| 347 |
with f = \attr -> HtmlT (mk attr <$> runHtmlT f) |
| 348 |
where |
| 349 |
mk attr ~(f',a) = (\attr' -> f' (attr' <> Seq.fromList attr) |
| 350 |
,a) |
| 351 |
|
| 352 |
|
| 353 |
instance (Functor m) => With (HtmlT m a -> HtmlT m a) where |
| 354 |
with f = \attr inner -> HtmlT (mk attr <$> runHtmlT (f inner)) |
| 355 |
where |
| 356 |
mk attr ~(f',a) = (\attr' -> f' (attr' <> Seq.fromList attr) |
| 357 |
,a) |
| 358 |
|
| 359 |
-------------------------------------------------------------------------------- |
| 360 |
-- Running |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
renderToFile :: FilePath -> Html a -> IO () |
| 369 |
renderToFile fp = L.writeFile fp . Blaze.toLazyByteString . runIdentity . execHtmlT |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
renderBS :: Html a -> ByteString |
| 378 |
renderBS = Blaze.toLazyByteString . runIdentity . execHtmlT |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
renderText :: Html a -> LT.Text |
| 388 |
renderText = LT.decodeUtf8 . Blaze.toLazyByteString . runIdentity . execHtmlT |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
renderBST :: Monad m => HtmlT m a -> m ByteString |
| 397 |
renderBST = fmap Blaze.toLazyByteString . execHtmlT |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
renderTextT :: Monad m => HtmlT m a -> m LT.Text |
| 406 |
renderTextT = fmap (LT.decodeUtf8 . Blaze.toLazyByteString) . execHtmlT |
| 407 |
|
| 408 |
-------------------------------------------------------------------------------- |
| 409 |
-- Running, transformer versions |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
execHtmlT :: Monad m |
| 417 |
=> HtmlT m a |
| 418 |
-> m Builder |
| 419 |
execHtmlT m = |
| 420 |
do (f,_) <- runHtmlT m |
| 421 |
return (f mempty) |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
relaxHtmlT :: Monad m |
| 431 |
=> HtmlT Identity a |
| 432 |
-> HtmlT m a |
| 433 |
relaxHtmlT = hoist go |
| 434 |
where |
| 435 |
go :: Monad m => Identity a -> m a |
| 436 |
go = return . runIdentity |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
commuteHtmlT :: (Functor m, Monad n) |
| 456 |
=> HtmlT m a |
| 457 |
-> m (HtmlT n a) |
| 458 |
commuteHtmlT (HtmlT xs) = fmap (HtmlT . return) xs |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
evalHtmlT :: Monad m |
| 471 |
=> HtmlT m a |
| 472 |
-> m a |
| 473 |
evalHtmlT m = |
| 474 |
do (_,a) <- runHtmlT m |
| 475 |
return a |
| 476 |
|
| 477 |
-------------------------------------------------------------------------------- |
| 478 |
-- Combinators |
| 479 |
|
| 480 |
|
| 481 |
makeAttribute :: Text |
| 482 |
-> Text |
| 483 |
-> Attribute |
| 484 |
makeAttribute x y = Attribute x y |
| 485 |
|
| 486 |
|
| 487 |
makeElement :: Functor m |
| 488 |
=> Text |
| 489 |
-> HtmlT m a |
| 490 |
-> HtmlT m a |
| 491 |
{-# INLINE[1] makeElement #-} |
| 492 |
makeElement name = \m' -> HtmlT (mk <$> runHtmlT m') |
| 493 |
where |
| 494 |
mk ~(f,a) = |
| 495 |
(\attr -> |
| 496 |
s "<" <> Blaze.fromText name |
| 497 |
<> foldlMapWithKey buildAttr attr <> s ">" |
| 498 |
<> f mempty |
| 499 |
<> s "</" <> Blaze.fromText name <> s ">" |
| 500 |
,a) |
| 501 |
|
| 502 |
|
| 503 |
makeElementNoEnd :: Applicative m |
| 504 |
=> Text |
| 505 |
-> HtmlT m () |
| 506 |
makeElementNoEnd name = |
| 507 |
HtmlT (pure (\attr -> s "<" <> Blaze.fromText name |
| 508 |
<> foldlMapWithKey buildAttr attr <> s ">", |
| 509 |
())) |
| 510 |
|
| 511 |
|
| 512 |
makeXmlElementNoEnd :: Applicative m |
| 513 |
=> Text |
| 514 |
-> HtmlT m () |
| 515 |
makeXmlElementNoEnd name = |
| 516 |
HtmlT (pure (\attr -> s "<" <> Blaze.fromText name |
| 517 |
<> foldlMapWithKey buildAttr attr <> s "/>", |
| 518 |
())) |
| 519 |
|
| 520 |
|
| 521 |
buildAttr :: Text -> Text -> Builder |
| 522 |
buildAttr key val = |
| 523 |
s " " <> |
| 524 |
Blaze.fromText key <> |
| 525 |
if val == mempty |
| 526 |
then mempty |
| 527 |
else s "=\"" <> Blaze.fromHtmlEscapedText val <> s "\"" |
| 528 |
|
| 529 |
|
| 530 |
foldlMapWithKey :: (Text -> Text -> Builder) -> Seq Attribute -> Builder |
| 531 |
foldlMapWithKey f attributes = |
| 532 |
case nubOrdMaybe (map fst pairs) of |
| 533 |
Just keyList -> |
| 534 |
foldMap (\k -> fromMaybe mempty (fmap (f k) (M.lookup k values))) keyList |
| 535 |
where values = M.fromListWith (<>) pairs |
| 536 |
Nothing -> foldMap (\(Attribute k v) -> f k v) attributes |
| 537 |
where |
| 538 |
pairs = map (\(Attribute k v) -> (k,v)) (toList attributes) |
| 539 |
|
| 540 |
|
| 541 |
nubOrdMaybe :: Ord a => [a] -> Maybe [a] |
| 542 |
nubOrdMaybe = go False Set.empty [] |
| 543 |
where |
| 544 |
go (!removed) set acc (x:xs) |
| 545 |
| x `Set.member` set = go True set acc xs |
| 546 |
| otherwise = go removed (Set.insert x set) (x : acc) xs |
| 547 |
go removed _set acc [] = |
| 548 |
if removed |
| 549 |
then pure (reverse acc) |
| 550 |
else Nothing |
| 551 |
|
| 552 |
|
| 553 |
s :: String -> Builder |
| 554 |
s = Blaze.fromString |
| 555 |
{-# INLINE s #-} |