shakespeare 1.0.3.1 → 1.0.4
raw patch · 3 files changed
+35/−29 lines, 3 files
Files
- Text/Shakespeare.hs +29/−22
- shakespeare.cabal +1/−1
- test/ShakespeareBaseTest.hs +5/−6
Text/Shakespeare.hs view
@@ -44,6 +44,8 @@ import qualified Data.Text as TS import qualified Data.Text.Lazy as TL import Text.Shakespeare.Base+import Prelude hiding (catch)+import Control.Exception (throwIO, catch) -- for pre conversion import System.Process (readProcess)@@ -99,8 +101,7 @@ , wrapInsertionSeparator :: String , wrapInsertionStartClose :: String , wrapInsertionEnd :: String- , wrapInsertionApplyBegin :: String- , wrapInsertionApplyClose :: String+ , wrapInsertionAddParens :: Bool } data PreConversion = ReadProcess String [String]@@ -138,8 +139,8 @@ [|PreConvert $(lift convert) $(lift ignore) $(lift comment) $(lift wrapInsertion)|] instance Lift WrapInsertion where- lift (WrapInsertion indent sb sep sc e ab ac) =- [|WrapInsertion $(lift indent) $(lift sb) $(lift sep) $(lift sc) $(lift e) $(lift ab) $(lift ac)|]+ lift (WrapInsertion indent sb sep sc e wp) =+ [|WrapInsertion $(lift indent) $(lift sb) $(lift sep) $(lift sc) $(lift e) $(lift wp)|] instance Lift PreConversion where lift (ReadProcess command args) =@@ -213,24 +214,25 @@ preFilter ShakespeareSettings {..} template = case preConversion of Nothing -> return template- Just pre@(PreConvert convert _ _ mwi) ->+ Just pre@(PreConvert convert _ _ mWrapI) -> if all isSpace template then return template else let (groups, rvars) = eShowErrors $ parse- (parseConvertWrapInsertion mwi pre)+ (parseConvertWrapInsertion mWrapI pre) template- (indentedTemplate mwi)+ template vars = reverse rvars parsed = mconcat groups- in applyVars mwi vars `fmap` (case convert of- Id -> return - ReadProcess command args -> readProcess command args- ) (addVars mwi vars parsed)+ withVars = (addVars mWrapI vars parsed)+ in applyVars mWrapI vars `fmap` case convert of+ Id -> return withVars+ ReadProcess command args ->+ readProcess command args withVars+ `catch` (\ex -> print withVars >> throwIO (ex :: IOError)) where- indentedTemplate Nothing = template- indentedTemplate (Just wi) = addIndent $ wrapInsertionIndent wi+ addIndent :: Maybe String -> String -> String+ addIndent Nothing str = str+ addIndent (Just indent) str = mapLines (\line -> indent <> line) str where- addIndent Nothing = template- addIndent (Just indent) = mapLines (\line -> indent <> line) template mapLines f = unlines . map f . lines shakespeare_prefix = "shakespeare_var_"@@ -238,21 +240,26 @@ shakespeare_var_conversion (_:'{':str) = shakespeare_prefix <> filter isAlphaNum (init str) shakespeare_var_conversion err = error $ "did not expect: " <> err + applyVars _ [] str = str applyVars Nothing _ str = str- applyVars _ [] str = str applyVars (Just WrapInsertion {..}) vars str =- reverse (dropWhile (\c -> c == ';' || isSpace c) (reverse str))- <> wrapInsertionApplyBegin- <> (mconcat $ intersperse wrapInsertionSeparator vars)- <> wrapInsertionApplyClose+ (if wrapInsertionAddParens then "(" else "")+ <> removeTrailingSemiColon+ <> (if wrapInsertionAddParens then ")" else "")+ <> "("+ <> (mconcat $ intersperse ", " vars)+ <> ");\n"+ where + removeTrailingSemiColon = reverse $+ dropWhile (\c -> c == ';' || isSpace c) (reverse str) + addVars _ [] str = str addVars Nothing _ str = str- addVars _ [] str = str addVars (Just WrapInsertion {..}) vars str = wrapInsertionStartBegin <> (mconcat $ intersperse wrapInsertionSeparator $ map shakespeare_var_conversion vars) <> wrapInsertionStartClose- <> str+ <> addIndent wrapInsertionIndent str <> wrapInsertionEnd parseConvertWrapInsertion Nothing = parseConvert id
shakespeare.cabal view
@@ -1,5 +1,5 @@ name: shakespeare-version: 1.0.3.1+version: 1.0.4 license: MIT license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>
test/ShakespeareBaseTest.hs view
@@ -26,11 +26,11 @@ it "preFilter on" $ do preFilter preConversionSettings template `shouldReturn`- "(function(shakespeare_var_var, shakespeare_var_url, shakespeare_var_int){unchanged shakespeare_var_var shakespeare_var_url shakespeare_var_int})(#{var}, @{url}, ^{int})"+ "(function(shakespeare_var_var, shakespeare_var_url, shakespeare_var_int){unchanged shakespeare_var_var shakespeare_var_url shakespeare_var_int})(#{var}, @{url}, ^{int});\n" it "preFilter ignore quotes" $ do preFilter preConversionSettings templateQuote `shouldReturn`- "(function(shakespeare_var_url){unchanged '#{var}' shakespeare_var_url '^{int}'})(@{url})"+ "(function(shakespeare_var_url){unchanged '#{var}' shakespeare_var_url '^{int}'})(@{url});\n" it "preFilter ignore comments" $ do preFilter preConversionSettings templateCommented@@ -48,12 +48,11 @@ , preEscapeIgnoreLine = "&" , wrapInsertion = Just WrapInsertion { wrapInsertionIndent = Nothing- , wrapInsertionStartBegin = "(function("+ , wrapInsertionStartBegin = "function(" , wrapInsertionSeparator = ", " , wrapInsertionStartClose = "){"- , wrapInsertionEnd = "})"- , wrapInsertionApplyBegin = "("- , wrapInsertionApplyClose = ")"+ , wrapInsertionEnd = "}"+ , wrapInsertionAddParens = True } } }