@@ -4,15 +4,15 @@ module Text.Hakyll.Page
44 , getValue
55 , getBody
66 , readPage
7+ , splitAtDelimiters
78 ) where
89
910import qualified Data.Map as M
10−import qualified Data.List as L
11+import Data.List (isPrefixOf)
1112import Data.Char (isSpace)
1213import Data.Maybe (fromMaybe)
1314import Control.Parallel.Strategies (rdeepseq, ($|))
1415import Control.Monad.Reader (liftIO)
15−import Control.Monad (unless)
1616import System.FilePath (takeExtension)
1717import System.IO
1818
@@ -81,33 +81,7 @@ splitAtDelimiters ls@(x:xs)
8181
8282-- | Check if the given string is a metadata delimiter.
8383isDelimiter :: String -> Bool
84−isDelimiter = L.isPrefixOf "---"
85−
86−-- | Used for caching of files.
87−cachePage :: Page -> Hakyll ()
88−cachePage page@(Page mapping) = do
89− makeDirectories destination
90− liftIO writePageToCache
91− where
92− (sectionMetaData, simpleMetaData) = M.partition (elem '\n')
93− (M.delete "body" mapping)
94−
95− writePageToCache = do
96− handle <- openFile destination WriteMode
97− hPutStrLn handle "---"
98− mapM_ (writePair handle) $ M.toList simpleMetaData
99− mapM_ (writeSection handle) $ M.toList sectionMetaData
100− hPutStrLn handle "---"
101− hPutStrLn handle $ getBody page
102− hClose handle
103−
104− writePair h (k, v) = do hPutStr h $ k ++ ": " ++ v
105− hPutStrLn h ""
106−
107− writeSection h (k, v) = do hPutStrLn h $ "--- " ++ k
108− hPutStrLn h v
109−
110− destination = toCache $ getURL page
84+isDelimiter = isPrefixOf "---"
11185
11286-- | Read one section of a page.
11387readSection :: (String -> String) -- ^ Render function.
@@ -122,7 +96,7 @@ readSection renderFunction isFirst ls
12296 | otherwise = body (tail ls)
12397 where
12498 isDelimiter' = isDelimiter (head ls)
125− isNamedDelimiter = head ls `matchesRegex` "----* *[a-zA-Z0-9][a-zA-Z0-9]*"
99+ isNamedDelimiter = head ls `matchesRegex` "^----* *[a-zA-Z0-9][a-zA-Z0-9]*"
126100 body ls' = [("body", renderFunction $ unlines ls')]
127101
128102 readSimpleMetaData = map readPair . filter (not . all isSpace)
@@ -137,11 +111,8 @@ readSection renderFunction isFirst ls
137111-- | Read a page from a file. Metadata is supported, and if the filename
138112-- has a @.markdown@ extension, it will be rendered using pandoc.
139113readPage :: FilePath -> Hakyll Page
140−readPage pagePath = do
141− -- Check cache.
142− getFromCache <- isCacheValid cacheFile [pagePath]
143− let path = if getFromCache then cacheFile else pagePath
144− renderFunction = getRenderFunction $ takeExtension path
114+readPage path = do
115+ let renderFunction = getRenderFunction $ takeExtension path
145116 sectionFunctions = map (readSection renderFunction)
146117 (True : repeat False)
147118
@@ -149,21 +120,19 @@ readPage pagePath = do
149120 handle <- liftIO $ openFile path ReadMode
150121 sections <- fmap (splitAtDelimiters . lines )
151122 (liftIO $ hGetContents handle)
123+ liftIO $ print sections
152124
153125 let context = concat $ zipWith ($) sectionFunctions sections
154126 page = fromContext $ M.fromList $
155127 [ ("url", url)
156− , ("path", pagePath)
128+ , ("path", path)
157129 ] ++ context
158130
159131 seq (($|) id rdeepseq context) $ liftIO $ hClose handle
160132
161− -- Cache if needed
162− unless getFromCache $ cachePage page
163133 return page
164134 where
165− url = toURL pagePath
166− cacheFile = toCache url
135+ url = toURL path
167136
168137-- Make pages renderable.
169138instance Renderable Page where