module Text.Julius
(
js
, julius
, juliusFile
, jsFile
, juliusFileDebug
, jsFileDebug
, juliusFileReload
, jsFileReload
, JavascriptUrl
, Javascript (..)
, RawJavascript (..)
, ToJavascript (..)
, RawJS (..)
, renderJavascript
, renderJavascriptUrl
, javascriptSettings
, juliusUsedIdentifiers
, asJavascriptUrl
) 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
import Data.Aeson (Value)
import Data.Aeson.Encode (fromValue)
renderJavascript :: Javascript -> TL.Text
renderJavascript (Javascript b) = toLazyText b
renderJavascriptUrl :: (url -> [(TS.Text, TS.Text)] -> TS.Text) -> JavascriptUrl url -> TL.Text
renderJavascriptUrl r s = renderJavascript $ s r
newtype Javascript = Javascript { unJavascript :: Builder }
deriving Monoid
type JavascriptUrl url = (url -> [(TS.Text, TS.Text)] -> TS.Text) -> Javascript
asJavascriptUrl :: JavascriptUrl url -> JavascriptUrl url
asJavascriptUrl = id
class ToJavascript a where
toJavascript :: a -> Javascript
instance ToJavascript Bool where toJavascript = Javascript . fromText . TS.toLower . TS.pack . show
instance ToJavascript Value where toJavascript = Javascript . fromValue
newtype RawJavascript = RawJavascript Builder
instance ToJavascript RawJavascript where
toJavascript (RawJavascript a) = Javascript a
class RawJS a where
rawJS :: a -> RawJavascript
instance RawJS [Char] where rawJS = RawJavascript . fromLazyText . TL.pack
instance RawJS TS.Text where rawJS = RawJavascript . fromText
instance RawJS TL.Text where rawJS = RawJavascript . fromLazyText
instance RawJS Builder where rawJS = RawJavascript
instance RawJS Bool where rawJS = RawJavascript . unJavascript . toJavascript
javascriptSettings :: Q ShakespeareSettings
javascriptSettings = do
toJExp <- [|toJavascript|]
wrapExp <- [|Javascript|]
unWrapExp <- [|unJavascript|]
asJavascriptUrl' <- [|asJavascriptUrl|]
return $ defaultShakespeareSettings { toBuilder = toJExp
, wrap = wrapExp
, unwrap = unWrapExp
, modifyFinalValue = Just asJavascriptUrl'
}
js, julius :: QuasiQuoter
js = QuasiQuoter { quoteExp = \s -> do
rs <- javascriptSettings
quoteExp (shakespeare rs) s
}
julius = js
jsFile, juliusFile :: FilePath -> Q Exp
jsFile fp = do
rs <- javascriptSettings
shakespeareFile rs fp
juliusFile = jsFile
jsFileReload, juliusFileReload :: FilePath -> Q Exp
jsFileReload fp = do
rs <- javascriptSettings
shakespeareFileReload rs fp
juliusFileReload = jsFileReload
jsFileDebug, juliusFileDebug :: FilePath -> Q Exp
juliusFileDebug = jsFileReload
jsFileDebug = jsFileReload
juliusUsedIdentifiers :: String -> [(Deref, VarType)]
juliusUsedIdentifiers = shakespeareUsedIdentifiers defaultShakespeareSettings