Working with file handles so files get closed in time.
This commit is contained in:
parent
453932a8b8
commit
8cc0cb94fc
2 changed files with 11 additions and 3 deletions
|
@ -11,8 +11,11 @@ module Text.Hakyll.Page
|
|||
|
||||
import qualified Data.Map as M
|
||||
import qualified Data.List as L
|
||||
import System.FilePath
|
||||
import Data.Maybe
|
||||
|
||||
import System.FilePath
|
||||
import System.IO
|
||||
|
||||
import Text.Pandoc
|
||||
|
||||
-- | A Page is basically key-value mapping. Certain keys have special
|
||||
|
@ -57,7 +60,9 @@ markdownToHTML = writeHtmlString writerOptions .
|
|||
-- pages are not templates, so they should not contain $identifiers.
|
||||
readPage :: FilePath -> IO Page
|
||||
readPage path = do
|
||||
content <- readFile path
|
||||
handle <- openFile path ReadMode
|
||||
content <- hGetContents handle
|
||||
seq content $ hClose handle
|
||||
let context = extractContext content
|
||||
body = (if takeExtension path == ".markdown" then markdownToHTML else id)
|
||||
(getBody context)
|
||||
|
|
|
@ -12,6 +12,7 @@ import Control.Monad
|
|||
|
||||
import System.FilePath
|
||||
import System.Directory
|
||||
import System.IO
|
||||
|
||||
import Text.Hakyll.Page
|
||||
import Text.Hakyll.Util
|
||||
|
@ -25,7 +26,9 @@ createContext = M.fromList . map packPair . M.toList
|
|||
|
||||
renderPage :: FilePath -> Page -> IO Page
|
||||
renderPage templatePath page = do
|
||||
templateString <- B.readFile templatePath
|
||||
handle <- openFile templatePath ReadMode
|
||||
templateString <- liftM B.pack $ hGetContents handle
|
||||
seq templateString $ hClose handle
|
||||
let body = substitute templateString (createContext page)
|
||||
return $ addContext "body" (B.unpack body) page
|
||||
|
||||
|
|
Loading…
Reference in a new issue