git.iko.soy/typeable/pandoc
on master help sign in
normalize: Normalize spaces too.

In normal form, Space elements only occur to separate two non-Space elements. So, we never have [Space], or [, ..., Space].

commit70405ef98ab48e76e9cc8cdd827cb7580dacf4e6 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 4 Feb 2011, 13:22 (16 years ago) parent8e81437
2 files changed+15−5
src/Tests/Shared.hs+5 src/Text/Pandoc/Shared.hs+10−5
src/Tests/Shared.hs+5
@@ -10,6 +10,8 @@ tests :: [Test]
1010tests = [ testGroup "normalize"
1111 [ property "p_normalize_blocks_rt" p_normalize_blocks_rt
1212 , property "p_normalize_inlines_rt" p_normalize_inlines_rt
13+ , property "p_normalize_no_trailing_spaces"
14+ p_normalize_no_trailing_spaces
1315 ]
1416 ]
1517
@@ -19,3 +21,6 @@ p_normalize_blocks_rt bs = normalize bs == normalize (normalize bs)
1921p_normalize_inlines_rt :: [Inline] -> Bool
2022p_normalize_inlines_rt ils = normalize ils == normalize (normalize ils)
2123
24+p_normalize_no_trailing_spaces :: [Inline] -> Bool
25+p_normalize_no_trailing_spaces ils = null ils' || last ils' /= Space
26+ where ils' = normalize $ ils ++ [Space]
src/Text/Pandoc/Shared.hs+10−5
@@ -243,10 +243,7 @@ orderedListMarkers (start, numstyle, numdelim) =
243243-- remove empty Str elements.
244244normalizeSpaces :: [Inline] -> [Inline]
245245normalizeSpaces = cleanup . dropWhile isSpaceOrEmpty
246− where isSpaceOrEmpty Space = True
247− isSpaceOrEmpty (Str "") = True
248− isSpaceOrEmpty _ = False
249− cleanup [] = []
246+ where cleanup [] = []
250247 cleanup (Space:rest) = let rest' = dropWhile isSpaceOrEmpty rest
251248 in case rest' of
252249 [] -> []
@@ -254,13 +251,18 @@ normalizeSpaces = cleanup . dropWhile isSpaceOrEmpty
254251 cleanup ((Str ""):rest) = cleanup rest
255252 cleanup (x:rest) = x : cleanup rest
256253
254+isSpaceOrEmpty :: Inline -> Bool
255+isSpaceOrEmpty Space = True
256+isSpaceOrEmpty (Str "") = True
257+isSpaceOrEmpty _ = False
258+
257259-- | Normalize @Pandoc@ document, consolidating doubled 'Space's,
258260-- combining adjacent 'Str's and 'Emph's, remove 'Null's and
259261-- empty elements, etc.
260262normalize :: (Eq a, Data a) => a -> a
261263normalize = topDown removeEmptyBlocks .
262264 topDown consolidateInlines .
263− bottomUp removeEmptyInlines
265+ bottomUp (removeEmptyInlines . removeTrailingInlineSpaces)
264266
265267removeEmptyBlocks :: [Block] -> [Block]
266268removeEmptyBlocks (Null : xs) = removeEmptyBlocks xs
@@ -284,6 +286,9 @@ removeEmptyInlines (Str "" : zs) = removeEmptyInlines zs
284286removeEmptyInlines (x : xs) = x : removeEmptyInlines xs
285287removeEmptyInlines [] = []
286288
289+removeTrailingInlineSpaces :: [Inline] -> [Inline]
290+removeTrailingInlineSpaces = reverse . dropWhile isSpaceOrEmpty . reverse
291+
287292consolidateInlines :: [Inline] -> [Inline]
288293consolidateInlines (Str x : ys) =
289294 case concat (x : map fromStr strs) of