-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from cdepillabout/template-haskell-ede
Template haskell ede. Closes #6.
- Loading branch information
Showing
8 changed files
with
105 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# We use 4 space indents. | ||
indent-size: 4 | ||
line-length: 80 | ||
force-trailing-newline: false |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,3 @@ | ||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
|
||
module Kucipong.Monad.OtherInstances where | ||
|
||
import Kucipong.Prelude | ||
|
||
import Control.Monad.Random ( MonadRandom(..) ) | ||
|
||
instance MonadRandom m => MonadRandom (LoggingT m) where | ||
getRandom = lift getRandom | ||
getRandomR = lift . getRandomR | ||
getRandoms = lift getRandoms | ||
getRandomRs = lift . getRandomRs | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{-# LANGUAGE QuasiQuotes #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Kucipong.RenderTemplate where | ||
|
||
import Kucipong.Prelude hiding ( try ) | ||
|
||
import Control.Exception ( try ) | ||
import Language.Haskell.TH | ||
( Exp, Q, appE, litE, lookupValueName, mkName, stringL, varE ) | ||
import Language.Haskell.TH.Syntax ( addDependentFile ) | ||
import Network.HTTP.Types ( internalServerError500 ) | ||
import Text.EDE ( Template, eitherParse, eitherRender, fromPairs ) | ||
import Web.Spock ( html, setStatus ) | ||
|
||
import Kucipong.Util ( fromEitherM ) | ||
|
||
templateDirectory :: FilePath | ||
templateDirectory = "frontend" </> "dist" | ||
|
||
unsafeFromRight :: Show e => Either e a -> a | ||
unsafeFromRight (Right a) = a | ||
unsafeFromRight (Left err) = error $ | ||
"Called unsafeFromRight, but we got a left with this value: " <> show err | ||
|
||
renderTemplateFromEnv :: String -> Q Exp | ||
renderTemplateFromEnv filename = do | ||
addDependentFile fullFilePath | ||
eitherRawTemplate <- liftIO . try $ readFile fullFilePath | ||
rawTemplate <- fromEitherM handleTemplateFileRead eitherRawTemplate | ||
let eitherParsedTemplate = eitherParse rawTemplate | ||
-- Check to make sure the template can be parsed correctly. Return an | ||
-- error during compile time to the user if it cannot. | ||
void $ fromEitherM handleIncorrectTemplate eitherParsedTemplate | ||
templateExp <- [e| unsafeFromRight $ eitherParse rawTemplate |] | ||
eitherRenderLambdaExp <- [e| eitherRender $(pure templateExp) |] | ||
[e| \environmentObject -> case $(pure eitherRenderLambdaExp) environmentObject of | ||
Left err -> do | ||
setStatus internalServerError500 | ||
html $ "<h3>Error with rendering template:</h3>" <> pack err | ||
Right bytestring -> html $ toStrict bytestring |] | ||
where | ||
-- This is the full path of the template file. | ||
fullFilePath :: FilePath | ||
fullFilePath = templateDirectory </> filename | ||
|
||
handleTemplateFileRead :: SomeException -> Q a | ||
handleTemplateFileRead exception = fail $ | ||
"exception occured when trying to read the template file \"" <> | ||
fullFilePath <> | ||
":\n" <> | ||
show exception | ||
|
||
handleIncorrectTemplate :: String -> Q a | ||
handleIncorrectTemplate errorMsg = fail $ | ||
"exception occured when trying to parse the template file \"" <> | ||
fullFilePath <> ":\n" <> errorMsg |
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