Permalink
Browse files

Expose ability to get list of used variables (needed for yesodweb/yes…

  • Loading branch information...
1 parent 4265187 commit ce4d79b1c8a56f144117f61409b874c6e840c21d @snoyberg snoyberg committed Oct 30, 2012
@@ -32,6 +32,8 @@ module Text.Cassius
, PercentageSize (..)
, percentageSize
, PixelSize (..)
+ -- * Internal
+ , cassiusUsedIdentifiers
) where
import Text.Css
@@ -58,3 +60,8 @@ cassiusFile fp = do
cassiusFileDebug, cassiusFileReload :: FilePath -> Q Exp
cassiusFileDebug = cssFileDebug True [|Text.Lucius.parseTopLevels|] Text.Lucius.parseTopLevels
cassiusFileReload = cassiusFileDebug
+
+-- | Determine which identifiers are used by the given template, useful for
+-- creating systems like yesod devel.
+cassiusUsedIdentifiers :: String -> [(Deref, VarType)]
+cassiusUsedIdentifiers = cssUsedIdentifiers True Text.Lucius.parseTopLevels
@@ -20,6 +20,7 @@ import Language.Haskell.TH
import Control.Applicative ((<$>), (<*>))
import Control.Arrow ((***))
import Text.IndentToBrace (i2b)
+import Data.Functor.Identity (runIdentity)
type CssUrl url = (url -> [(T.Text, T.Text)] -> T.Text) -> Css
@@ -65,22 +66,19 @@ data CDData url = CDPlain Builder
| CDUrl url
| CDUrlParam (url, [(Text, Text)])
-cssFileDebug :: Bool -- ^ perform the indent-to-brace conversion
- -> Q Exp -> Parser [TopLevel] -> FilePath -> Q Exp
-cssFileDebug toi2b parseBlocks' parseBlocks fp = do
- s' <- fmap TL.unpack $ qRunIO $ readUtf8File fp
- let s = if toi2b then i2b s' else s'
-#ifdef GHC_7_4
- qAddDependentFile fp
-#endif
- let a = either (error . show) id $ parse parseBlocks s s
- let (scope, contents) = go a
- vs <- mapM (getVars scope) contents
- c <- mapM vtToExp $ concat vs
- cr <- [|cssRuntime toi2b|]
- parseBlocks'' <- parseBlocks'
- return $ cr `AppE` parseBlocks'' `AppE` (LitE $ StringL fp) `AppE` ListE c
+-- | Determine which identifiers are used by the given template, useful for
+-- creating systems like yesod devel.
+cssUsedIdentifiers :: Bool -- ^ perform the indent-to-brace conversion
+ -> Parser [TopLevel]
+ -> String
+ -> [(Deref, VarType)]
+cssUsedIdentifiers toi2b parseBlocks s' =
+ concat $ runIdentity $ mapM (getVars scope0) contents
where
+ s = if toi2b then i2b s' else s'
+ a = either (error . show) id $ parse parseBlocks s s
+ (scope0, contents) = go a
+
go :: [TopLevel] -> ([(String, String)], [Content])
go [] = ([], [])
go (TopAtDecl dec _FIXMEcs:rest) =
@@ -110,6 +108,19 @@ cssFileDebug toi2b parseBlocks' parseBlocks fp = do
(scope, rest') = go rest
go' (k, v) = k ++ v
+cssFileDebug :: Bool -- ^ perform the indent-to-brace conversion
+ -> Q Exp -> Parser [TopLevel] -> FilePath -> Q Exp
+cssFileDebug toi2b parseBlocks' parseBlocks fp = do
+ s <- fmap TL.unpack $ qRunIO $ readUtf8File fp
+#ifdef GHC_7_4
+ qAddDependentFile fp
+#endif
+ let vs = cssUsedIdentifiers toi2b parseBlocks s
+ c <- mapM vtToExp vs
+ cr <- [|cssRuntime toi2b|]
+ parseBlocks'' <- parseBlocks'
+ return $ cr `AppE` parseBlocks'' `AppE` (LitE $ StringL fp) `AppE` ListE c
+
combineSelectors :: Selector -> Selector -> Selector
combineSelectors a b = do
a' <- a
@@ -38,6 +38,7 @@ module Text.Lucius
, PixelSize (..)
-- * Internal
, parseTopLevels
+ , luciusUsedIdentifiers
) where
import Text.CssCommon
@@ -285,3 +286,8 @@ luciusRT' tl =
luciusRT :: TL.Text -> [(Text, Text)] -> Either String TL.Text
luciusRT tl scope = either Left (Right . renderCss . CssWhitespace) $ either Left ($ scope) (luciusRT' tl)
+
+-- | Determine which identifiers are used by the given template, useful for
+-- creating systems like yesod devel.
+luciusUsedIdentifiers :: String -> [(Deref, VarType)]
+luciusUsedIdentifiers = cssUsedIdentifiers False parseTopLevels
@@ -1,5 +1,5 @@
name: shakespeare-css
-version: 1.0.1.5
+version: 1.0.2
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
@@ -39,6 +39,8 @@ module Text.Julius
-- ** internal, used by 'Text.Coffee'
, javascriptSettings
+ -- ** internal
+ , juliusUsedIdentifiers
) where
import Language.Haskell.TH.Quote (QuasiQuoter (..))
@@ -119,3 +121,8 @@ juliusFileDebug = jsFileReload
{-# DEPRECATED juliusFileDebug "Please use juliusFileReload instead." #-}
jsFileDebug = jsFileReload
{-# DEPRECATED jsFileDebug "Please use jsFileReload instead." #-}
+
+-- | Determine which identifiers are used by the given template, useful for
+-- creating systems like yesod devel.
+juliusUsedIdentifiers :: String -> [(Deref, VarType)]
+juliusUsedIdentifiers = shakespeareUsedIdentifiers defaultShakespeareSettings
@@ -1,5 +1,5 @@
name: shakespeare-js
-version: 1.0.0.6
+version: 1.0.1
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
@@ -29,7 +29,7 @@ homepage: http://www.yesodweb.com/book/shakespearean-templates
library
build-depends: base >= 4 && < 5
- , shakespeare >= 1.0.1.4 && < 1.1
+ , shakespeare >= 1.0.2 && < 1.1
, template-haskell
, text >= 0.7 && < 0.12
@@ -16,7 +16,10 @@ module Text.Shakespeare
, shakespeareFileReload
-- * low-level
, shakespeareFromString
+ , shakespeareUsedIdentifiers
, RenderUrl
+ , VarType
+ , Deref
#ifdef TEST_EXPORT
, preFilter
@@ -287,11 +290,16 @@ data VarExp url = EPlain Builder
| EUrlParam (url, [(TS.Text, TS.Text)])
| EMixin (Shakespeare url)
+-- | Determine which identifiers are used by the given template, useful for
+-- creating systems like yesod devel.
+shakespeareUsedIdentifiers :: ShakespeareSettings -> String -> [(Deref, VarType)]
+shakespeareUsedIdentifiers settings = concatMap getVars . contentFromString settings
+
shakespeareFileReload :: ShakespeareSettings -> FilePath -> Q Exp
shakespeareFileReload rs fp = do
str <- readFileQ fp
s <- qRunIO $ preFilter rs str
- let b = concatMap getVars $ contentFromString rs s
+ let b = shakespeareUsedIdentifiers rs s
c <- mapM vtToExp b
rt <- [|shakespeareRuntime|]
wrap' <- [|\x -> $(return $ wrap rs) . x|]
@@ -37,7 +37,7 @@ import qualified Data.Text.Lazy.IO as TIO
import Control.Monad (when)
newtype Ident = Ident String
- deriving (Show, Eq, Read, Data, Typeable)
+ deriving (Show, Eq, Read, Data, Typeable, Ord)
type Scope = [(Ident, Exp)]
@@ -49,7 +49,7 @@ data Deref = DerefModulesIdent [String] Ident
| DerefBranch Deref Deref
| DerefList [Deref]
| DerefTuple [Deref]
- deriving (Show, Eq, Read, Data, Typeable)
+ deriving (Show, Eq, Read, Data, Typeable, Ord)
instance Lift Ident where
lift (Ident s) = [|Ident|] `appE` lift s
@@ -1,5 +1,5 @@
name: shakespeare
-version: 1.0.1.4
+version: 1.0.2
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>

0 comments on commit ce4d79b

Please sign in to comment.