shakespeare 0.10.3.1 → 0.11
raw patch · 3 files changed
+38/−19 lines, 3 filesdep ~shakespearenew-uploader
Dependency ranges changed: shakespeare
Files
- Text/Shakespeare.hs +31/−17
- shakespeare.cabal +2/−2
- test.hs +5/−0
Text/Shakespeare.hs view
@@ -13,7 +13,7 @@ , defaultShakespeareSettings , shakespeare , shakespeareFile- , shakespeareFileDebug+ , shakespeareFileReload -- * low-level , shakespeareFromString , RenderUrl@@ -58,14 +58,16 @@ -- During the pre-conversion we first modify all Haskell insertions -- so that they will be ignored by the Coffeescript compiler (backticks). -- So %{var} is change to `%{var}` using the preEscapeBegin and preEscapeEnd.--- preEscapeIgnore is used to not insert backtacks for variable already inside strings or backticks.+-- preEscapeIgnoreBalanced is used to not insert backtacks for variable already inside strings or backticks. -- coffeescript will happily ignore the interpolations, and backticks would not be treated as escaping in that context.+-- preEscapeIgnoreLine was added to ignore comments (which in Coffeescript begin with a '#') data PreConvert = PreConvert { preConvert :: PreConversion , preEscapeBegin :: String , preEscapeEnd :: String- , preEscapeIgnore :: [Char]+ , preEscapeIgnoreBalanced :: [Char]+ , preEscapeIgnoreLine :: [Char] } data PreConversion = ReadProcess String [String]@@ -94,8 +96,8 @@ } instance Lift PreConvert where- lift (PreConvert convert begin end ignore) =- [|PreConvert $(lift convert) $(lift begin) $(lift end) $(lift ignore)|]+ lift (PreConvert convert begin end ignore comment) =+ [|PreConvert $(lift convert) $(lift begin) $(lift end) $(lift ignore) $(lift comment)|] instance Lift PreConversion where lift (ReadProcess command args) =@@ -166,16 +168,16 @@ preFilter ShakespeareSettings {..} s = case preConversion of Nothing -> return s- Just pre@(PreConvert convert _ _ _) ->+ Just pre@(PreConvert convert _ _ _ _) -> let parsed = mconcat $ eShowErrors $ parse (parseConvert pre) s s in case convert of Id -> return parsed ReadProcess command args ->- readProcess command (args ++ [parsed]) []+ readProcess command args parsed where parseConvert PreConvert {..} = many1 $ choice $- (map (try . escapedParse) preEscapeIgnore) ++- [mainParser preEscapeIgnore]+ map (try . escapedParse) preEscapeIgnoreBalanced +++ [mainParser] where escapedParse ignoreC = do@@ -183,16 +185,28 @@ inside <- many $ noneOf [ignoreC] _<- char ignoreC return $ ignoreC:inside ++ [ignoreC]- -- return $ ignoreC:(eShowErrors $ parse (mainParser escapeNone) "" inside) ++ [ignoreC] - mainParser i = parseVar' <|> parseUrl' <|> parseInt' <|> parseChar' i+ mainParser =+ parseVar' <|>+ parseUrl' <|>+ parseInt' <|>+ parseCommentLine preEscapeIgnoreLine <|>+ parseChar' preEscapeIgnoreLine preEscapeIgnoreBalanced+ escape str = preEscapeBegin ++ str ++ preEscapeEnd escapeRight = either id escape + newLine = "\r\n"+ parseCommentLine cs = do+ begin <- oneOf cs+ comment <- many $ noneOf newLine+ return $ begin : comment+ parseVar' = escapeRight `fmap` parseVarString varChar parseUrl' = escapeRight `fmap` parseUrlString urlChar '?' parseInt' = escapeRight `fmap` parseIntString intChar- parseChar' i = many1 (noneOf ([varChar, urlChar, intChar] ++ i))+ parseChar' comments ignores =+ many1 (noneOf ([varChar, urlChar, intChar] ++ comments ++ ignores)) contentsToShakespeare :: ShakespeareSettings -> [Content] -> Q Exp@@ -213,7 +227,7 @@ contentToBuilder _ (ContentRaw s') = do ts <- [|fromText . TS.pack|] return $ wrap rs `AppE` (ts `AppE` LitE (StringL s'))- contentToBuilder _ (ContentVar d) = do+ contentToBuilder _ (ContentVar d) = return $ wrap rs `AppE` (toBuilder rs `AppE` derefToExp [] d) contentToBuilder r (ContentUrl d) = do ts <- [|fromText|]@@ -222,7 +236,7 @@ ts <- [|fromText|] up <- [|\r' (u, p) -> r' u p|] return $ wrap rs `AppE` (ts `AppE` (up `AppE` VarE r `AppE` derefToExp [] d))- contentToBuilder r (ContentMix d) = do+ contentToBuilder r (ContentMix d) = return $ derefToExp [] d `AppE` VarE r shakespeare :: ShakespeareSettings -> QuasiQuoter@@ -231,7 +245,7 @@ shakespeareFromString :: ShakespeareSettings -> String -> Q Exp shakespeareFromString r str = do s <- qRunIO $ preFilter r str- contentsToShakespeare r $ contentFromString r $ s+ contentsToShakespeare r $ contentFromString r s shakespeareFile :: ShakespeareSettings -> FilePath -> Q Exp shakespeareFile r fp = do@@ -254,8 +268,8 @@ | EUrlParam (url, [(TS.Text, TS.Text)]) | EMixin (Shakespeare url) -shakespeareFileDebug :: ShakespeareSettings -> FilePath -> Q Exp-shakespeareFileDebug rs fp = do+shakespeareFileReload :: ShakespeareSettings -> FilePath -> Q Exp+shakespeareFileReload rs fp = do str <- readFileQ fp s <- qRunIO $ preFilter rs str let b = concatMap getVars $ contentFromString rs s
shakespeare.cabal view
@@ -1,5 +1,5 @@ name: shakespeare-version: 0.10.3.1+version: 0.11 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>@@ -56,4 +56,4 @@ source-repository head type: git- location: git://github.com/yesodweb/hamlet.git+ location: git://github.com/yesodweb/shakespeare.git
+ test.hs view
@@ -0,0 +1,5 @@+import Test.Hspec.Monadic+import ShakespeareBaseTest (specs)++main :: IO ()+main = hspecX $ specs