Created
January 12, 2012 07:24
-
-
Save jaspervdj/1599228 to your computer and use it in GitHub Desktop.
File listing using Hakyll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Applicative ((<$>)) | |
import Control.Arrow ((>>>)) | |
import Data.Monoid (mempty) | |
import System.FilePath (takeBaseName) | |
import System.Posix (getFileStatus, fileSize) | |
import qualified Data.Map as M | |
import Hakyll | |
getFileSize :: String -> IO Integer | |
getFileSize path = fromIntegral . fileSize <$> getFileStatus path | |
main :: IO () | |
main = hakyll $ do | |
-- Actually copy the files | |
match "files/*" $ do | |
route idRoute | |
compile copyFileCompiler | |
-- Create a metadata page for each file | |
metadata <- group "metadata" $ | |
match "files/*" $ do | |
-- We use unsafeCompiler to do arbitrary IO actions | |
compile $ unsafeCompiler $ \(Resource path) -> do | |
size <- getFileSize path | |
-- Create a page with some metadata | |
let page = fromMap $ M.fromList | |
[ ("url", path) | |
, ("filename", takeBaseName path) | |
, ("size", show size ++ " bytes") | |
] :: Page String | |
return page | |
-- Create a page listing! | |
match "files.html" $ route idRoute | |
create "files.html" $ constA mempty | |
>>> setFieldPageList id "templates/file.html" "files" metadata | |
>>> applyTemplateCompiler "templates/files.html" | |
>>> relativizeUrlsCompiler | |
-- Compile templates | |
match "templates/*" $ compile templateCompiler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment