normalize: Normalize spaces too.
In normal form, Space elements only occur to separate two non-Space elements. So, we never have [Space], or [, ..., Space].
This commit is contained in:
parent
8e81437fd1
commit
70405ef98a
2 changed files with 15 additions and 5 deletions
|
@ -10,6 +10,8 @@ tests :: [Test]
|
||||||
tests = [ testGroup "normalize"
|
tests = [ testGroup "normalize"
|
||||||
[ property "p_normalize_blocks_rt" p_normalize_blocks_rt
|
[ property "p_normalize_blocks_rt" p_normalize_blocks_rt
|
||||||
, property "p_normalize_inlines_rt" p_normalize_inlines_rt
|
, property "p_normalize_inlines_rt" p_normalize_inlines_rt
|
||||||
|
, property "p_normalize_no_trailing_spaces"
|
||||||
|
p_normalize_no_trailing_spaces
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -19,3 +21,6 @@ p_normalize_blocks_rt bs = normalize bs == normalize (normalize bs)
|
||||||
p_normalize_inlines_rt :: [Inline] -> Bool
|
p_normalize_inlines_rt :: [Inline] -> Bool
|
||||||
p_normalize_inlines_rt ils = normalize ils == normalize (normalize ils)
|
p_normalize_inlines_rt ils = normalize ils == normalize (normalize ils)
|
||||||
|
|
||||||
|
p_normalize_no_trailing_spaces :: [Inline] -> Bool
|
||||||
|
p_normalize_no_trailing_spaces ils = null ils' || last ils' /= Space
|
||||||
|
where ils' = normalize $ ils ++ [Space]
|
||||||
|
|
|
@ -243,10 +243,7 @@ orderedListMarkers (start, numstyle, numdelim) =
|
||||||
-- remove empty Str elements.
|
-- remove empty Str elements.
|
||||||
normalizeSpaces :: [Inline] -> [Inline]
|
normalizeSpaces :: [Inline] -> [Inline]
|
||||||
normalizeSpaces = cleanup . dropWhile isSpaceOrEmpty
|
normalizeSpaces = cleanup . dropWhile isSpaceOrEmpty
|
||||||
where isSpaceOrEmpty Space = True
|
where cleanup [] = []
|
||||||
isSpaceOrEmpty (Str "") = True
|
|
||||||
isSpaceOrEmpty _ = False
|
|
||||||
cleanup [] = []
|
|
||||||
cleanup (Space:rest) = let rest' = dropWhile isSpaceOrEmpty rest
|
cleanup (Space:rest) = let rest' = dropWhile isSpaceOrEmpty rest
|
||||||
in case rest' of
|
in case rest' of
|
||||||
[] -> []
|
[] -> []
|
||||||
|
@ -254,13 +251,18 @@ normalizeSpaces = cleanup . dropWhile isSpaceOrEmpty
|
||||||
cleanup ((Str ""):rest) = cleanup rest
|
cleanup ((Str ""):rest) = cleanup rest
|
||||||
cleanup (x:rest) = x : 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,
|
-- | Normalize @Pandoc@ document, consolidating doubled 'Space's,
|
||||||
-- combining adjacent 'Str's and 'Emph's, remove 'Null's and
|
-- combining adjacent 'Str's and 'Emph's, remove 'Null's and
|
||||||
-- empty elements, etc.
|
-- empty elements, etc.
|
||||||
normalize :: (Eq a, Data a) => a -> a
|
normalize :: (Eq a, Data a) => a -> a
|
||||||
normalize = topDown removeEmptyBlocks .
|
normalize = topDown removeEmptyBlocks .
|
||||||
topDown consolidateInlines .
|
topDown consolidateInlines .
|
||||||
bottomUp removeEmptyInlines
|
bottomUp (removeEmptyInlines . removeTrailingInlineSpaces)
|
||||||
|
|
||||||
removeEmptyBlocks :: [Block] -> [Block]
|
removeEmptyBlocks :: [Block] -> [Block]
|
||||||
removeEmptyBlocks (Null : xs) = removeEmptyBlocks xs
|
removeEmptyBlocks (Null : xs) = removeEmptyBlocks xs
|
||||||
|
@ -284,6 +286,9 @@ removeEmptyInlines (Str "" : zs) = removeEmptyInlines zs
|
||||||
removeEmptyInlines (x : xs) = x : removeEmptyInlines xs
|
removeEmptyInlines (x : xs) = x : removeEmptyInlines xs
|
||||||
removeEmptyInlines [] = []
|
removeEmptyInlines [] = []
|
||||||
|
|
||||||
|
removeTrailingInlineSpaces :: [Inline] -> [Inline]
|
||||||
|
removeTrailingInlineSpaces = reverse . dropWhile isSpaceOrEmpty . reverse
|
||||||
|
|
||||||
consolidateInlines :: [Inline] -> [Inline]
|
consolidateInlines :: [Inline] -> [Inline]
|
||||||
consolidateInlines (Str x : ys) =
|
consolidateInlines (Str x : ys) =
|
||||||
case concat (x : map fromStr strs) of
|
case concat (x : map fromStr strs) of
|
||||||
|
|
Loading…
Reference in a new issue