2010-12-26 18:03:03 +00:00
|
|
|
-- | A page is an important concept in Hakyll: it has a body (usually of the
|
|
|
|
-- type 'String') and number of metadata fields. This type is used to represent
|
|
|
|
-- pages on your website.
|
|
|
|
--
|
2010-12-28 10:12:45 +00:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
2010-12-26 18:03:03 +00:00
|
|
|
module Hakyll.Web.Page
|
|
|
|
( Page (..)
|
2010-12-30 20:42:23 +00:00
|
|
|
, addField
|
2010-12-26 18:03:03 +00:00
|
|
|
, toMap
|
2010-12-30 20:18:55 +00:00
|
|
|
, pageRead
|
2010-12-30 20:42:23 +00:00
|
|
|
, addDefaultFields
|
2010-12-26 18:03:03 +00:00
|
|
|
) where
|
|
|
|
|
2010-12-30 20:42:23 +00:00
|
|
|
import Prelude hiding (id)
|
|
|
|
import Control.Category (id)
|
|
|
|
import Control.Arrow ((>>^), (&&&), (>>>))
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import System.FilePath (takeBaseName)
|
|
|
|
import Data.Maybe (fromMaybe)
|
2010-12-26 18:03:03 +00:00
|
|
|
import Data.Map (Map)
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2010-12-30 20:42:23 +00:00
|
|
|
import Hakyll.Core.Identifier
|
2010-12-30 20:18:55 +00:00
|
|
|
import Hakyll.Core.Compiler
|
|
|
|
import Hakyll.Web.Page.Internal
|
|
|
|
import Hakyll.Web.Page.Read
|
2010-12-30 20:42:23 +00:00
|
|
|
import Hakyll.Web.Util.String
|
|
|
|
|
|
|
|
-- | Add a metadata field. If the field already exists, it is not overwritten.
|
|
|
|
--
|
|
|
|
addField :: String -- ^ Key
|
|
|
|
-> String -- ^ Value
|
|
|
|
-> Page a -- ^ Page to add it to
|
|
|
|
-> Page a -- ^ Resulting page
|
|
|
|
addField k v (Page m b) = Page (M.insertWith (flip const) k v m) b
|
2010-12-26 18:03:03 +00:00
|
|
|
|
|
|
|
-- | Convert a page to a map. The body will be placed in the @body@ key.
|
|
|
|
--
|
|
|
|
toMap :: Page String -> Map String String
|
|
|
|
toMap (Page m b) = M.insert "body" b m
|
2010-12-30 20:18:55 +00:00
|
|
|
|
|
|
|
-- | Read a page (do not render it)
|
|
|
|
--
|
|
|
|
pageRead :: Compiler a (Page String)
|
|
|
|
pageRead = getResourceString >>^ readPage
|
2010-12-30 20:42:23 +00:00
|
|
|
|
|
|
|
-- | Add a number of default metadata fields to a page. These fields include:
|
|
|
|
--
|
|
|
|
-- * @$url@
|
|
|
|
--
|
|
|
|
-- * @$root@
|
|
|
|
--
|
|
|
|
-- * @$title@
|
|
|
|
--
|
|
|
|
addDefaultFields :: Compiler (Page a) (Page a)
|
|
|
|
addDefaultFields = (getRoute &&& id >>^ uncurry addRoute)
|
|
|
|
>>> (getIdentifier &&& id >>^ uncurry addTitle)
|
|
|
|
where
|
|
|
|
addRoute r = addField "url" (fromMaybe "?" r)
|
|
|
|
. addField "root" (fromMaybe "/" $ toSiteRoot <$> r)
|
|
|
|
addTitle i = addField "title" (takeBaseName $ toFilePath i)
|