blaze-html-truncate 0.1.0.3 → 0.1.0.4
raw patch · 2 files changed
+21/−3 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- Text/Blaze/Truncate.hs +20/−2
- blaze-html-truncate.cabal +1/−1
Text/Blaze/Truncate.hs view
@@ -56,7 +56,7 @@ (ts',ts'') -> ((TS.TagText str') : ts', (TS.TagText str'') : ts'') dropWhileEndPreEscapedHtml :: SL.StringLike str => (Char -> Bool) -> str -> str-dropWhileEndPreEscapedHtml p txt = (TS.renderTags . reverse . (go 0) . reverse . TS.parseTags) txt+dropWhileEndPreEscapedHtml p txt = (TS.renderTags . reverse . filterEmptyTags . (go 0) . reverse . TS.parseTags) txt where go :: (SL.StringLike str) => Int -> [TS.Tag str] -> [TS.Tag str] go _ [] = []@@ -70,13 +70,16 @@ else case dropWhileEndSL p str of str' -> if not (SL.strNull str') then (TS.TagText str') : ts else go openTgs ts+ filterEmptyTags ((TS.TagClose _) : (TS.TagOpen _ _) : ts) = filterEmptyTags ts+ filterEmptyTags (t : ts) = t : (filterEmptyTags ts)+ filterEmptyTags [] = [] -- | Truncate the given HTML to a certain length, preserving tags. Returns the truncated Html or `Nothing` if no truncation occured. -- Words are preserved, so if the truncated text ends within some word, that whole word is cut. truncateHtml :: Int -- ^ The amount of characters (not counting tags) which the truncated text should have at most -> Html -- ^ The HTML to truncate -> Maybe Html -- ^ `Just` the truncated HTML or `Nothing` if no truncation occured-truncateHtml n html = case go n html of Tagged n' html' -> if n' <= 0 then Just html' else Nothing+truncateHtml n html = case go n html of Tagged n' html' -> if n' <= 0 then filterEmptyTags html' else Nothing where go :: Int -> HtmlM b -> Tagged (HtmlM b) go i (Parent t open close content) = fmap (Parent t open close) (go i content)@@ -88,6 +91,21 @@ Tagged j h1' -> fmap (Append h1') (go j h2) go i Empty = Tagged i Empty go i (Content content) = fmap Content (truncateChoiceString i content)+ + -- filter _trailing_ empty tags+ filterEmptyTags :: HtmlM a -> Maybe (HtmlM b)+ filterEmptyTags (Parent t open close content) = fmap (Parent t open close) (filterEmptyTags content)+ filterEmptyTags (Leaf t begin end) = Nothing+ filterEmptyTags (AddAttribute t key value h) = fmap (AddAttribute t key value) (filterEmptyTags h)+ filterEmptyTags (AddCustomAttribute t key value h) = fmap (AddCustomAttribute t key value) (filterEmptyTags h)+ filterEmptyTags (Append h1 h2) = case filterEmptyTags h2 of+ Nothing -> filterEmptyTags h1+ Just h2' -> Just (Append h1 h2')+ filterEmptyTags Empty = Nothing+ filterEmptyTags (Content content) = if (length' content) == 0 then Nothing else Just (Content content)+ + + splitAtPreEscaped' :: Int -> ChoiceString -> (ChoiceString, ChoiceString) splitAtPreEscaped' i str | i <= 0 = (EmptyChoiceString, str)
blaze-html-truncate.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: blaze-html-truncate-version: 0.1.0.3+version: 0.1.0.4 synopsis: A truncator for blaze-html description: This package provides a simple function to truncate HTML, preserving tags and preventing cut-off words. homepage: http://github.com/mruegenberg/blaze-html-truncate