From 6db9326c348f54cd25f399ad1e472b20cb7771c0 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 18 Oct 2015 00:12:55 +0200 Subject: Added massaging to make sure blocks are not followed by extraneous whitespace --- bbcode/src/BBCode.hs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'bbcode/src/BBCode.hs') diff --git a/bbcode/src/BBCode.hs b/bbcode/src/BBCode.hs index 3071db6..118aed7 100644 --- a/bbcode/src/BBCode.hs +++ b/bbcode/src/BBCode.hs @@ -55,7 +55,20 @@ make' (Cooked c) = c make' (Raw _) = error "Cannot transform block containing raw data to bbcode" parse :: String -> Either String (Block String) -parse input = (remerge . blockify <$> (tokenize input >>= treeify)) >>= semantics +parse input = (remerge . massage . blockify <$> (tokenize input >>= treeify)) >>= semantics + +massage :: [Decorated String] -> [Decorated String] +massage [] = [] +massage (x@(Decorated _ tags) : x'@(Decorated wSpace []) : xs) + | any $ map isSpace wSpace + , any $ map isBlock tags = (x:Decorated (massage' wSpace) []:xs) + | otherwise = x : massage (x':xs) + where + massage' [] = [] + massage' l@(x:xs) + | isSpace x = massage' xs + | otherwise = l +massage (x:xs) = x : massage xs blockify :: ContentForest -> [Decorated String] blockify = map sortDeco . concat . map (blockify' []) -- cgit v1.2.3