aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJohannes Gerer <oss@johannesgerer.com>2016-10-26 03:39:23 +0200
committerJohannes Gerer <oss@johannesgerer.com>2016-10-26 03:39:23 +0200
commit86d89e47d648770ae36dba01f7ae09d34f2ee171 (patch)
tree504457544a461b341bf5a8c9f56368b5e2cba554
parent675085c2e0b0b851378da08b7d73024766107c87 (diff)
downloadblazeT-86d89e47d648770ae36dba01f7ae09d34f2ee171.tar.gz
blazeT-86d89e47d648770ae36dba01f7ae09d34f2ee171.tar.zst
blazeT-86d89e47d648770ae36dba01f7ae09d34f2ee171.zip
a
-rw-r--r--README.md75
-rw-r--r--src/Readme.hs23
-rw-r--r--src/Text/BlazeT.hs2
-rw-r--r--src/Text/BlazeT/Internal.hs14
-rw-r--r--src/Text/BlazeT/Renderer/Pretty.hs10
-rw-r--r--src/Text/BlazeT/Renderer/String.hs8
-rw-r--r--src/Text/BlazeT/Renderer/Text.hs50
-rw-r--r--src/Text/BlazeT/Renderer/Utf8.hs34
8 files changed, 118 insertions, 98 deletions
diff --git a/README.md b/README.md
index 0169fab..853f1e2 100644
--- a/README.md
+++ b/README.md
@@ -26,14 +26,14 @@ accumulating log or other diagnostic output
26doing `IO` (like database access) are the first things that come to 26doing `IO` (like database access) are the first things that come to
27mind. 27mind.
28 28
29The initial reason of existence of this library is its use 29The reason of existence of this library is its use
30in [Lykah](http://johannesgerer.com/Lykah), which powers my personal 30in [Lykah](http://johannesgerer.com/Lykah), which powers my personal
31website 31website
32[http://johannesgerer.com](http://johannesgerer.com/johannesgerer.com). In 32[http://johannesgerer.com](http://johannesgerer.com/johannesgerer.com). In
33Lykah, the HTML templates have access to the whole site structure (to 33Lykah, the HTML templates have access to the whole site structure (to
34build things like menus) and automatically check, insert and keep 34build things like menus or blog post lists) and automatically check,
35track of referenced pages and assets, which turns out to be very 35insert and keep track of referenced pages and assets, which turns out
36useful for the task of static website generation. 36to be very useful functionality of a static website generator.
37 37
38# How to use it? 38# How to use it?
39 39
@@ -51,20 +51,73 @@ their [documentation](https://jaspervdj.be/blaze/).
51 51
52## Unleash the monads 52## Unleash the monads
53 53
54[Text.BlazeT](https://hackage.haskell.org/package/blazeT/docs/Text-BlazeT.html) exports
55`runWith` and `execWith`, which work on any
56`Text.Blaze.Renderer.*`. The rendered markup will be returned within
57the base monad, whose actions can
58be
59[`lift`ed](https://hackage.haskell.org/package/transformers-0.5.2.0/docs/Control-Monad-Trans-Class.html) into
60the Markup, as shown in the following example
61(from [here](src/Readme.hs)):
54 62
63```Haskell
64{-# LANGUAGE OverloadedStrings #-}
65
66import Data.Time (getCurrentTime)
67import Text.BlazeT.Html5 hiding (main)
68import Text.BlazeT.Renderer.String
69import Control.Monad.Trans.Class (lift)
70
71-- Backwords compatible Blaze HTML
72old :: Markup
73old = do
74 p $ "created with blaze-html"
75
76-- BlazeT HTML with lifted IO actions
77new :: MarkupT IO ()
78new = do
79 time <- lift getCurrentTime
80 p $ string $ "created with blazeT at " ++ show time
81
82main :: IO ()
83main = do
84 putStrLn $ renderMarkup old
85 putStrLn =<< execWith renderMarkup new
86
87```
88
89prints:
90
91```HTML
92<p>created with blaze-html</p>
93<p>created with blazeT at 2016-10-26 01:09:16.969147361 UTC</p>
94```
95
96# Installation
97
981. To make it available on your system (or sandbox) use `cabal install blazeT`.
99
1002. To play around with the source, obtain by cloning this repo or use
101 `cabal get blazet`, enter the directory and run:
102
103```bash
104cabal sandbox init #optional
105cabal install
106```
107
108# Documentation on [Hackage](https://hackage.haskell.org/package/blazeT)
55 109
56# Implementation 110# Implementation
57 111
58... is located 112... is contained
59in 113in
60[Text.BlazeT.Internals](https://hackage.haskell.org/package/blazeT/docs/Text-BlazeT-Internals.html). 114[Text.BlazeT.Internals](https://hackage.haskell.org/package/blazeT/docs/Text-BlazeT-Internals.html).
61 115
62Everything build around the simple `newtype` definition of the 116Everything is build around the simple `newtype` definition of the
63`MarkupT` transformer, which makes use of the fact that `Blaze.Markup` 117`MarkupT` transformer, which makes use
64is 118the
65a 119[Monoid](https://hackage.haskell.org/package/base-4.7.0.2/docs/Data-Monoid.html) instance
66[Monoid](https://hackage.haskell.org/package/base-4.7.0.2/docs/Data-Monoid.html) and 120of `Blaze.Markup` and is basically a `WriterT` writing `Blaze.Markup`:
67which is basically a `WriterT` transformer writing `Blaze.Markup`:
68 121
69```Haskell 122```Haskell
70newtype MarkupT m a = MarkupT { fromMarkupT :: WriterT B.Markup m a } 123newtype MarkupT m a = MarkupT { fromMarkupT :: WriterT B.Markup m a }
diff --git a/src/Readme.hs b/src/Readme.hs
new file mode 100644
index 0000000..3ccabad
--- /dev/null
+++ b/src/Readme.hs
@@ -0,0 +1,23 @@
1{-# LANGUAGE OverloadedStrings #-}
2
3import Data.Time (getCurrentTime)
4import Text.BlazeT.Html5 hiding (main)
5import Text.BlazeT.Renderer.String
6import Control.Monad.Trans.Class (lift)
7
8-- Backwords compatible Blaze HTML
9old :: Markup
10old = do
11 p $ "created with blaze-html"
12
13-- BlazeT HTML with lifted IO actions
14new :: MarkupT IO ()
15new = do
16 time <- lift getCurrentTime
17 p $ string $ "created with blazeT at " ++ show time
18
19main :: IO ()
20main = do
21 putStrLn $ renderMarkup old
22 putStrLn =<< execWith renderMarkup new
23
diff --git a/src/Text/BlazeT.hs b/src/Text/BlazeT.hs
index 27228fa..b3e7c8d 100644
--- a/src/Text/BlazeT.hs
+++ b/src/Text/BlazeT.hs
@@ -62,6 +62,8 @@ module Text.BlazeT
62 ,runMarkupT 62 ,runMarkupT
63 ,execMarkup 63 ,execMarkup
64 ,execMarkupT 64 ,execMarkupT
65 ,runWith
66 ,execWith
65 ) where 67 ) where
66 68
67import qualified Text.Blaze as B 69import qualified Text.Blaze as B
diff --git a/src/Text/BlazeT/Internal.hs b/src/Text/BlazeT/Internal.hs
index 24ef1fe..f0c3edb 100644
--- a/src/Text/BlazeT/Internal.hs
+++ b/src/Text/BlazeT/Internal.hs
@@ -83,8 +83,11 @@ module Text.BlazeT.Internal
83 ,wrapMarkupT 83 ,wrapMarkupT
84 ,wrapMarkup2 84 ,wrapMarkup2
85 ,wrapMarkupT2 85 ,wrapMarkupT2
86 ,runWith
87 ,execWith
86 ) where 88 ) where
87 89
90import Control.Arrow
88import Control.Monad.Identity 91import Control.Monad.Identity
89import Control.Monad.Trans.Class 92import Control.Monad.Trans.Class
90import Control.Monad.Writer.Strict 93import Control.Monad.Writer.Strict
@@ -121,10 +124,21 @@ runMarkupT :: MarkupT m a -> m (a,B.Markup)
121runMarkupT = runWriterT . fromMarkupT 124runMarkupT = runWriterT . fromMarkupT
122{-# INLINE runMarkupT #-} 125{-# INLINE runMarkupT #-}
123 126
127-- | run the MarkupT and return a pair consisting of the result of the
128-- computation and the blaze markup rendered with a blaze renderer
129-- like 'Text.Blaze.Renderer.Text.renderHtml'
130runWith :: Monad m => (MarkupM () -> c) -> MarkupT m a -> m (a, c)
131runWith renderer = liftM (second $ renderer . wrapMarkup) . runMarkupT
132{-# INLINE runWith #-}
133
124execMarkupT :: Monad m => MarkupT m a -> m B.Markup 134execMarkupT :: Monad m => MarkupT m a -> m B.Markup
125execMarkupT = liftM snd . runMarkupT 135execMarkupT = liftM snd . runMarkupT
126{-# INLINE execMarkupT #-} 136{-# INLINE execMarkupT #-}
127 137
138execWith :: Monad m => (MarkupM () -> c) -> MarkupT m a -> m c
139execWith renderer = liftM snd . runWith renderer
140{-# INLINE execWith #-}
141
128runMarkup :: MarkupM a -> (a,B.Markup) 142runMarkup :: MarkupM a -> (a,B.Markup)
129runMarkup = runIdentity . runMarkupT 143runMarkup = runIdentity . runMarkupT
130{-# INLINE runMarkup #-} 144{-# INLINE runMarkup #-}
diff --git a/src/Text/BlazeT/Renderer/Pretty.hs b/src/Text/BlazeT/Renderer/Pretty.hs
index 8977c94..fa8cdad 100644
--- a/src/Text/BlazeT/Renderer/Pretty.hs
+++ b/src/Text/BlazeT/Renderer/Pretty.hs
@@ -1,22 +1,14 @@
1module Text.BlazeT.Renderer.Pretty 1module Text.BlazeT.Renderer.Pretty
2 ( renderMarkup 2 ( renderMarkup
3 , renderHtml 3 , renderHtml
4 , renderMarkupT
5 , renderHtmlT
6 ) where 4 ) where
7 5
8import Control.Monad
9import Control.Monad.Identity
10import qualified Text.Blaze.Renderer.Pretty as BU 6import qualified Text.Blaze.Renderer.Pretty as BU
11import Text.BlazeT 7import Text.BlazeT
12 8
13renderMarkup :: MarkupM a -> String 9renderMarkup :: MarkupM a -> String
14renderMarkup = runIdentity . renderMarkupT 10renderMarkup = BU.renderMarkup . execMarkup
15renderMarkupT :: Monad m => MarkupT m a -> m String
16renderMarkupT = liftM BU.renderMarkup . execMarkupT
17 11
18renderHtml :: MarkupM a -> String 12renderHtml :: MarkupM a -> String
19renderHtml = renderMarkup 13renderHtml = renderMarkup
20renderHtmlT :: Monad m => MarkupT m a -> m String
21renderHtmlT = renderMarkupT
22 14
diff --git a/src/Text/BlazeT/Renderer/String.hs b/src/Text/BlazeT/Renderer/String.hs
index 0a2de8a..9f0e0a0 100644
--- a/src/Text/BlazeT/Renderer/String.hs
+++ b/src/Text/BlazeT/Renderer/String.hs
@@ -2,8 +2,6 @@ module Text.BlazeT.Renderer.String
2 ( fromChoiceString 2 ( fromChoiceString
3 , renderMarkup 3 , renderMarkup
4 , renderHtml 4 , renderHtml
5 , renderMarkupT
6 , renderHtmlT
7 ) where 5 ) where
8 6
9import Control.Monad 7import Control.Monad
@@ -16,12 +14,8 @@ fromChoiceString :: ChoiceString -> String -> String
16fromChoiceString = BU.fromChoiceString 14fromChoiceString = BU.fromChoiceString
17 15
18renderMarkup :: MarkupM a -> String 16renderMarkup :: MarkupM a -> String
19renderMarkup = runIdentity . renderMarkupT 17renderMarkup = BU.renderMarkup . execMarkup
20renderMarkupT :: Monad m => MarkupT m a -> m String
21renderMarkupT = liftM BU.renderMarkup . execMarkupT
22 18
23renderHtml :: MarkupM a -> String 19renderHtml :: MarkupM a -> String
24renderHtml = renderMarkup 20renderHtml = renderMarkup
25renderHtmlT :: Monad m => MarkupT m a -> m String
26renderHtmlT = renderMarkupT
27 21
diff --git a/src/Text/BlazeT/Renderer/Text.hs b/src/Text/BlazeT/Renderer/Text.hs
index 31181eb..991c81c 100644
--- a/src/Text/BlazeT/Renderer/Text.hs
+++ b/src/Text/BlazeT/Renderer/Text.hs
@@ -1,13 +1,5 @@
1module Text.BlazeT.Renderer.Text 1module Text.BlazeT.Renderer.Text
2 ( renderMarkupBuilderT 2 ( renderMarkupBuilder
3 , renderMarkupBuilder
4 , renderMarkupBuilderWithT
5 , renderMarkupT
6 , renderMarkupWithT
7 , renderHtmlBuilderT
8 , renderHtmlBuilderWithT
9 , renderHtmlT
10 , renderHtmlWithT
11 , renderMarkupBuilderWith 3 , renderMarkupBuilderWith
12 , renderMarkup 4 , renderMarkup
13 , renderMarkupWith 5 , renderMarkupWith
@@ -17,9 +9,7 @@ module Text.BlazeT.Renderer.Text
17 , renderHtmlWith 9 , renderHtmlWith
18 ) where 10 ) where
19 11
20import Control.Monad
21import Data.ByteString (ByteString) 12import Data.ByteString (ByteString)
22import Control.Monad.Identity
23import Data.Text (Text) 13import Data.Text (Text)
24import qualified Data.Text.Lazy as L 14import qualified Data.Text.Lazy as L
25import qualified Data.Text.Lazy.Builder as B 15import qualified Data.Text.Lazy.Builder as B
@@ -28,48 +18,26 @@ import qualified Text.Blaze.Renderer.Text as BU
28import Text.BlazeT 18import Text.BlazeT
29 19
30renderMarkupBuilder :: MarkupM a -> B.Builder 20renderMarkupBuilder :: MarkupM a -> B.Builder
31renderMarkupBuilder = runIdentity . renderMarkupBuilderT 21renderMarkupBuilder = BU.renderMarkupBuilder . execMarkup
32
33renderMarkupBuilderT :: Monad m => MarkupT m a -> m B.Builder
34renderMarkupBuilderT = liftM BU.renderMarkupBuilder . execMarkupT
35 22
36renderHtmlBuilder :: MarkupM a -> B.Builder 23renderHtmlBuilder :: MarkupM a -> B.Builder
37renderHtmlBuilder = renderMarkupBuilder 24renderHtmlBuilder = renderMarkupBuilder
38 25
39renderHtmlBuilderT :: Monad m => MarkupT m a -> m B.Builder
40renderHtmlBuilderT = renderMarkupBuilderT
41
42renderMarkup :: MarkupM a -> L.Text 26renderMarkup :: MarkupM a -> L.Text
43renderMarkup = runIdentity . renderMarkupT 27renderMarkup = BU.renderMarkup . execMarkup
44renderMarkupT :: Monad m => MarkupT m a -> m L.Text
45renderMarkupT = liftM BU.renderMarkup . execMarkupT
46 28
47renderHtml :: MarkupM a -> L.Text 29renderHtml :: MarkupM a -> L.Text
48renderHtml = renderMarkup 30renderHtml = renderMarkup
49renderHtmlT :: Monad m => MarkupT m a -> m L.Text
50renderHtmlT = renderMarkupT
51
52renderMarkupWithT :: Monad m => (ByteString -> Text) -> MarkupT m a -> m L.Text
53renderMarkupWithT g = liftM (BU.renderMarkupWith g) . execMarkupT
54 31
55renderMarkupWith :: (ByteString -> Text) -> MarkupM a -> L.Text 32renderMarkupWith :: (ByteString -> Text) -> MarkupM a -> L.Text
56renderMarkupWith g = runIdentity . renderMarkupWithT g 33renderMarkupWith g = (BH.renderHtmlWith g) . execMarkup
57
58renderHtmlWithT :: Monad m => (ByteString -> Text) -> MarkupT m a -> m L.Text
59renderHtmlWithT g = liftM (BH.renderHtmlWith g) . execMarkupT
60 34
61renderHtmlWith :: (ByteString -> Text) -> MarkupM a -> L.Text 35renderHtmlWith :: (ByteString -> Text) -> MarkupM a -> L.Text
62renderHtmlWith g = runIdentity . renderHtmlWithT g 36renderHtmlWith = renderMarkupWith
63
64renderHtmlBuilderWithT :: Monad m => (ByteString -> Text) -> MarkupT m a -> m B.Builder
65renderHtmlBuilderWithT g = liftM (BH.renderHtmlBuilderWith g) . execMarkupT
66
67renderHtmlBuilderWith :: (ByteString -> Text) -> MarkupM a -> B.Builder
68renderHtmlBuilderWith g = runIdentity . renderHtmlBuilderWithT g
69 37
38renderMarkupBuilderWith :: (ByteString -> Text) -> MarkupM a -> B.Builder
39renderMarkupBuilderWith g = (BU.renderMarkupBuilderWith g) . execMarkup
70 40
71renderMarkupBuilderWithT :: Monad m => (ByteString -> Text) -> MarkupT m a -> m B.Builder 41renderHtmlBuilderWith :: (ByteString -> Text) -> MarkupM a -> B.Builder
72renderMarkupBuilderWithT g = liftM (BU.renderMarkupBuilderWith g) . execMarkupT 42renderHtmlBuilderWith = renderHtmlBuilderWith
73 43
74renderMarkupBuilderWith :: (ByteString -> Text) -> MarkupM a -> B.Builder
75renderMarkupBuilderWith g = runIdentity . renderMarkupBuilderWithT g
diff --git a/src/Text/BlazeT/Renderer/Utf8.hs b/src/Text/BlazeT/Renderer/Utf8.hs
index 292f81f..b5fd656 100644
--- a/src/Text/BlazeT/Renderer/Utf8.hs
+++ b/src/Text/BlazeT/Renderer/Utf8.hs
@@ -13,54 +13,28 @@ module Text.BlazeT.Renderer.Utf8
13 , renderHtmlBuilder 13 , renderHtmlBuilder
14 , renderHtml 14 , renderHtml
15 , renderHtmlToByteStringIO 15 , renderHtmlToByteStringIO
16
17 -- * new BlazeT stuff
18 , renderMarkupBuilderT
19 , renderMarkupT
20 , renderMarkupToByteStringIOT
21 , renderHtmlToByteStringIOT
22 , renderHtmlBuilderT
23 , renderHtmlT
24 ) where 16 ) where
25 17
26import qualified Blaze.ByteString.Builder as B 18import qualified Blaze.ByteString.Builder as B
27import Control.Monad
28import Control.Monad.Identity
29import qualified Data.ByteString as BS 19import qualified Data.ByteString as BS
30import qualified Data.ByteString.Lazy as BL 20import qualified Data.ByteString.Lazy as BL
31import qualified Text.Blaze.Renderer.Utf8 as BU 21import qualified Text.Blaze.Renderer.Utf8 as BU
32import Text.BlazeT 22import Text.BlazeT
33 23
34renderMarkupBuilder :: MarkupM a -> B.Builder 24renderMarkupBuilder :: MarkupM a -> B.Builder
35renderMarkupBuilder = runIdentity . renderMarkupBuilderT 25renderMarkupBuilder = BU.renderMarkupBuilder . execMarkup
36
37renderMarkupBuilderT :: Monad m => MarkupT m a -> m B.Builder
38renderMarkupBuilderT = liftM BU.renderMarkupBuilder . execMarkupT
39 26
40renderHtmlBuilder :: MarkupM a -> B.Builder 27renderHtmlBuilder :: MarkupM a -> B.Builder
41renderHtmlBuilder = renderMarkupBuilder 28renderHtmlBuilder = renderMarkupBuilder
42 29
43renderHtmlBuilderT :: Monad m => MarkupT m a -> m B.Builder
44renderHtmlBuilderT = renderMarkupBuilderT
45
46renderMarkup :: MarkupM a -> BL.ByteString 30renderMarkup :: MarkupM a -> BL.ByteString
47renderMarkup = runIdentity . renderMarkupT 31renderMarkup = BU.renderMarkup . execMarkup
48renderMarkupT :: Monad m => MarkupT m a -> m BL.ByteString
49renderMarkupT = liftM BU.renderMarkup . execMarkupT
50 32
51renderHtml :: MarkupM a -> BL.ByteString 33renderHtml :: MarkupM a -> BL.ByteString
52renderHtml = renderMarkup 34renderHtml = renderMarkup
53renderHtmlT :: Monad m => MarkupT m a -> m BL.ByteString
54renderHtmlT = renderMarkupT
55 35
56renderMarkupToByteStringIO :: (BS.ByteString -> IO ()) -> MarkupM a -> IO () 36renderMarkupToByteStringIO :: (BS.ByteString -> IO ()) -> MarkupM a -> IO ()
57renderMarkupToByteStringIO g = runIdentity . renderMarkupToByteStringIOT g 37renderMarkupToByteStringIO g = BU.renderMarkupToByteStringIO g . execMarkup
58renderMarkupToByteStringIOT :: Monad m => (BS.ByteString -> IO ()) ->
59 MarkupT m a -> m (IO ())
60renderMarkupToByteStringIOT g = liftM (BU.renderMarkupToByteStringIO g) . execMarkupT
61 38
62renderHtmlToByteStringIO :: (BS.ByteString -> IO ()) -> MarkupM a -> IO () 39renderHtmlToByteStringIO :: (BS.ByteString -> IO ()) -> MarkupM a -> IO ()
63renderHtmlToByteStringIO g = runIdentity . renderMarkupToByteStringIOT g 40renderHtmlToByteStringIO = renderMarkupToByteStringIO
64renderHtmlToByteStringIOT :: Monad m => (BS.ByteString -> IO ()) ->
65 MarkupT m a -> m (IO ())
66renderHtmlToByteStringIOT g = liftM (BU.renderMarkupToByteStringIO g) . execMarkupT