From 41c69acf6467ee5d47f4516fc82efd6af4fbbfd1 Mon Sep 17 00:00:00 2001 From: Eduardo Trujillo <ed@chromabits.com> Date: Sat, 26 Dec 2020 14:49:06 -0800 Subject: [PATCH] feat(Server): Allow output format to be specified --- src/Shift/Server.hs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Shift/Server.hs b/src/Shift/Server.hs index 0e69ee4..3eaff0d 100644 --- a/src/Shift/Server.hs +++ b/src/Shift/Server.hs @@ -1,16 +1,22 @@ {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} module Shift.Server (runServer) where -import Web.Scotty (scotty, get, param, html) +import CMarkGFM (commonmarkToHtml, nodeToCommonmark, nodeToHtml) +import Control.Monad.IO.Class (MonadIO (liftIO)) +import Data.Text.Lazy (fromChunks) import Shift.CLI (ShiftOptions) import Shift.Git (renderToNode) -import Control.Monad.IO.Class (MonadIO(liftIO)) -import CMarkGFM (nodeToHtml, nodeToHtml, commonmarkToHtml) -import Data.Text.Lazy (fromChunks) +import Web.Scotty (rescue, rescue, get, html, param, scotty, text) runServer :: ShiftOptions -> IO () runServer options = scotty 3000 $ - get "/" $ do - node <- liftIO $ renderToNode options - html $ fromChunks [nodeToHtml [] [] node] + get "/" $ do + (asMarkdown :: Bool) <- param "as_markdown" `rescue` (\_ -> pure False) + + node <- liftIO $ renderToNode options + + if asMarkdown + then text $ fromChunks [nodeToCommonmark [] Nothing node] + else html $ fromChunks [nodeToHtml [] [] node] -- GitLab