on master help sign in
Add IsString instance for Template
677cca0Jasper Van der Jeugt
hakyll.cabal−1
@@ -119,7 +119,6 @@ Library
119 Hakyll.Web.Template
119 Hakyll.Web.Template
120 Hakyll.Web.Template.Context
120 Hakyll.Web.Template.Context
121 Hakyll.Web.Template.List
121 Hakyll.Web.Template.List
122− Hakyll.Web.Template.Read
123
122
124 Other-Modules:
123 Other-Modules:
125 Hakyll.Check
124 Hakyll.Check
src/Hakyll.hs−2
@@ -28,7 +28,6 @@ module Hakyll
28 , module Hakyll.Web.Template
28 , module Hakyll.Web.Template
29 , module Hakyll.Web.Template.Context
29 , module Hakyll.Web.Template.Context
30 , module Hakyll.Web.Template.List
30 , module Hakyll.Web.Template.List
31− , module Hakyll.Web.Template.Read
32 ) where
31 ) where
33
32
34
33
@@ -59,4 +58,3 @@ import Hakyll.Web.Tags
59import Hakyll.Web.Template
58import Hakyll.Web.Template
60import Hakyll.Web.Template.Context
59import Hakyll.Web.Template.Context
61import Hakyll.Web.Template.List
60import Hakyll.Web.Template.List
62−import Hakyll.Web.Template.Read
src/Hakyll/Web/Feed.hs−1
@@ -35,7 +35,6 @@ import Hakyll.Core.Item
35import Hakyll.Web.Template
35import Hakyll.Web.Template
36import Hakyll.Web.Template.Context
36import Hakyll.Web.Template.Context
37import Hakyll.Web.Template.List
37import Hakyll.Web.Template.List
38−import Hakyll.Web.Template.Read
39
38
40
39
41--------------------------------------------------------------------------------
40--------------------------------------------------------------------------------
src/Hakyll/Web/Template.hs+1−1
@@ -122,6 +122,7 @@ module Hakyll.Web.Template
122 , applyTemplate
122 , applyTemplate
123 , loadAndApplyTemplate
123 , loadAndApplyTemplate
124 , applyAsTemplate
124 , applyAsTemplate
125+ , readTemplate
125 ) where
126 ) where
126
127
127
128
@@ -139,7 +140,6 @@ import Hakyll.Core.Identifier
139import Hakyll.Core.Item
140import Hakyll.Core.Item
140import Hakyll.Web.Template.Context
141import Hakyll.Web.Template.Context
141import Hakyll.Web.Template.Internal
142import Hakyll.Web.Template.Internal
142−import Hakyll.Web.Template.Read
143
143
144
144
145--------------------------------------------------------------------------------
145--------------------------------------------------------------------------------
src/Hakyll/Web/Template/Internal.hs+93−6
@@ -5,16 +5,22 @@
5module Hakyll.Web.Template.Internal
5module Hakyll.Web.Template.Internal
6 ( Template (..)
6 ( Template (..)
7 , TemplateElement (..)
7 , TemplateElement (..)
8+ , readTemplate
8 ) where
9 ) where
9
10
10
11
11--------------------------------------------------------------------------------
12--------------------------------------------------------------------------------
12−import Control.Applicative (pure, (<$>), (<*>))
13+import Control.Applicative (pure, (<$), (<$>), (<*>), (<|>))
14+import Control.Monad (void)
13−import Data.Binary (Binary, get, getWord8, put, putWord8)
15+import Data.Binary (Binary, get, getWord8, put, putWord8)
14−import Data.Typeable (Typeable)
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
15
20
16
21
17--------------------------------------------------------------------------------
22--------------------------------------------------------------------------------
23+import Hakyll.Core.Util.Parser
18import Hakyll.Core.Writable
24import Hakyll.Core.Writable
19
25
20
26
@@ -46,10 +52,10 @@ data TemplateElement
46--------------------------------------------------------------------------------
52--------------------------------------------------------------------------------
47instance Binary TemplateElement where
53instance Binary TemplateElement where
48 put (Chunk string) = putWord8 0 >> put string
54 put (Chunk string) = putWord8 0 >> put string
49− put (Key key) = putWord8 1 >> put key
55+ put (Key k) = putWord8 1 >> put k
50 put (Escaped) = putWord8 2
56 put (Escaped) = putWord8 2
51− put (If key t f) = putWord8 3 >> put key >> put t >> put f
57+ put (If k t f ) = putWord8 3 >> put k >> put t >> put f
52− put (For key b s) = putWord8 4 >> put key >> put b >> put s
58+ put (For k b s) = putWord8 4 >> put k >> put b >> put s
53 put (Partial p) = putWord8 5 >> put p
59 put (Partial p) = putWord8 5 >> put p
54
60
55 get = getWord8 >>= \tag -> case tag of
61 get = getWord8 >>= \tag -> case tag of
@@ -61,3 +67,84 @@ instance Binary TemplateElement where
61 5 -> Partial <$> get
67 5 -> Partial <$> get
62 _ -> error $
68 _ -> error $
63 "Hakyll.Web.Template.Internal: Error reading cached template"
69 "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