From c2d711b32917d036e30f7c015f5d5d41b7aef030 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 18 Oct 2015 00:33:59 +0200 Subject: added massaging to avoid uneccesary whitespace injection --- bbcode/src/BBCode.hs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'bbcode/src/BBCode.hs') diff --git a/bbcode/src/BBCode.hs b/bbcode/src/BBCode.hs index 3071db6..087842f 100644 --- a/bbcode/src/BBCode.hs +++ b/bbcode/src/BBCode.hs @@ -55,7 +55,19 @@ 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 = massage <$> ((remerge . blockify <$> (tokenize input >>= treeify)) >>= semantics) + +massage :: Block String -> Block String +massage (Over [x]) = massage x +massage (Over xs) = Over $ map massage xs +massage (Center x) = Center $ massage x +massage (Paragraph x) = Paragraph $ massage' x + +massage' :: Inline String -> Inline String +massage' (Beside [x]) = massage' x +massage' (Beside xs) = Beside $ map massage' xs +massage' (Underline x) = Underline $ massage' x +massage' z = z blockify :: ContentForest -> [Decorated String] blockify = map sortDeco . concat . map (blockify' []) @@ -82,7 +94,7 @@ remerge xs = concat $ map toTree $ groupLasts xs toTree (x@(Decorated _ ds):xs) = [Tagged content tag] where tag = last ds - content = toTree $ map stripLast (x:xs) + content = concat $ map toTree $ groupLasts $ map stripLast (x:xs) stripLast (Decorated c ds) = Decorated c (init ds) unknownTag :: forall a. String -> Either String a -- cgit v1.2.3