Skip to content
This repository
Browse code

Expose more attribute splice functionality

  • Loading branch information...
commit bfd3bbd20810a2c0829bd8e8f69719f7211ea7a0 1 parent cc5fc84
Doug Beardsley mightybyte authored

Showing 2 changed files with 45 additions and 28 deletions. Show diff stats Hide diff stats

  1. +1 0  src/Heist/Compiled.hs
  2. +44 28 src/Heist/Compiled/Internal.hs
1  src/Heist/Compiled.hs
@@ -72,6 +72,7 @@ module Heist.Compiled
72 72 , runNodeList
73 73 , runNode
74 74 , runAttributes
  75 + , runAttributesRaw
75 76 , callTemplate
76 77
77 78 ) where
72 src/Heist/Compiled/Internal.hs
@@ -387,34 +387,6 @@ hasAttributeSubstitutions txt = any isIdent ast
387 387
388 388
389 389 ------------------------------------------------------------------------------
390   --- |
391   -parseAtt :: Monad n => (Text, Text) -> HeistT n IO (DList (Chunk n))
392   -parseAtt (k,v) = do
393   - mas <- getsHS (H.lookup k . _attrSpliceMap)
394   - maybe doInline (return . doAttrSplice) mas
395   -
396   - where
397   - cvt (Literal x) = return $ yieldPureText x
398   - cvt (Ident x) =
399   - localParamNode (const $ X.Element x [] []) $ getAttributeSplice x
400   -
401   - -- Handles inline parsing of $() splice syntax in attributes
402   - doInline = do
403   - let ast = case AP.feed (AP.parse attParser v) "" of
404   - (AP.Done _ res) -> res
405   - (AP.Fail _ _ _) -> []
406   - (AP.Partial _ ) -> []
407   - chunks <- mapM cvt ast
408   - let value = DL.concat chunks
409   - return $ attrToChunk k value
410   -
411   - -- Handles attribute splices
412   - doAttrSplice splice = DL.singleton $ RuntimeHtml $ do
413   - res <- splice v
414   - return $ mconcat $ map attrToBuilder res
415   -
416   -
417   -------------------------------------------------------------------------------
418 390 -- | Given a 'X.Node' in the DOM tree, produces a \"runtime splice\" that will
419 391 -- generate html at runtime.
420 392 compileNode :: Monad n => X.Node -> Splice n
@@ -446,6 +418,34 @@ compileNode _ = error "impossible"
446 418
447 419
448 420 ------------------------------------------------------------------------------
  421 +-- |
  422 +parseAtt :: Monad n => (Text, Text) -> HeistT n IO (DList (Chunk n))
  423 +parseAtt (k,v) = do
  424 + mas <- getsHS (H.lookup k . _attrSpliceMap)
  425 + maybe doInline (return . doAttrSplice) mas
  426 +
  427 + where
  428 + cvt (Literal x) = return $ yieldPureText x
  429 + cvt (Ident x) =
  430 + localParamNode (const $ X.Element x [] []) $ getAttributeSplice x
  431 +
  432 + -- Handles inline parsing of $() splice syntax in attributes
  433 + doInline = do
  434 + let ast = case AP.feed (AP.parse attParser v) "" of
  435 + (AP.Done _ res) -> res
  436 + (AP.Fail _ _ _) -> []
  437 + (AP.Partial _ ) -> []
  438 + chunks <- mapM cvt ast
  439 + let value = DL.concat chunks
  440 + return $ attrToChunk k value
  441 +
  442 + -- Handles attribute splices
  443 + doAttrSplice splice = DL.singleton $ RuntimeHtml $ do
  444 + res <- splice v
  445 + return $ mconcat $ map attrToBuilder res
  446 +
  447 +
  448 +------------------------------------------------------------------------------
449 449 -- | Performs splice processing on a list of attributes. This is useful in
450 450 -- situations where you need to stop recursion, but still run splice
451 451 -- processing on the node's attributes.
@@ -453,6 +453,22 @@ runAttributes :: Monad n => [(Text, Text)] -> HeistT n IO [DList (Chunk n)]
453 453 runAttributes = mapM parseAtt
454 454
455 455
  456 +------------------------------------------------------------------------------
  457 +-- | Performs splice processing on a list of attributes. This is useful in
  458 +-- situations where you need to stop recursion, but still run splice
  459 +-- processing on the node's attributes.
  460 +runAttributesRaw :: (Monad m, Monad n)
  461 + => [(Text, Text)]
  462 + -> HeistT n m (RuntimeSplice n [(Text, Text)])
  463 +runAttributesRaw attrs = do
  464 + arrs <- mapM runSingle attrs
  465 + return $ liftM concat $ sequence arrs
  466 + where
  467 + runSingle p@(k,v) = do
  468 + mas <- getsHS (H.lookup k . _attrSpliceMap)
  469 + return $ maybe (return [p]) ($v) mas
  470 +
  471 +
456 472 attrToChunk :: Text -> DList (Chunk n) -> DList (Chunk n)
457 473 attrToChunk !k !v = do
458 474 DL.concat

0 comments on commit bfd3bbd

Please sign in to comment.
Something went wrong with that request. Please try again.