blaze-html-truncate 0.1.0.4 → 0.2.0.0
raw patch · 2 files changed
+11/−32 lines, 2 filesdep +blaze-markupdep −blaze-htmldep ~basePVP ok
version bump matches the API change (PVP)
Dependencies added: blaze-markup
Dependencies removed: blaze-html
Dependency ranges changed: base
API changes (from Hackage documentation)
- Text.Blaze.Truncate: truncateHtml :: Int -> Html -> Maybe Html
+ Text.Blaze.Truncate: truncateHtml :: Int -> Markup -> Maybe Markup
Files
- Text/Blaze/Truncate.hs +8/−29
- blaze-html-truncate.cabal +3/−3
Text/Blaze/Truncate.hs view
@@ -1,8 +1,8 @@ module Text.Blaze.Truncate(truncateHtml) where -- from Blaze 0.4 to 0.5: Html -> Markup; HtmlM -> MarkupM; AddCustomAttribute has an additional first argument-import Text.Blaze(Html)-import Text.Blaze.Internal(HtmlM(..),ChoiceString(..),StaticString(..))+import Text.Blaze(Markup)+import Text.Blaze.Internal(MarkupM(..),ChoiceString(..),StaticString(..)) import Data.Char import Data.Text(Text) import qualified Data.Text as T@@ -17,9 +17,6 @@ data Tagged a = Tagged Int a instance Functor Tagged where fmap f (Tagged n a) = Tagged n (f a)- --- type Html = Markup--- type HtmlM a = MarkupM a -- (inefficient `take` for StringLike) splitAtSL :: SL.StringLike a => Int -> a -> (a,a)@@ -56,7 +53,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 . filterEmptyTags . (go 0) . reverse . TS.parseTags) txt+dropWhileEndPreEscapedHtml p txt = (TS.renderTags . reverse . (go 0) . reverse . TS.parseTags) txt where go :: (SL.StringLike str) => Int -> [TS.Tag str] -> [TS.Tag str] go _ [] = []@@ -70,42 +67,24 @@ 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 filterEmptyTags html' else Nothing+ -> Markup -- ^ The HTML to truncate+ -> Maybe Markup -- ^ `Just` the truncated HTML or `Nothing` if no truncation occured+truncateHtml n html = case go n html of Tagged n' html' -> if n' /= n then Just html' else Nothing where- go :: Int -> HtmlM b -> Tagged (HtmlM b)+ go :: Int -> MarkupM b -> Tagged (MarkupM b) go i (Parent t open close content) = fmap (Parent t open close) (go i content) go i (Leaf t begin end) = Tagged i (Leaf t begin end) go i (AddAttribute t key value h) = fmap (AddAttribute t key value) (go i h)- go i (AddCustomAttribute t key value h) = fmap (AddCustomAttribute t key value) (go i h)+ go i (AddCustomAttribute key value h) = fmap (AddCustomAttribute key value) (go i h) go i (Append h1 h2) = case go i h1 of Tagged j h1' | j <= 0 -> Tagged j (Append h1' Empty) -- FIXME: we actually want to return just Tagged j h1', but can't due to a type error 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.4+version: 0.2.0.0 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@@ -22,8 +22,8 @@ library exposed-modules: Text.Blaze.Truncate -- other-modules: - build-depends: base >= 4.5.0.0 && < 4.7.0.0- , blaze-html >= 0.4.0.0 && < 0.5.0.0+ build-depends: base >= 4 && < 5+ , blaze-markup >= 0.5.0.0 , bytestring >= 0.9 , text >= 0.10 , tagsoup >= 0.10