@@ -10,44 +10,28 @@ module Text.Hakyll.Render.Internal
1010 ) where
1111
1212import qualified Data.Map as M
13−import Text.Hakyll.Context (Context, ContextManipulation)
1413import Control.Monad.Reader (liftIO)
15−import Data.List (isPrefixOf, foldl')
16−import Data.Char (isAlphaNum)
14+import Data.List (foldl')
1715import Data.Maybe (fromMaybe)
1816
17+import Text.Hakyll.Template (Template, substitute, fromString)
18+import Text.Hakyll.Context (Context, ContextManipulation)
1919import Text.Hakyll.Renderable
2020import Text.Hakyll.Page
2121import Text.Hakyll.File
2222import Text.Hakyll.Hakyll
2323
24−-- | Substitutes @$identifiers@ in the given string by values from the given
25−-- "Context". When a key is not found, it is left as it is. You can here
26−-- specify the characters used to replace escaped dollars (@$$@).
27−substitute :: String -> String -> Context -> String
28−substitute _ [] _ = []
29−substitute escaper string context
30− | "$$" `isPrefixOf` string = escaper ++ substitute' (tail tail')
31− | "$" `isPrefixOf` string = substituteKey
32− | otherwise = head string : substitute' tail'
33− where
34− tail' = tail string
35− (key, rest) = span isAlphaNum tail'
36− replacement = fromMaybe ('$' : key) $ M.lookup key context
37− substituteKey = replacement ++ substitute' rest
38− substitute' str = substitute escaper str context
39−
4024-- | "substitute" for use during a chain.
41−regularSubstitute :: String -> Context -> String
25+regularSubstitute :: Template -> Context -> String
4226regularSubstitute = substitute "$$"
4327
4428-- | "substitute" for the end of a chain (just before writing).
45−finalSubstitute :: String -> Context -> String
29+finalSubstitute :: Template -> Context -> String
4630finalSubstitute = substitute "$"
4731
4832-- | A pure render function.
4933pureRenderWith :: ContextManipulation -- ^ Manipulation to apply on the context.
50− -> String -- ^ Template to use for rendering.
34+ -> Template -- ^ Template to use for rendering.
5135 -> Context -- ^ Renderable object to render with given template.
5236 -> Context -- ^ The body of the result will contain the render.
5337pureRenderWith manipulation template context =
@@ -59,7 +43,7 @@ pureRenderWith manipulation template context =
5943
6044-- | A pure renderAndConcat function.
6145pureRenderAndConcatWith :: ContextManipulation -- ^ Manipulation to apply.
62− -> [String] -- ^ Templates to use.
46+ -> [Template] -- ^ Templates to use.
6347 -> [Context] -- ^ Different renderables.
6448 -> String
6549pureRenderAndConcatWith manipulation templates =
@@ -70,7 +54,7 @@ pureRenderAndConcatWith manipulation templates =
7054
7155-- | A pure renderChain function.
7256pureRenderChainWith :: ContextManipulation
73− -> [String]
57+ -> [Template]
7458 -> Context
7559 -> Context
7660pureRenderChainWith manipulation templates context =
@@ -86,6 +70,7 @@ writePage page = do
8670 let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
8771 makeDirectories destination
8872 -- Substitute $root here, just before writing.
89− liftIO $ writeFile destination $ finalSubstitute (getBody page) context
73+ liftIO $ writeFile destination $ finalSubstitute (fromString $ getBody page)
74+ context
9075 where
9176 url = getURL page