Navigation Menu

Skip to content

Commit

Permalink
Add 404 page support
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorycollins committed Jul 24, 2009
1 parent 8df94af commit 1fa756f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion blaaargh.cabal
@@ -1,5 +1,5 @@
Name: Blaaargh Name: Blaaargh
Version: 0.1 Version: 0.2
Synopsis: A simple content-management system for Happstack, Synopsis: A simple content-management system for Happstack,
suitable for small-to-medium-sized websites suitable for small-to-medium-sized websites
License: BSD3 License: BSD3
Expand Down
18 changes: 17 additions & 1 deletion src/Blaaargh/Handlers.hs
Expand Up @@ -11,6 +11,7 @@ import Data.List
import Data.Map (Map) import Data.Map (Map)
import qualified Data.Map as Map import qualified Data.Map as Map
import Data.Maybe import Data.Maybe
import Data.Monoid
import Happstack.Server import Happstack.Server
import Happstack.Server.HTTP.FileServe import Happstack.Server.HTTP.FileServe
import Happstack.Server.Parts import Happstack.Server.Parts
Expand Down Expand Up @@ -44,7 +45,7 @@ serveBlaaargh = do
cm <- lift get >>= return . blaaarghPostMap cm <- lift get >>= return . blaaarghPostMap
paths <- askRq >>= return . map B.pack . rqPaths paths <- askRq >>= return . map B.pack . rqPaths


serve [] paths cm serve [] paths cm `mappend` fourohfour


where where
-------------------------------------------------------------------------- --------------------------------------------------------------------------
Expand Down Expand Up @@ -103,6 +104,21 @@ serveBlaaargh = do
mbD mbD





------------------------------------------------------------------------------
fourohfour :: BlaaarghHandler
fourohfour = do
state <- lift get
mbTmpl <- findFourOhFourTemplate
tmpl <- maybe mzero return mbTmpl

let title = getTextContent . Atom.feedTitle . blaaarghFeedInfo $ state

let tmpl' = setAttribute "pageTitle" title tmpl

return $ toResponse $ HtmlResponse $ render tmpl'


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
serveStatic :: FilePath -> BlaaarghHandler serveStatic :: FilePath -> BlaaarghHandler
serveStatic = localRq (\r -> r { rqPaths=[]}) . fileServeStrict [] serveStatic = localRq (\r -> r { rqPaths=[]}) . fileServeStrict []
Expand Down
43 changes: 32 additions & 11 deletions src/Blaaargh/Templates.hs
@@ -1,7 +1,8 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}


module Blaaargh.Templates module Blaaargh.Templates
( findTemplateForPost ( findFourOhFourTemplate
, findTemplateForPost
, findTemplateForDirectory ) , findTemplateForDirectory )
where where


Expand All @@ -14,13 +15,23 @@ import Data.ByteString.Char8 (ByteString)
import Data.List import Data.List
import Data.Maybe import Data.Maybe
import Data.Monoid import Data.Monoid
import Happstack.Server
import Prelude hiding (catch) import Prelude hiding (catch)
import Text.StringTemplate import Text.StringTemplate
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
import Blaaargh.Types import Blaaargh.Types
import Blaaargh.Util.Templates import Blaaargh.Util.Templates




------------------------------------------------------------------------------
findFourOhFourTemplate :: ServerPartT BlaaarghMonad (Maybe Template)
findFourOhFourTemplate = do
templates <- lift $ liftM blaaarghTemplates get
pathList <- askRq >>= return . map B.pack . rqPaths

lift $ cascadingTemplateFind pathList "404"


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
findTemplateForPost :: [ByteString] -- ^ path to the post, relative findTemplateForPost :: [ByteString] -- ^ path to the post, relative
-- to the "content/" directory; -- to the "content/" directory;
Expand All @@ -31,25 +42,21 @@ findTemplateForPost :: [ByteString] -- ^ path to the post, relative
-> BlaaarghMonad (Maybe (Template)) -> BlaaarghMonad (Maybe (Template))
findTemplateForPost pathList = do findTemplateForPost pathList = do
xformTmpl <- liftM blaaarghExtraTmpl get xformTmpl <- liftM blaaarghExtraTmpl get
templates <- liftM blaaarghTemplates get
assert (not $ null pathList) (return ()) assert (not $ null pathList) (return ())


mbT <- findFirstMatchingTemplate templatesToSearch let ft = First $ lookupTmpl templates firstTmpl
st <- cascadingTemplateFind pathList "post" >>= return . First
let mbT = getFirst (ft `mappend` st)

return $ xformTmpl `fmap` mbT return $ xformTmpl `fmap` mbT


where where
postName = last pathList postName = last pathList


-- if post is at "foo/bar/baz.md", then containingDirs contains
-- [["foo","bar"], ["foo"], []]
containingDirs = tail . reverse . inits $ pathList

-- search for a template specific to this post first, then walk up -- search for a template specific to this post first, then walk up
-- the directory structure looking for a template named "post" -- the directory structure looking for a template named "post"

firstTmpl = (listToPath $ init pathList, postName)
firstTmpl = (listToPath $ head containingDirs, postName)

templatesToSearch = firstTmpl :
map (\d -> (listToPath d, "post")) containingDirs




------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Expand Down Expand Up @@ -95,6 +102,20 @@ findFirstMatchingTemplate templatesToSearch = do
map (First . lookupTmpl templates) templatesToSearch map (First . lookupTmpl templates) templatesToSearch




------------------------------------------------------------------------------
cascadingTemplateFind :: [ByteString]
-> ByteString
-> BlaaarghMonad (Maybe (StringTemplate ByteString))
cascadingTemplateFind directories templateName = do
templates <- liftM blaaarghTemplates get
assert (not $ null directories) (return ())


findFirstMatchingTemplate templatesToSearch


where
-- if requested "foo/bar/baz", then containingDirs contains
-- [["foo","bar"], ["foo"], []]
containingDirs = tail . reverse . inits $ directories


templatesToSearch = map (\d -> (listToPath d, templateName))
containingDirs

0 comments on commit 1fa756f

Please sign in to comment.