Skip to content

Instantly share code, notes, and snippets.

@gregwebs
Created February 6, 2012 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregwebs/1751814 to your computer and use it in GitHub Desktop.
Save gregwebs/1751814 to your computer and use it in GitHub Desktop.
Yesod Static File generator.
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