diff --git a/examples/pretty-yaml.hs b/examples/pretty-yaml.hs new file mode 100644 index 000000000..132ff5a2d --- /dev/null +++ b/examples/pretty-yaml.hs @@ -0,0 +1,38 @@ +{-# LANGUAGE QuasiQuotes #-} +import Yesod +import Data.Object.Yaml +import Network.Wai.Handler.SimpleServer +import Web.Encodings +import qualified Data.ByteString as B +import qualified Data.ByteString.Lazy as L + +data PY = PY TemplateGroup +instance YesodTemplate PY where + getTemplateGroup (PY tg) = tg + defaultTemplateAttribs _ _ = return +instance Yesod PY where + resources = [$mkResources| +/: + GET: homepageH + POST: showYamlH +|] + +homepageH :: Handler PY RepHtml +homepageH = templateHtml "pretty-yaml" return + +showYamlH :: Handler PY RepHtmlJson +showYamlH = do + rr <- getRawRequest + (_, files) <- liftIO $ rawRequestBody rr + fi <- case lookup "yaml" files of + Nothing -> invalidArgs [("yaml", "Missing input")] + Just x -> return x + to <- decode $ B.concat $ L.toChunks $ fileContent fi + let ho' = fmap Text to + templateHtmlJson "pretty-yaml" ho' $ \ho -> + return . setHtmlAttrib "yaml" (Scalar $ cs ho :: HtmlObject) + +main :: IO () +main = do + putStrLn "Running..." + loadTemplateGroup "examples" >>= toWaiApp . PY >>= run 3000 diff --git a/examples/pretty-yaml.st b/examples/pretty-yaml.st new file mode 100644 index 000000000..68e1e604e --- /dev/null +++ b/examples/pretty-yaml.st @@ -0,0 +1,16 @@ + + + + +Pretty YAML + + +
+File name: + +
+$if(yaml)$ +
$yaml$
+$endif$ + +