Created
April 29, 2012 21:50
-
-
Save jbpotonnier/2553421 to your computer and use it in GitHub Desktop.
Play with Scotty web framework
This file contains hidden or 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
.hsenv |
This file contains hidden or 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
{-# LANGUAGE OverloadedStrings #-} | |
import Text.Pandoc ( | |
readMarkdown, writeHtmlString, | |
defaultParserState, defaultWriterOptions) | |
import Web.Scotty | |
import Network.Wai.Middleware.RequestLogger | |
import System.Directory (getDirectoryContents) | |
import Data.Monoid (mconcat) | |
import Control.Monad.Trans (liftIO) | |
import qualified Data.Text.Lazy as T | |
markdownToHtml :: String -> T.Text | |
markdownToHtml = T.pack . writeHtmlString defaultWriterOptions . readMarkdown defaultParserState | |
page :: T.Text -> T.Text -> T.Text | |
page title body = mconcat ["<html><head><title>", title ,"</title></head><body>", | |
body, | |
"</body></html>"] | |
main :: IO () | |
main = scotty 3000 $ do | |
middleware logStdoutDev | |
get "/entry/new" $ do | |
html $ page "New page" $ mconcat ["<form method=POST action=\"/newpage\">", | |
"<label for=\"title\">Title : </label>", | |
"<input type=\"text\"text name=\"title\" id=\"title\">", | |
"<br/>", | |
"<label for=\"content\">Content : </label>", | |
"<textarea name=\"content\" id=\"content\" cols=\"80\" rows=\"20\"></textarea>", | |
"<br/>", | |
"<input type=\"submit\">", | |
"</form>" ] | |
get "/entry/:entry" $ do | |
entry <- param "entry" | |
content <- liftIO $ readFile $ mconcat ["./pages/", entry] | |
html $ (page (T.pack entry) . markdownToHtml) content | |
get "/entries" $ do | |
entries <- liftIO $ getDirectoryContents "./pages/" | |
html $ mconcat ["<ul>", mconcat $ map (item . T.pack) entries, "</ul>"] | |
where | |
item i = mconcat ["<li>", "<a href=\"entry/", i, "\">", i, "</a>", "</li>"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment