aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2011-02-04 13:22:31 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2011-02-04 13:43:38 -0800
commit70405ef98ab48e76e9cc8cdd827cb7580dacf4e6 (patch)
tree98acdaa5c5e8e9d2d8b1f735b8daa62ffef16f53 /src/Text
parent8e81437fd1ad7e2fab200af722c86b2b265af818 (diff)
downloadpandoc-70405ef98ab48e76e9cc8cdd827cb7580dacf4e6.tar.gz
normalize: Normalize spaces too.
In normal form, Space elements only occur to separate two non-Space elements. So, we never have [Space], or [, ..., Space].
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Shared.hs15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index b1d5de63f..37873f822 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -243,10 +243,7 @@ orderedListMarkers (start, numstyle, numdelim) =
-- remove empty Str elements.
normalizeSpaces :: [Inline] -> [Inline]
normalizeSpaces = cleanup . dropWhile isSpaceOrEmpty
- where isSpaceOrEmpty Space = True
- isSpaceOrEmpty (Str "") = True
- isSpaceOrEmpty _ = False
- cleanup [] = []
+ where cleanup [] = []
cleanup (Space:rest) = let rest' = dropWhile isSpaceOrEmpty rest
in case rest' of
[] -> []
@@ -254,13 +251,18 @@ normalizeSpaces = cleanup . dropWhile isSpaceOrEmpty
cleanup ((Str ""):rest) = cleanup rest
cleanup (x:rest) = x : cleanup rest
+isSpaceOrEmpty :: Inline -> Bool
+isSpaceOrEmpty Space = True
+isSpaceOrEmpty (Str "") = True
+isSpaceOrEmpty _ = False
+
-- | Normalize @Pandoc@ document, consolidating doubled 'Space's,
-- combining adjacent 'Str's and 'Emph's, remove 'Null's and
-- empty elements, etc.
normalize :: (Eq a, Data a) => a -> a
normalize = topDown removeEmptyBlocks .
topDown consolidateInlines .
- bottomUp removeEmptyInlines
+ bottomUp (removeEmptyInlines . removeTrailingInlineSpaces)
removeEmptyBlocks :: [Block] -> [Block]
removeEmptyBlocks (Null : xs) = removeEmptyBlocks xs
@@ -284,6 +286,9 @@ removeEmptyInlines (Str "" : zs) = removeEmptyInlines zs
removeEmptyInlines (x : xs) = x : removeEmptyInlines xs
removeEmptyInlines [] = []
+removeTrailingInlineSpaces :: [Inline] -> [Inline]
+removeTrailingInlineSpaces = reverse . dropWhile isSpaceOrEmpty . reverse
+
consolidateInlines :: [Inline] -> [Inline]
consolidateInlines (Str x : ys) =
case concat (x : map fromStr strs) of