@@ -10,44 +10,28 @@ module Text.Hakyll.Render.Internal
12import qualified Data.Map as M
12import qualified Data.Map as M
13−import Text.Hakyll.Context (Context, ContextManipulation)
14import Control.Monad.Reader (liftIO)
13import Control.Monad.Reader (liftIO)
15−import Data.List (isPrefixOf, foldl')
14+import Data.List (foldl')
16−import Data.Char (isAlphaNum)
17import Data.Maybe (fromMaybe)
15import Data.Maybe (fromMaybe)
17+import Text.Hakyll.Template (Template, substitute, fromString)
18+import Text.Hakyll.Context (Context, ContextManipulation)
19import Text.Hakyll.Renderable
19import Text.Hakyll.Renderable
20import Text.Hakyll.Page
20import Text.Hakyll.Page
21import Text.Hakyll.File
21import Text.Hakyll.File
22import Text.Hakyll.Hakyll
22import Text.Hakyll.Hakyll
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'
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
40-- | "substitute" for use during a chain.
24-- | "substitute" for use during a chain.
41−regularSubstitute :: String -> Context -> String
25+regularSubstitute :: Template -> Context -> String
42regularSubstitute = substitute "$$"
26regularSubstitute = substitute "$$"
44-- | "substitute" for the end of a chain (just before writing).
28-- | "substitute" for the end of a chain (just before writing).
45−finalSubstitute :: String -> Context -> String
29+finalSubstitute :: Template -> Context -> String
46finalSubstitute = substitute "$"
30finalSubstitute = substitute "$"
48-- | A pure render function.
32-- | A pure render function.
49pureRenderWith :: ContextManipulation -- ^ Manipulation to apply on the context.
33pureRenderWith :: ContextManipulation -- ^ Manipulation to apply on the context.
50− -> String -- ^ Template to use for rendering.
34+ -> Template -- ^ Template to use for rendering.
51 -> Context -- ^ Renderable object to render with given template.
35 -> Context -- ^ Renderable object to render with given template.
52 -> Context -- ^ The body of the result will contain the render.
36 -> Context -- ^ The body of the result will contain the render.
53pureRenderWith manipulation template context =
37pureRenderWith manipulation template context =
@@ -59,7 +43,7 @@ pureRenderWith manipulation template context =
60-- | A pure renderAndConcat function.
44-- | A pure renderAndConcat function.
61pureRenderAndConcatWith :: ContextManipulation -- ^ Manipulation to apply.
45pureRenderAndConcatWith :: ContextManipulation -- ^ Manipulation to apply.
62− -> [String] -- ^ Templates to use.
46+ -> [Template] -- ^ Templates to use.
63 -> [Context] -- ^ Different renderables.
47 -> [Context] -- ^ Different renderables.
65pureRenderAndConcatWith manipulation templates =
49pureRenderAndConcatWith manipulation templates =
@@ -70,7 +54,7 @@ pureRenderAndConcatWith manipulation templates =
71-- | A pure renderChain function.
55-- | A pure renderChain function.
72pureRenderChainWith :: ContextManipulation
56pureRenderChainWith :: ContextManipulation
73− -> [String]
57+ -> [Template]
74 -> Context
58 -> Context
75 -> Context
59 -> Context
76pureRenderChainWith manipulation templates context =
60pureRenderChainWith manipulation templates context =
@@ -86,6 +70,7 @@ writePage page = do
86 let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
70 let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
87 makeDirectories destination
71 makeDirectories destination
88 -- Substitute $root here, just before writing.
72 -- Substitute $root here, just before writing.
89− liftIO $ writeFile destination $ finalSubstitute (getBody page) context
73+ liftIO $ writeFile destination $ finalSubstitute (fromString $ getBody page)
91 url = getURL page
76 url = getURL page