hamlet 1.1.7 → 1.1.7.1
raw patch · 4 files changed
+63/−13 lines, 4 filesdep ~template-haskellPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: template-haskell
API changes (from Hackage documentation)
Files
- Text/Hamlet.hs +15/−7
- Text/Hamlet/Parse.hs +37/−5
- hamlet.cabal +1/−1
- test/HamletTest.hs +10/−0
Text/Hamlet.hs view
@@ -56,6 +56,7 @@ import Control.Monad (mplus) import Data.Monoid (mempty, mappend) import Control.Arrow ((***))+import Data.List (intercalate) -- | Convert some value to a list of attribute pairs. class ToAttributes a where@@ -118,10 +119,10 @@ bindingPattern (BindList is) = do (patterns, scopes) <- fmap unzip $ mapM bindingPattern is return (ListP patterns, concat scopes)-bindingPattern (BindConstr (Ident con) is) = do+bindingPattern (BindConstr con is) = do (patterns, scopes) <- fmap unzip $ mapM bindingPattern is- return (ConP (mkName con) patterns, concat scopes)-bindingPattern (BindRecord (Ident con) fields wild) = do+ return (ConP (mkConName con) patterns, concat scopes)+bindingPattern (BindRecord con fields wild) = do let f (Ident field,b) = do (p,s) <- bindingPattern b return ((mkName field,p),s)@@ -129,8 +130,15 @@ (patterns1, scopes1) <- if wild then bindWildFields con $ map fst fields else return ([],[])- return (RecP (mkName con) (patterns++patterns1), concat scopes ++ scopes1)+ return (RecP (mkConName con) (patterns++patterns1), concat scopes ++ scopes1) +mkConName :: DataConstr -> Name+mkConName = mkName . conToStr++conToStr :: DataConstr -> String+conToStr (DCUnqualified (Ident x)) = x+conToStr (DCQualified (Module xs) (Ident x)) = intercalate "." $ xs ++ [x]+ -- Wildcards bind all of the unbound fields to variables whose name -- matches the field name. --@@ -138,7 +146,7 @@ -- C {..} is equivalent to C {f1=f1, f2=f2} -- C {f1 = a, ..} is equivalent to C {f1=a, f2=f2} -- C {f2 = a, ..} is equivalent to C {f1=f1, f2=a}-bindWildFields :: String -> [Ident] -> Q ([(Name, Pat)], [(Ident, Exp)])+bindWildFields :: DataConstr -> [Ident] -> Q ([(Name, Pat)], [(Ident, Exp)]) bindWildFields conName fields = do fieldNames <- recordToFieldNames conName let available n = nameBase n `notElem` map unIdent fields@@ -152,11 +160,11 @@ -- same module as the reify is used. This means quasi-quoted Hamlet -- literals will not be able to use wildcards to match record types -- defined in the same module.-recordToFieldNames :: String -> Q [Name]+recordToFieldNames :: DataConstr -> Q [Name] recordToFieldNames conStr = do -- use 'lookupValueName' instead of just using 'mkName' so we reify the -- data constructor and not the type constructor if their names match.- Just conName <- lookupValueName conStr+ Just conName <- lookupValueName $ conToStr conStr DataConI _ _ typeName _ <- reify conName TyConI (DataD _ _ _ cons _) <- reify typeName [fields] <- return [fields | RecC name fields <- cons, name == conName]
Text/Hamlet/Parse.hs view
@@ -13,6 +13,8 @@ , Binding (..) , NewlineStyle (..) , specialOrIdent+ , DataConstr (..)+ , Module (..) ) where @@ -351,8 +353,7 @@ gcon :: Bool -> Parser Binding gcon allowArgs = do- c <- try $ do c <- ident- guard (isConstructor c)+ c <- try $ do c <- dataConstr return c choice [ record c@@ -361,6 +362,27 @@ ] <?> "constructor" + dataConstr = do+ p <- dcPiece+ ps <- many dcPieces+ return $ toDataConstr p ps++ dcPiece = do+ x@(Ident y) <- ident+ guard $ isConstructor x+ return y++ dcPieces = do+ _ <- char '.'+ dcPiece++ toDataConstr x [] = DCUnqualified $ Ident x+ toDataConstr x (y:ys) =+ go (x:) y ys+ where+ go front next [] = DCQualified (Module $ front []) (Ident next)+ go front next (rest:rests) = go (front . (next:)) rest rests+ record c = braces $ do (fields, wild) <- option ([], False) $ go return (BindRecord c fields wild)@@ -655,9 +677,19 @@ , ("strict", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">") ] -data Binding = BindVar Ident | BindAs Ident Binding | BindConstr Ident [Binding]- | BindTuple [Binding] | BindList [Binding]- | BindRecord Ident [(Ident, Binding)] Bool+data Binding = BindVar Ident+ | BindAs Ident Binding+ | BindConstr DataConstr [Binding]+ | BindTuple [Binding]+ | BindList [Binding]+ | BindRecord DataConstr [(Ident, Binding)] Bool+ deriving (Eq, Show, Read, Data, Typeable)++data DataConstr = DCQualified Module Ident+ | DCUnqualified Ident+ deriving (Eq, Show, Read, Data, Typeable)++newtype Module = Module [String] deriving (Eq, Show, Read, Data, Typeable) spaceTabs :: Parser String
hamlet.cabal view
@@ -1,5 +1,5 @@ name: hamlet-version: 1.1.7+version: 1.1.7.1 license: MIT license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>
test/HamletTest.hs view
@@ -201,7 +201,17 @@ |] + it "maybe with qualified constructor" $ do + helper "5" [hamlet| + $maybe HamletTestTypes.ARecord x y <- Just $ ARecord 5 True + \#{x} + |] + it "record with qualified constructor" $ do + helper "5" [hamlet| + $maybe HamletTestTypes.ARecord {..} <- Just $ ARecord 5 True + \#{field1} + |]