Some light documentation for Taggy.{Combinators,Renderer}.

This commit is contained in:
vi 2014-07-01 20:14:45 +08:00
parent 5c562ecc52
commit 15cf405c94
2 changed files with 25 additions and 2 deletions

View File

@ -41,19 +41,39 @@ innerText :: Element -> Text
innerText = mconcat . map getContent . eltChildren
where getContent = \case { NodeElement e -> innerText e; NodeContent x -> x }
-- | Filter an element and its children to those
-- satisfying a given predicate.
(//) :: Element -> (Element -> Bool) -> [Element]
(//) = flip filter . trees
-- | Given a sequence of predicates, filter an element
-- and its children, selecting only those subtrees who
-- match the provided predicate for each point.
--
-- >>> let element = (\(NodeElement e) -> e) . head . domify . taggyWith False $ "<html>foo<bar class=\"el\">baz</bar><qux class=\"el\"><quux></quux></qux></html>"
-- >>> element /& [const False]
-- []
-- >>> element /& [flip hasAttr "class", flip hasName "quux"]
-- [Element "quux" "" ""]
(/&) :: Element -> [(Element -> Bool)] -> [Element]
(/&) element [] = [element]
(/&) element (x:xs) = (/& xs) <=< filter x . catElements $ eltChildren element
-- | Filter from all subtrees (including the one
-- with the target as its root), those matching the
-- given sequence of predicates.
(/*) :: Element -> [(Element -> Bool)] -> [Element]
(/*) element selector = concat . filter (not.null) . map (/& selector) $ trees element
-- | Extracts all subtrees of its target, including the target.
trees :: Element -> [Element]
trees = ap (:) subtrees
-- | Extracts all subtrees of its target, excluding the target.
subtrees :: Element -> [Element]
subtrees = ap (:) subtrees <=< catElements . eltChildren

View File

@ -26,8 +26,8 @@ import Text.Blaze.Internal (ChoiceString(..), StaticString(..), MarkupM(..))
-- escaped, but a naked `Text s` is.
class AsMarkup a where
-- If the first parameter is true, we align the constructors for entity
-- conversion.
-- | If the first parameter is true, we align the constructors for entity
-- conversion.
toMarkup :: Bool -> a -> Markup
-- | A 'Node' is convertible to 'Markup'
@ -49,6 +49,9 @@ class Renderable a where
render = renderWith True
renderWith :: Bool -> a -> Lazy.Text
-- | Any value convertible to 'Markup' can be rendered as HTML, by way of
-- 'render' and 'renderWith'.
instance AsMarkup a => Renderable a where
renderWith = fmap renderMarkup . toMarkup