@@ -15,8 +15,9 @@ module Hakyll.Web.Template.Internal
15import Control.Applicative ((<|>))
15import Control.Applicative ((<|>))
16import Control.Monad (void)
16import Control.Monad (void)
17import Data.Binary (Binary, get, getWord8, put, putWord8)
17import Data.Binary (Binary, get, getWord8, put, putWord8)
18+import Data.List (intercalate)
19+import Data.Maybe (isJust)
18import Data.Typeable (Typeable)
20import Data.Typeable (Typeable)
19−import Data.List (intercalate)
20import GHC.Exts (IsString (..))
21import GHC.Exts (IsString (..))
21import qualified Text.Parsec as P
22import qualified Text.Parsec as P
22import qualified Text.Parsec.String as P
23import qualified Text.Parsec.String as P
@@ -34,6 +35,12 @@ newtype Template = Template
34 } deriving (Show, Eq, Binary, Typeable)
35 } deriving (Show, Eq, Binary, Typeable)
38+--------------------------------------------------------------------------------
39+instance Monoid Template where
41+ (Template xs) `mappend` (Template ys) = Template (xs `mappend` ys)
37--------------------------------------------------------------------------------
44--------------------------------------------------------------------------------
38instance Writable Template where
45instance Writable Template where
39 -- Writing a template is impossible
46 -- Writing a template is impossible
@@ -131,13 +138,19 @@ readTemplate input = case P.parse template "" input of
132--------------------------------------------------------------------------------
139--------------------------------------------------------------------------------
133template :: P.Parser Template
140template :: P.Parser Template
134−template = Template <$>
141+template = mconcat <$> P.many (P.choice [ lift chunk
135− (P.many $ chunk <|> escaped <|> conditional <|> for <|> partial <|> expr)
143+ , conditional
148+ where lift = fmap (Template . (:[]))
138--------------------------------------------------------------------------------
151--------------------------------------------------------------------------------
139chunk :: P.Parser TemplateElement
152chunk :: P.Parser TemplateElement
140−chunk = Chunk <$> (P.many1 $ P.noneOf "$")
153+chunk = Chunk <$> P.many1 (P.noneOf "$")
143--------------------------------------------------------------------------------
156--------------------------------------------------------------------------------
@@ -156,19 +169,56 @@ expr' = stringLiteral <|> call <|> ident
157--------------------------------------------------------------------------------
170--------------------------------------------------------------------------------
158escaped :: P.Parser TemplateElement
171escaped :: P.Parser TemplateElement
159−escaped = Escaped <$ (P.try $ P.string "$$")
172+escaped = Escaped <$ P.try (P.string "$$")
175+--------------------------------------------------------------------------------
176+trimOpen :: P.Parser Bool
179+ trimLIf <- P.optionMaybe $ P.try (P.char '-')
180+ pure $ isJust trimLIf
162--------------------------------------------------------------------------------
183--------------------------------------------------------------------------------
184+trimClose :: P.Parser Bool
186+ trimIfR <- P.optionMaybe $ P.try (P.char '-')
188+ pure $ isJust trimIfR
191+--------------------------------------------------------------------------------
163−conditional :: P.Parser TemplateElement
192+conditional :: P.Parser Template
164conditional = P.try $ do
193conditional = P.try $ do
165− void $ P.string "$if("
195+ void $ P.string "if("
166 e <- expr'
196 e <- expr'
167− void $ P.string ")$"
197+ void $ P.char ')'
198+ trimRIf <- trimClose
168 thenBranch <- template
200 thenBranch <- template
169− elseBranch <- P.optionMaybe $ P.try (P.string "$else$") >> template
202+ elseBranch <- P.optionMaybe $ P.try $ do
203+ trimLElse <- trimOpen
204+ void $ P.string "else"
205+ trimRElse <- trimClose
206+ elseBody <- template
207+ pure $ mconcat $ concat [ [Template [TrimL] | trimLElse]
208+ , [Template [TrimR] | trimRElse]
212+ trimLEnd <- trimOpen
170− void $ P.string "$endif$"
213+ void $ P.string "endif"
214+ trimREnd <- trimClose
216+ pure $ Template $ mconcat [ [TrimL | trimLIf]
171− return $ If e thenBranch elseBranch
218+ , [If e thenBranch elseBranch]
219+ , [TrimL | trimLEnd]
220+ , [TrimR | trimREnd]
174--------------------------------------------------------------------------------
224--------------------------------------------------------------------------------