Added CustomPage.
This commit is contained in:
parent
d83791c106
commit
d988921714
3 changed files with 35 additions and 6 deletions
|
@ -23,5 +23,6 @@ library
|
|||
exposed-modules: Text.Hakyll.Render
|
||||
Text.Hakyll.Renderable
|
||||
Text.Hakyll.Page
|
||||
Text.Hakyll.CustomPage
|
||||
Text.Hakyll.RenderableFilePath
|
||||
Text.Hakyll.Util
|
||||
|
|
29
src/Text/Hakyll/CustomPage.hs
Normal file
29
src/Text/Hakyll/CustomPage.hs
Normal file
|
@ -0,0 +1,29 @@
|
|||
module Text.Hakyll.CustomPage
|
||||
( CustomPage,
|
||||
createCustomPage
|
||||
) where
|
||||
|
||||
import System.FilePath
|
||||
import qualified Data.ByteString.Lazy.Char8 as B
|
||||
import qualified Data.Map as M
|
||||
import Control.Monad
|
||||
import Text.Hakyll.Renderable
|
||||
|
||||
data CustomPage = CustomPage { url :: String,
|
||||
dependencies :: [FilePath],
|
||||
mapping :: [(String, Either String (IO B.ByteString))]
|
||||
}
|
||||
|
||||
createCustomPage :: String
|
||||
-> [FilePath]
|
||||
-> [(String, Either String (IO B.ByteString))]
|
||||
-> CustomPage
|
||||
createCustomPage = CustomPage
|
||||
|
||||
instance Renderable CustomPage where
|
||||
getDependencies = dependencies
|
||||
getURL = url
|
||||
toContext page = do
|
||||
values <- mapM (either (return . B.pack) (>>= return) . snd) (mapping page)
|
||||
let keys = map (B.pack . fst) (mapping page)
|
||||
return $ M.fromList $ (B.pack "url", B.pack $ url page) : zip keys values
|
|
@ -40,12 +40,11 @@ writePage page = do
|
|||
makeDirectories destination
|
||||
B.writeFile destination (getBody page)
|
||||
|
||||
renderAndConcat :: FilePath -> [FilePath] -> IO B.ByteString
|
||||
renderAndConcat templatePath paths = foldM concatRender' B.empty paths
|
||||
where concatRender' :: B.ByteString -> FilePath -> IO B.ByteString
|
||||
concatRender' chunk path = do
|
||||
page <- readPage path
|
||||
rendered <- render templatePath page
|
||||
renderAndConcat :: Renderable a => FilePath -> [a] -> IO B.ByteString
|
||||
renderAndConcat templatePath renderables = foldM concatRender' B.empty renderables
|
||||
where concatRender' :: Renderable a => B.ByteString -> a -> IO B.ByteString
|
||||
concatRender' chunk renderable = do
|
||||
rendered <- render templatePath renderable
|
||||
let body = getBody rendered
|
||||
return $ B.append chunk $ body
|
||||
|
||||
|
|
Loading…
Reference in a new issue