Created
February 6, 2012 12:13
-
-
Save gregwebs/1751814 to your computer and use it in GitHub Desktop.
Yesod Static File generator.
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
import Prelude | |
import Yesod.Routes.Parse (staticPageRoutes) | |
import Yesod hiding (Request) | |
import Text.Hamlet | |
import Network.Wai | |
import Network.Wai.Test | |
import Data.Conduit (runResourceT) | |
import Blaze.ByteString.Builder (toLazyByteString) | |
import qualified Data.ByteString.Char8 as BS8 | |
import qualified Data.ByteString.Lazy as LBS | |
import Yesod.Markdown | |
data StaticPages = StaticPages | |
mkYesodStaticPages "StaticPages" [staticPageRoutes| | |
/pages/ PagesR GET | |
/pages/#String PageR GET | |
/pages/ | |
about | |
data | |
faq | |
|] | |
instance Yesod StaticPages where | |
approot _ = "" | |
defaultLayout widget = do | |
pc <- widgetToPageContent $ do | |
addWidget widget | |
hamletToRepHtml $(hamletFile "templates/static/layout.hamlet") | |
renderMarkdownFile :: String -> IO String | |
renderMarkdownFile file = -- readFile $ "templates/static/" ++ file ++ ".markdown" | |
fmap markdownToHtmlTrusted (markdownFromFile "templates/static/" ++ file ++ ".markdown") | |
getPageR :: String -> Handler RepHtml | |
getPageR page = do | |
defaultLayout $ do | |
content <- liftIO $ renderMarkdownFile page | |
toWidget [shamlet|#{content}|] | |
getPagesR :: Handler RepHtml | |
getPagesR = do | |
defaultLayout $ do | |
content <- liftIO $ readFile "templates/static/index.html" | |
[whamlet|#{content}|] | |
main :: IO () | |
main = renderStaticPages "public/" | |
renderStaticPages :: FilePath -> IO () | |
renderStaticPages directory = do | |
app <- toWaiAppPlain StaticPages | |
flip mapM_ staticPageRoutePaths $ \path -> do | |
let req = setRawPathInfo defaultRequest $ BS8.pack $ path | |
rsp <- runResourceT $ app req | |
case rsp of | |
ResponseBuilder _ _ b -> | |
LBS.writeFile (directory ++ (BS8.unpack $ rawPathInfo req) ++ ".html") $ toLazyByteString b | |
_ -> error "expected ResponseBuilder" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment