on master help sign in
Add IsString instance for Template

Closes #150

commit677cca05784a0409e91ee2b8c354f148772e5d57 authorJasper Van der Jeugt <m@jaspervdj.be> dateSun 19 Jan 2014, 21:24 (13 years ago) parentc024ed8
6 files changed+94−104
hakyll.cabal−1 src/Hakyll.hs−2 src/Hakyll/Web/Feed.hs−1 src/Hakyll/Web/Template.hs+1−1 src/Hakyll/Web/Template/Internal.hs+93−6 src/Hakyll/Web/Template/Read.hs−93
hakyll.cabal−1
@@ -119,7 +119,6 @@ Library
119119 Hakyll.Web.Template
120120 Hakyll.Web.Template.Context
121121 Hakyll.Web.Template.List
122− Hakyll.Web.Template.Read
123122
124123 Other-Modules:
125124 Hakyll.Check
src/Hakyll.hs−2
@@ -28,7 +28,6 @@ module Hakyll
2828 , module Hakyll.Web.Template
2929 , module Hakyll.Web.Template.Context
3030 , module Hakyll.Web.Template.List
31− , module Hakyll.Web.Template.Read
3231 ) where
3332
3433
@@ -59,4 +58,3 @@ import Hakyll.Web.Tags
5958import Hakyll.Web.Template
6059import Hakyll.Web.Template.Context
6160import Hakyll.Web.Template.List
62−import Hakyll.Web.Template.Read
src/Hakyll/Web/Feed.hs−1
@@ -35,7 +35,6 @@ import Hakyll.Core.Item
3535import Hakyll.Web.Template
3636import Hakyll.Web.Template.Context
3737import Hakyll.Web.Template.List
38−import Hakyll.Web.Template.Read
3938
4039
4140--------------------------------------------------------------------------------
src/Hakyll/Web/Template.hs+1−1
@@ -122,6 +122,7 @@ module Hakyll.Web.Template
122122 , applyTemplate
123123 , loadAndApplyTemplate
124124 , applyAsTemplate
125+ , readTemplate
125126 ) where
126127
127128
@@ -139,7 +140,6 @@ import Hakyll.Core.Identifier
139140import Hakyll.Core.Item
140141import Hakyll.Web.Template.Context
141142import Hakyll.Web.Template.Internal
142−import Hakyll.Web.Template.Read
143143
144144
145145--------------------------------------------------------------------------------
src/Hakyll/Web/Template/Internal.hs+93−6
@@ -5,16 +5,22 @@
55module Hakyll.Web.Template.Internal
66 ( Template (..)
77 , TemplateElement (..)
8+ , readTemplate
89 ) where
910
1011
1112--------------------------------------------------------------------------------
12−import Control.Applicative (pure, (<$>), (<*>))
13−import Data.Binary (Binary, get, getWord8, put, putWord8)
14−import Data.Typeable (Typeable)
13+import Control.Applicative (pure, (<$), (<$>), (<*>), (<|>))
14+import Control.Monad (void)
15+import Data.Binary (Binary, get, getWord8, put, putWord8)
16+import Data.Typeable (Typeable)
17+import GHC.Exts (IsString (..))
18+import qualified Text.Parsec as P
19+import qualified Text.Parsec.String as P
1520
1621
1722--------------------------------------------------------------------------------
23+import Hakyll.Core.Util.Parser
1824import Hakyll.Core.Writable
1925
2026
@@ -46,10 +52,10 @@ data TemplateElement
4652--------------------------------------------------------------------------------
4753instance Binary TemplateElement where
4854 put (Chunk string) = putWord8 0 >> put string
49− put (Key key) = putWord8 1 >> put key
55+ put (Key k) = putWord8 1 >> put k
5056 put (Escaped) = putWord8 2
51− put (If key t f) = putWord8 3 >> put key >> put t >> put f
52− put (For key b s) = putWord8 4 >> put key >> put b >> put s
57+ put (If k t f ) = putWord8 3 >> put k >> put t >> put f
58+ put (For k b s) = putWord8 4 >> put k >> put b >> put s
5359 put (Partial p) = putWord8 5 >> put p
5460
5561 get = getWord8 >>= \tag -> case tag of
@@ -61,3 +67,84 @@ instance Binary TemplateElement where
6167 5 -> Partial <$> get
6268 _ -> error $
6369 "Hakyll.Web.Template.Internal: Error reading cached template"
70+
71+
72+--------------------------------------------------------------------------------
73+instance IsString Template where
74+ fromString = readTemplate
75+
76+
77+--------------------------------------------------------------------------------
78+readTemplate :: String -> Template
79+readTemplate input = case P.parse template "" input of
80+ Left err -> error $ "Cannot parse template: " ++ show err
81+ Right t -> t
82+
83+
84+--------------------------------------------------------------------------------
85+template :: P.Parser Template
86+template = Template <$>
87+ (P.many1 $ chunk <|> escaped <|> conditional <|> for <|> partial <|> key)
88+
89+
90+--------------------------------------------------------------------------------
91+chunk :: P.Parser TemplateElement
92+chunk = Chunk <$> (P.many1 $ P.noneOf "$")
93+
94+
95+--------------------------------------------------------------------------------
96+escaped :: P.Parser TemplateElement
97+escaped = Escaped <$ (P.try $ P.string "$$")
98+
99+
100+--------------------------------------------------------------------------------
101+conditional :: P.Parser TemplateElement
102+conditional = P.try $ do
103+ void $ P.string "$if("
104+ i <- metadataKey
105+ void $ P.string ")$"
106+ thenBranch <- template
107+ elseBranch <- P.optionMaybe $ P.try (P.string "$else$") >> template
108+ void $ P.string "$endif$"
109+ return $ If i thenBranch elseBranch
110+
111+
112+--------------------------------------------------------------------------------
113+for :: P.Parser TemplateElement
114+for = P.try $ do
115+ void $ P.string "$for("
116+ i <- metadataKey
117+ void $ P.string ")$"
118+ body <- template
119+ sep <- P.optionMaybe $ P.try (P.string "$sep$") >> template
120+ void $ P.string "$endfor$"
121+ return $ For i body sep
122+
123+
124+--------------------------------------------------------------------------------
125+partial :: P.Parser TemplateElement
126+partial = P.try $ do
127+ void $ P.string "$partial("
128+ i <- stringLiteral
129+ void $ P.string ")$"
130+ return $ Partial i
131+
132+
133+--------------------------------------------------------------------------------
134+key :: P.Parser TemplateElement
135+key = P.try $ do
136+ void $ P.char '$'
137+ k <- metadataKey
138+ void $ P.char '$'
139+ return $ Key k
140+
141+
142+--------------------------------------------------------------------------------
143+stringLiteral :: P.Parser String
144+stringLiteral = do
145+ void $ P.char '\"'
146+ str <- P.many $ do
147+ x <- P.noneOf "\""
148+ if x == '\\' then P.anyChar else return x
149+ void $ P.char '\"'
150+ return str
src/Hakyll/Web/Template/Read.hsdeleted−93
@@ -1,93 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Read templates in Hakyll's native format
3−module Hakyll.Web.Template.Read
4− ( readTemplate
5− ) where
6−
7−
8−--------------------------------------------------------------------------------
9−import Control.Applicative ((<$), (<$>))
10−import Control.Monad (void)
11−import Text.Parsec
12−import Text.Parsec.String
13−
14−
15−--------------------------------------------------------------------------------
16−import Hakyll.Core.Util.Parser
17−import Hakyll.Web.Template.Internal
18−
19−
20−--------------------------------------------------------------------------------
21−readTemplate :: String -> Template
22−readTemplate input = case parse template "" input of
23− Left err -> error $ "Cannot parse template: " ++ show err
24− Right t -> t
25−
26−
27−--------------------------------------------------------------------------------
28−template :: Parser Template
29−template = Template <$>
30− (many1 $ chunk <|> escaped <|> conditional <|> for <|> partial <|> key)
31−
32−
33−--------------------------------------------------------------------------------
34−chunk :: Parser TemplateElement
35−chunk = Chunk <$> (many1 $ noneOf "$")
36−
37−
38−--------------------------------------------------------------------------------
39−escaped :: Parser TemplateElement
40−escaped = Escaped <$ (try $ string "$$")
41−
42−
43−--------------------------------------------------------------------------------
44−conditional :: Parser TemplateElement
45−conditional = try $ do
46− void $ string "$if("
47− i <- metadataKey
48− void $ string ")$"
49− thenBranch <- template
50− elseBranch <- optionMaybe $ try (string "$else$") >> template
51− void $ string "$endif$"
52− return $ If i thenBranch elseBranch
53−
54−
55−--------------------------------------------------------------------------------
56−for :: Parser TemplateElement
57−for = try $ do
58− void $ string "$for("
59− i <- metadataKey
60− void $ string ")$"
61− body <- template
62− sep <- optionMaybe $ try (string "$sep$") >> template
63− void $ string "$endfor$"
64− return $ For i body sep
65−
66−
67−--------------------------------------------------------------------------------
68−partial :: Parser TemplateElement
69−partial = try $ do
70− void $ string "$partial("
71− i <- stringLiteral
72− void $ string ")$"
73− return $ Partial i
74−
75−
76−--------------------------------------------------------------------------------
77−key :: Parser TemplateElement
78−key = try $ do
79− void $ char '$'
80− k <- metadataKey
81− void $ char '$'
82− return $ Key k
83−
84−
85−--------------------------------------------------------------------------------
86−stringLiteral :: Parser String
87−stringLiteral = do
88− void $ char '\"'
89− str <- many $ do
90− x <- noneOf "\""
91− if x == '\\' then anyChar else return x
92− void $ char '\"'
93− return str