Skip to content

Instantly share code, notes, and snippets.

@jaspervdj
Created January 12, 2012 07:24
Show Gist options
  • Save jaspervdj/1599228 to your computer and use it in GitHub Desktop.
Save jaspervdj/1599228 to your computer and use it in GitHub Desktop.
File listing using Hakyll
{-# 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