WikiRoutes.hs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. {-# LANGUAGE FlexibleContexts #-}
  2. {-# LANGUAGE MultiParamTypeClasses #-}
  3. {-# LANGUAGE QuasiQuotes #-}
  4. {-# LANGUAGE TemplateHaskell #-}
  5. {-# LANGUAGE TypeFamilies #-}
  6. -- | Define our Wiki data type, routes, and the YesodWiki typeclass. Due to the
  7. -- GHC stage restriction, the routes must be declared in a separate module from
  8. -- our dispatch instance.
  9. module WikiRoutes where
  10. import Control.Monad (liftM)
  11. import Control.Monad.IO.Class (MonadIO)
  12. import Data.IORef (IORef, newIORef)
  13. import Data.Map (Map, empty)
  14. import Yesod
  15. -- | Simple Wiki datatype: just store a Map from Wiki path to the contents of
  16. -- the page.
  17. data Wiki = Wiki
  18. { wikiContent :: IORef (Map Texts Textarea)
  19. }
  20. -- | A typeclass that all master sites that want a Wiki must implement. A
  21. -- master must be able to render form messages, as we use yesod-forms for
  22. -- processing user input.
  23. class (RenderMessage master FormMessage, Yesod master) => YesodWiki master where
  24. -- | Write protection. By default, no protection.
  25. canEditPage :: Texts -> HandlerT master IO Bool
  26. canEditPage _ = return True
  27. -- | Define our routes. We'll have a homepage that lists all of the pages, a
  28. -- read route for reading a page, and an edit route.
  29. mkYesodSubData "Wiki" [parseRoutes|
  30. / WikiHomeR GET
  31. /read/*Texts WikiReadR GET
  32. /edit/*Texts WikiEditR GET POST
  33. |]
  34. -- | A convenience function for creating an empty Wiki.
  35. newWiki :: MonadIO m => m Wiki
  36. newWiki = Wiki `liftM` liftIO (newIORef empty)