{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-missing-fields #-}
-- | A Shakespearean module for Roy, introducing type-safe,
-- compile-time variable and url interpolation. It is exactly the same as
-- "Text.Julius", except that the template is first compiled to Javascript with
-- the system tool @roy@.
--
-- To use this module, @roy@ must be installed on your system.
--
-- If you interpolate variables,
-- the template is first wrapped with a function containing javascript variables representing shakespeare variables,
-- then compiled with @roy@,
-- and then the value of the variables are applied to the function.
-- This means that in production the template can be compiled
-- once at compile time and there will be no dependency in your production
-- system on @roy@. 
--
-- Your code:
--
-- > let b = 1
-- > console.log(#{a} + b)
--
-- Final Result:
--
-- > ;(function(shakespeare_var_a){
-- >   var b = 1;
-- >   console.log(shakespeare_var_a + b);
-- > })(#{a});
--
-- Further reading:
--
-- 1. Shakespearean templates: <https://www.yesodweb.com/book/shakespearean-templates>
--
-- 2. Roy: <http://roy.brianmckenna.org/>
module Text.Roy
    ( -- * Functions
      -- ** Template-Reading Functions
      -- | These QuasiQuoter and Template Haskell methods return values of
      -- type @'JavascriptUrl' url@. See the Yesod book for details.
      roy
    , royFile
    , royFileReload

#ifdef TEST_EXPORT
    , roySettings
#endif
    ) where

import Language.Haskell.TH.Quote (QuasiQuoter (..))
import Language.Haskell.TH.Syntax
import Text.Shakespeare
import Text.Julius

-- | The Roy language compiles down to Javascript.
-- We do this compilation once at compile time to avoid needing to do it during the request.
-- We call this a preConversion because other shakespeare modules like Lucius use Haskell to compile during the request instead rather than a system call.
roySettings :: Q ShakespeareSettings
roySettings :: Q ShakespeareSettings
roySettings = do
  ShakespeareSettings
jsettings <- Q ShakespeareSettings
javascriptSettings
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ShakespeareSettings
jsettings { varChar :: Char
varChar = Char
'#'
  , preConversion :: Maybe PreConvert
preConversion = forall a. a -> Maybe a
Just PreConvert {
      preConvert :: PreConversion
preConvert = String -> [String] -> PreConversion
ReadProcess String
"roy" [String
"--stdio", String
"--browser"]
    , preEscapeIgnoreBalanced :: String
preEscapeIgnoreBalanced = String
"'\""
    , preEscapeIgnoreLine :: String
preEscapeIgnoreLine = String
"//"
    , wrapInsertion :: Maybe WrapInsertion
wrapInsertion = forall a. a -> Maybe a
Just WrapInsertion {
        wrapInsertionIndent :: Maybe String
wrapInsertionIndent = forall a. a -> Maybe a
Just String
"  "
      , wrapInsertionStartBegin :: String
wrapInsertionStartBegin = String
"(\\"
      , wrapInsertionSeparator :: String
wrapInsertionSeparator = String
" "
      , wrapInsertionStartClose :: String
wrapInsertionStartClose = String
" ->\n"
      , wrapInsertionEnd :: String
wrapInsertionEnd = String
")"
      , wrapInsertionAddParens :: Bool
wrapInsertionAddParens = Bool
True
      }
    }
  }

-- | Read inline, quasiquoted Roy.
roy :: QuasiQuoter
roy :: QuasiQuoter
roy = QuasiQuoter { quoteExp :: String -> Q Exp
quoteExp = \String
s -> do
    ShakespeareSettings
rs <- Q ShakespeareSettings
roySettings
    QuasiQuoter -> String -> Q Exp
quoteExp (ShakespeareSettings -> QuasiQuoter
shakespeare ShakespeareSettings
rs) String
s
    }

-- | Read in a Roy template file. This function reads the file once, at
-- compile time.
royFile :: FilePath -> Q Exp
royFile :: String -> Q Exp
royFile String
fp = do
    ShakespeareSettings
rs <- Q ShakespeareSettings
roySettings
    ShakespeareSettings -> String -> Q Exp
shakespeareFile ShakespeareSettings
rs String
fp

-- | Read in a Roy template file. This impure function uses
-- unsafePerformIO to re-read the file on every call, allowing for rapid
-- iteration.
royFileReload :: FilePath -> Q Exp
royFileReload :: String -> Q Exp
royFileReload String
fp = do
    ShakespeareSettings
rs <- Q ShakespeareSettings
roySettings
    ShakespeareSettings -> String -> Q Exp
shakespeareFileReload ShakespeareSettings
rs String
fp