2012-05-30 17:47:41 +00:00
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
2012-05-29 00:48:40 +00:00
|
|
|
|
{-# OPTIONS_GHC -fno-warn-missing-fields #-}
|
|
|
|
|
|
2013-05-28 13:47:36 +00:00
|
|
|
|
-- | This module contains Shakespearean (see "Text.Shakespeare") templates
|
|
|
|
|
-- for Elm. It introduces type-safe compile-time variable and URL
|
|
|
|
|
-- interpolation. A typeclass @'ToElm'@ is provided for interpolated
|
|
|
|
|
-- variables.
|
2012-05-30 17:47:41 +00:00
|
|
|
|
--
|
2013-05-28 13:47:36 +00:00
|
|
|
|
-- Further reading on Shakespearean templates:
|
|
|
|
|
-- <http://www.yesodweb.com/book/templates>
|
2012-05-30 17:47:41 +00:00
|
|
|
|
--
|
|
|
|
|
-- Further reading on Elm: <http://elm-lang.org>
|
|
|
|
|
module Language.Elm.Quasi
|
|
|
|
|
( -- * Functions
|
|
|
|
|
-- ** Template-Reading Functions
|
2012-05-30 19:49:46 +00:00
|
|
|
|
-- |These QuasiQuoters return functions of the type @(t -> 'Elm')@
|
|
|
|
|
-- where @t@ is the URL rendering function if type-safe URLs are used.
|
|
|
|
|
--
|
2013-05-28 13:47:36 +00:00
|
|
|
|
-- A usage example for both type-safe (Yesod) and standard path segment
|
|
|
|
|
-- (Happstack) URLs is provided in the Examples folder in the Git
|
|
|
|
|
-- repository.
|
2012-05-30 17:47:41 +00:00
|
|
|
|
elm
|
|
|
|
|
, elmFile
|
|
|
|
|
, elmFileReload
|
|
|
|
|
|
|
|
|
|
-- * Datatypes
|
|
|
|
|
, Elm (..)
|
|
|
|
|
|
|
|
|
|
-- * Typeclass for interpolated variables
|
|
|
|
|
, ToElm (..)
|
|
|
|
|
|
|
|
|
|
-- ** Rendering Functions
|
|
|
|
|
, renderElm
|
|
|
|
|
|
|
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
import Language.Haskell.TH.Quote (QuasiQuoter (..))
|
|
|
|
|
import Language.Haskell.TH.Syntax
|
|
|
|
|
import Data.Text.Lazy.Builder (Builder, fromText, toLazyText, fromLazyText)
|
|
|
|
|
import Data.Monoid
|
|
|
|
|
import qualified Data.Text as TS
|
|
|
|
|
import qualified Data.Text.Lazy as TL
|
|
|
|
|
import Text.Shakespeare
|
|
|
|
|
|
2012-05-30 19:49:46 +00:00
|
|
|
|
-- | Render Elm to lazy Text.
|
2012-05-30 17:47:41 +00:00
|
|
|
|
renderElm :: Elm -> TL.Text
|
|
|
|
|
renderElm (Elm b) = toLazyText b
|
|
|
|
|
|
|
|
|
|
-- | Newtype wrapper of 'Builder'.
|
|
|
|
|
newtype Elm = Elm { unElm :: Builder }
|
|
|
|
|
deriving Monoid
|
|
|
|
|
|
2012-05-30 19:49:46 +00:00
|
|
|
|
-- | A typeclass for types that can be interpolated in Elm templates.
|
2012-05-30 17:47:41 +00:00
|
|
|
|
class ToElm a where
|
|
|
|
|
toElm :: a -> Builder
|
|
|
|
|
instance ToElm [Char] where toElm = fromLazyText . TL.pack
|
|
|
|
|
instance ToElm TS.Text where toElm = fromText
|
|
|
|
|
instance ToElm TL.Text where toElm = fromLazyText
|
|
|
|
|
|
|
|
|
|
elmSettings :: Q ShakespeareSettings
|
|
|
|
|
elmSettings = do
|
|
|
|
|
toJExp <- [|toElm|]
|
|
|
|
|
wrapExp <- [|Elm|]
|
|
|
|
|
unWrapExp <- [|unElm|]
|
|
|
|
|
return $ defaultShakespeareSettings { toBuilder = toJExp
|
|
|
|
|
, wrap = wrapExp
|
|
|
|
|
, unwrap = unWrapExp
|
2012-05-29 00:48:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-30 19:49:46 +00:00
|
|
|
|
-- |QuasiQuoter for embedding Elm code inside of Haskell code.
|
|
|
|
|
--
|
|
|
|
|
-- Usage:
|
2012-06-02 21:00:24 +00:00
|
|
|
|
-- @[elm|main = plaintext \"Some elm code\"|]@
|
2012-05-30 17:47:41 +00:00
|
|
|
|
elm :: QuasiQuoter
|
|
|
|
|
elm = QuasiQuoter { quoteExp = \s -> do
|
|
|
|
|
rs <- elmSettings
|
|
|
|
|
quoteExp (shakespeare rs) s
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-30 19:49:46 +00:00
|
|
|
|
-- |A Template Haskell function for embedding Elm code from external
|
|
|
|
|
-- .elm files.
|
|
|
|
|
--
|
|
|
|
|
-- Usage:
|
2012-06-02 21:00:24 +00:00
|
|
|
|
-- @$(elmFile \"elm_source/index.elm\")@
|
2012-05-30 17:47:41 +00:00
|
|
|
|
elmFile :: FilePath -> Q Exp
|
|
|
|
|
elmFile fp = do
|
|
|
|
|
rs <- elmSettings
|
|
|
|
|
shakespeareFile rs fp
|
|
|
|
|
|
|
|
|
|
elmFileReload :: FilePath -> Q Exp
|
|
|
|
|
elmFileReload fp = do
|
|
|
|
|
rs <- elmSettings
|
|
|
|
|
shakespeareFileReload rs fp
|