Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend preconversion #190

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions Text/Julius.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,8 @@ javascriptSettings = do
wrapExp <- [|Javascript|]
unWrapExp <- [|unJavascript|]
asJavascriptUrl' <- [|asJavascriptUrl|]
return $ defaultShakespeareSettings { toBuilder = toJExp
, wrap = wrapExp
, unwrap = unWrapExp
, modifyFinalValue = Just asJavascriptUrl'
}
return $ (defaultShakespeareSettings toJExp wrapExp unWrapExp)
{ modifyFinalValue = Just asJavascriptUrl' }

js, julius :: QuasiQuoter
js = QuasiQuoter { quoteExp = \s -> do
Expand Down Expand Up @@ -210,4 +207,7 @@ jsFileDebug = jsFileReload
-- | Determine which identifiers are used by the given template, useful for
-- creating systems like yesod devel.
juliusUsedIdentifiers :: String -> [(Deref, VarType)]
juliusUsedIdentifiers = shakespeareUsedIdentifiers defaultShakespeareSettings
juliusUsedIdentifiers = shakespeareUsedIdentifiers partialSettings
where
unused = error "Unused setting"
partialSettings = defaultShakespeareSettings unused unused unused
15 changes: 11 additions & 4 deletions Text/Shakespeare.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ data WrapInsertion = WrapInsertion {

data PreConversion = ReadProcess String [String]
| Id


| ConvertAction (String -> IO String)

data ShakespeareSettings = ShakespeareSettings
{ varChar :: Char
Expand All @@ -143,11 +142,18 @@ data ShakespeareSettings = ShakespeareSettings
-- meaningful error messages.
}

defaultShakespeareSettings :: ShakespeareSettings
defaultShakespeareSettings = ShakespeareSettings {
defaultShakespeareSettings
:: Exp -- ^ An Exp that will convert variables to 'Builder'
-> Exp -- ^ An Exp that converts 'Builder' to the type being generated
-> Exp -- ^ The reversal of the previous conversion argument
-> ShakespeareSettings
defaultShakespeareSettings tobuild wrapexp unwrapexp = ShakespeareSettings {
varChar = '#'
, urlChar = '@'
, intChar = '^'
, toBuilder = tobuild
, wrap = wrapexp
, unwrap = unwrapexp
, justVarInterpolation = False
, preConversion = Nothing
, modifyFinalValue = Nothing
Expand Down Expand Up @@ -267,6 +273,7 @@ preFilter mfp ShakespeareSettings {..} template =
withVars = (addVars mWrapI vars parsed)
in applyVars mWrapI vars `fmap` case convert of
Id -> return withVars
ConvertAction act -> act template
ReadProcess command args ->
readProcessError command args withVars mfp
where
Expand Down
20 changes: 7 additions & 13 deletions Text/Shakespeare/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ settings = do
toTExp <- [|toText|]
wrapExp <- [|id|]
unWrapExp <- [|id|]
return $ defaultShakespeareSettings { toBuilder = toTExp
, wrap = wrapExp
, unwrap = unWrapExp
}

return $ defaultShakespeareSettings toTExp wrapExp unWrapExp

stext, lt, st, text, lbt, sbt :: QuasiQuoter
stext =
Expand Down Expand Up @@ -126,14 +122,12 @@ codegenSettings = do
toTExp <- [|toText|]
wrapExp <- [|id|]
unWrapExp <- [|id|]
return $ defaultShakespeareSettings { toBuilder = toTExp
, wrap = wrapExp
, unwrap = unWrapExp
, varChar = '~'
, urlChar = '*'
, intChar = '&'
, justVarInterpolation = True -- always!
}
return $ (defaultShakespeareSettings toTExp wrapExp unWrapExp)
{ varChar = '~'
, urlChar = '*'
, intChar = '&'
, justVarInterpolation = True -- always!
}

-- | codegen is designed for generating Yesod code, including templates
-- So it uses different interpolation characters that won't clash with templates.
Expand Down
18 changes: 11 additions & 7 deletions test/Text/Shakespeare/BaseSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import Data.Text.Lazy (pack)

-- run :: Text.Parsec.Prim.Parsec Text.Parsec.Pos.SourceName () c -> Text.Parsec.Pos.SourceName -> c

partialShakespeareSettings = defaultShakespeareSettings unused unused unused
where
unused = error "Unused setting"

spec :: Spec
spec = do
let preFilterN = preFilter Nothing
Expand All @@ -24,7 +28,7 @@ spec = do
-}

it "preFilter off" $ do
preFilterN defaultShakespeareSettings template
preFilterN partialShakespeareSettings template
`shouldReturn` template

it "preFilter on" $ do
Expand All @@ -41,11 +45,11 @@ spec = do

it "reload" $ do
let helper input = $(do
shakespeareFileReload defaultShakespeareSettings
{ toBuilder = VarE 'pack
, wrap = VarE 'toLazyText
, unwrap = VarE 'fromLazyText
} "test/reload.txt") undefined
shakespeareFileReload
(defaultShakespeareSettings (VarE 'pack)
(VarE 'toLazyText)
(VarE 'fromLazyText))
"test/reload.txt") undefined
helper "here1" `shouldBe` pack "here1\n"
helper "here2" `shouldBe` pack "here2\n"

Expand All @@ -54,7 +58,7 @@ spec = do
urlString = parseUrlString '@' '?'
intString = parseIntString '^'

preConversionSettings = defaultShakespeareSettings {
preConversionSettings = partialShakespeareSettings {
preConversion = Just PreConvert {
preConvert = Id
, preEscapeIgnoreBalanced = "'\""
Expand Down