@@ -4,15 +4,15 @@ module Text.Hakyll.Page
9import qualified Data.Map as M
10import qualified Data.Map as M
10−import qualified Data.List as L
11+import Data.List (isPrefixOf)
11import Data.Char (isSpace)
12import Data.Char (isSpace)
12import Data.Maybe (fromMaybe)
13import Data.Maybe (fromMaybe)
13import Control.Parallel.Strategies (rdeepseq, ($|))
14import Control.Parallel.Strategies (rdeepseq, ($|))
14import Control.Monad.Reader (liftIO)
15import Control.Monad.Reader (liftIO)
15−import Control.Monad (unless)
16import System.FilePath (takeExtension)
16import System.FilePath (takeExtension)
17import System.IO
17import System.IO
@@ -81,33 +81,7 @@ splitAtDelimiters ls@(x:xs)
82-- | Check if the given string is a metadata delimiter.
82-- | Check if the given string is a metadata delimiter.
83isDelimiter :: String -> Bool
83isDelimiter :: String -> Bool
84−isDelimiter = L.isPrefixOf "---"
84+isDelimiter = isPrefixOf "---"
86−-- | Used for caching of files.
87−cachePage :: Page -> Hakyll ()
88−cachePage page@(Page mapping) = do
89− makeDirectories destination
90− liftIO writePageToCache
92− (sectionMetaData, simpleMetaData) = M.partition (elem '\n')
93− (M.delete "body" mapping)
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
104− writePair h (k, v) = do hPutStr h $ k ++ ": " ++ v
107− writeSection h (k, v) = do hPutStrLn h $ "--- " ++ k
110− destination = toCache $ getURL page
112-- | Read one section of a page.
86-- | Read one section of a page.
113readSection :: (String -> String) -- ^ Render function.
87readSection :: (String -> String) -- ^ Render function.
@@ -122,7 +96,7 @@ readSection renderFunction isFirst ls
122 | otherwise = body (tail ls)
96 | otherwise = body (tail ls)
124 isDelimiter' = isDelimiter (head ls)
98 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]*"
126 body ls' = [("body", renderFunction $ unlines ls')]
100 body ls' = [("body", renderFunction $ unlines ls')]
128 readSimpleMetaData = map readPair . filter (not . all isSpace)
102 readSimpleMetaData = map readPair . filter (not . all isSpace)
@@ -137,11 +111,8 @@ readSection renderFunction isFirst ls
137-- | Read a page from a file. Metadata is supported, and if the filename
111-- | Read a page from a file. Metadata is supported, and if the filename
138-- has a @.markdown@ extension, it will be rendered using pandoc.
112-- has a @.markdown@ extension, it will be rendered using pandoc.
139readPage :: FilePath -> Hakyll Page
113readPage :: FilePath -> Hakyll Page
140−readPage pagePath = do
114+readPage path = do
142− getFromCache <- isCacheValid cacheFile [pagePath]
143− let path = if getFromCache then cacheFile else pagePath
144− renderFunction = getRenderFunction $ takeExtension path
115+ let renderFunction = getRenderFunction $ takeExtension path
145 sectionFunctions = map (readSection renderFunction)
116 sectionFunctions = map (readSection renderFunction)
146 (True : repeat False)
117 (True : repeat False)
@@ -149,21 +120,19 @@ readPage pagePath = do
149 handle <- liftIO $ openFile path ReadMode
120 handle <- liftIO $ openFile path ReadMode
150 sections <- fmap (splitAtDelimiters . lines )
121 sections <- fmap (splitAtDelimiters . lines )
151 (liftIO $ hGetContents handle)
122 (liftIO $ hGetContents handle)
123+ liftIO $ print sections
153 let context = concat $ zipWith ($) sectionFunctions sections
125 let context = concat $ zipWith ($) sectionFunctions sections
154 page = fromContext $ M.fromList $
126 page = fromContext $ M.fromList $
155 [ ("url", url)
127 [ ("url", url)
156− , ("path", pagePath)
128+ , ("path", path)
157 ] ++ context
129 ] ++ context
159 seq (($|) id rdeepseq context) $ liftIO $ hClose handle
131 seq (($|) id rdeepseq context) $ liftIO $ hClose handle
162− unless getFromCache $ cachePage page
163 return page
133 return page
165− url = toURL pagePath
135+ url = toURL path
166− cacheFile = toCache url
168-- Make pages renderable.
137-- Make pages renderable.
169instance Renderable Page where
138instance Renderable Page where