git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: don't strip out empty paragraphs.

We now have the `--strip-empty-paragraphs` option for that, if you want it. Closes #2252. Updated docx reader tests. We use stripEmptyParagraphs to avoid changing too many tests. We should add new tests for empty paragraphs.

commitd6c58eb836f033a48955796de4d9ffb3b30e297b authorJohn MacFarlane <jgm@berkeley.edu> dateSat 2 Dec 2017, 15:31 (9 years ago) parentf4b86a1
5 files changed+39−19
src/Text/Pandoc/Readers/Docx.hs+1−3 test/Tests/Readers/Docx.hs+20−7 test/docx/0_level_headers.native+8−8 test/docx/comments.native+1−1 test/docx/drop_cap_nostrip.native+9
src/Text/Pandoc/Readers/Docx.hs+1−3
@@ -534,9 +534,7 @@ bodyPartToBlocks (Paragraph pPr parparts)
534534 then do modify $ \s -> s { docxDropCap = ils' }
535535 return mempty
536536 else do modify $ \s -> s { docxDropCap = mempty }
537− return $ case isNull ils' of
538− True -> mempty
539− _ -> parStyleToTransform pPr $ para ils'
537+ return $ parStyleToTransform pPr $ para ils'
540538bodyPartToBlocks (ListItem pPr numId lvl (Just levelInfo) parparts) = do
541539 let
542540 kvs = case levelInfo of
test/Tests/Readers/Docx.hs+20−7
@@ -10,6 +10,7 @@ import Test.Tasty
1010import Test.Tasty.HUnit
1111import Tests.Helpers
1212import Text.Pandoc
13+import Text.Pandoc.Shared (stripEmptyParagraphs)
1314import qualified Text.Pandoc.Class as P
1415import Text.Pandoc.MediaBag (MediaBag, lookupMedia, mediaDirectory)
1516import Text.Pandoc.UTF8 as UTF8
@@ -37,20 +38,23 @@ instance ToString NoNormPandoc where
3738instance ToPandoc NoNormPandoc where
3839 toPandoc = unNoNorm
3940
40−compareOutput :: ReaderOptions
41− -> FilePath
42− -> FilePath
43− -> IO (NoNormPandoc, NoNormPandoc)
44−compareOutput opts docxFile nativeFile = do
41+compareOutput :: Bool
42+ -> ReaderOptions
43+ -> FilePath
44+ -> FilePath
45+ -> IO (NoNormPandoc, NoNormPandoc)
46+compareOutput strip opts docxFile nativeFile = do
4547 df <- B.readFile docxFile
4648 nf <- UTF8.toText <$> BS.readFile nativeFile
4749 p <- runIOorExplode $ readDocx opts df
4850 df' <- runIOorExplode $ readNative def nf
49− return $ (noNorm p, noNorm df')
51+ return $ (noNorm (if strip
52+ then stripEmptyParagraphs p
53+ else p), noNorm df')
5054
5155testCompareWithOptsIO :: ReaderOptions -> String -> FilePath -> FilePath -> IO TestTree
5256testCompareWithOptsIO opts name docxFile nativeFile = do
53− (dp, np) <- compareOutput opts docxFile nativeFile
57+ (dp, np) <- compareOutput True opts docxFile nativeFile
5458 return $ test id name (dp, np)
5559
5660testCompareWithOpts :: ReaderOptions -> String -> FilePath -> FilePath -> TestTree
@@ -71,6 +75,11 @@ testForWarningsWithOpts :: ReaderOptions -> String -> FilePath -> [String] -> Te
7175testForWarningsWithOpts opts name docxFile expected =
7276 unsafePerformIO $ testForWarningsWithOptsIO opts name docxFile expected
7377
78+testCompareNoStrip :: String -> FilePath -> FilePath -> TestTree
79+testCompareNoStrip name docxFile nativeFile = unsafePerformIO $ do
80+ (dp, np) <- compareOutput False defopts docxFile nativeFile
81+ return $ test id name (dp, np)
82+
7483-- testForWarnings :: String -> FilePath -> [String] -> TestTree
7584-- testForWarnings = testForWarningsWithOpts defopts
7685
@@ -257,6 +266,10 @@ tests = [ testGroup "inlines"
257266 "dropcap paragraphs"
258267 "docx/drop_cap.docx"
259268 "docx/drop_cap.native"
269+ , testCompareNoStrip
270+ "empty paragraphs without stripping"
271+ "docx/drop_cap.docx"
272+ "docx/drop_cap_nostrip.native"
260273 ]
261274 , testGroup "track changes"
262275 [ testCompare
test/docx/0_level_headers.native+8−8
@@ -1,15 +1,15 @@
11[Table [] [AlignDefault] [0.0]
22 [[]]
3− [[[]]
3+ [[[Plain []]]
44 ,[[Plain [Str "User\8217s",Space,Str "Guide"]]]
5− ,[[]]
6− ,[[]]
7− ,[[]]
5+ ,[[Plain []]]
6+ ,[[Plain []]]
7+ ,[[Plain []]]
88 ,[[Plain [Str "11",Space,Str "August",Space,Str "2017"]]]
9− ,[[]]
10− ,[[]]
11− ,[[]]
12− ,[[]]]
9+ ,[[Plain []]]
10+ ,[[Plain []]]
11+ ,[[Plain []]]
12+ ,[[Plain []]]]
1313,Para [Str "CONTENTS"]
1414,Para [Strong [Str "Section",Space,Str "Page"]]
1515,Para [Str "FIGURES",Space,Str "iv"]
test/docx/comments.native+1−1
@@ -1,4 +1,4 @@
11[Para [Str "I",Space,Str "want",Space,Span ("",["comment-start"],[("id","0"),("author","Jesse Rosenthal"),("date","2016-05-09T16:13:00Z")]) [Str "I",Space,Str "left",Space,Str "a",Space,Str "comment."],Str "some",Space,Str "text",Space,Str "to",Space,Str "have",Space,Str "a",Space,Str "comment",Space,Span ("",["comment-end"],[("id","0")]) [],Str "on",Space,Str "it."]
22,Para [Str "This",Space,Str "is",Space,Span ("",["comment-start"],[("id","1"),("author","Jesse Rosenthal"),("date","2016-05-09T16:13:00Z")]) [Str "A",Space,Str "comment",Space,Str "across",Space,Str "paragraphs."],Str "a",Space,Str "new",Space,Str "paragraph."]
33,Para [Str "And",Space,Str "so",Span ("",["comment-end"],[("id","1")]) [],Space,Str "is",Space,Str "this."]
4−,Para [Str "One",Space,Span ("",["comment-start"],[("id","2"),("author","Jesse Rosenthal"),("date","2016-05-09T16:14:00Z")]) [Str "This",Space,Str "one",Space,Str "has",Space,Str "multiple",Space,Str "paragraphs.",Space,Str "\182",Space,Str "See?"],Str "more",Span ("",["comment-end"],[("id","2")]) [],Str ".",Space,Str "And",Space,Str "this",Space,Str "is",Space,Str "one",Space,Str "with",Space,Str "a",Space,Span ("",["comment-start"],[("id","3"),("author","Jesse Rosenthal"),("date","2016-06-22T14:35:00Z")]) [Str "Do",Space,Str "something."],Span ("",["comment-start"],[("id","4"),("author","Jesse Rosenthal"),("date","2016-06-22T14:36:00Z")]) [Str "Do",Space,Str "something",Space,Str "else."],Str "comment",Space,Str "in",Space,Str "a",Space,Str "comment",Span ("",["comment-end"],[("id","3")]) [Span ("",["comment-end"],[("id","4")]) []],Str "."]]
4+,Para [Str "One",Space,Span ("",["comment-start"],[("id","2"),("author","Jesse Rosenthal"),("date","2016-05-09T16:14:00Z")]) [Str "This",Space,Str "one",Space,Str "has",Space,Str "multiple",Space,Str "paragraphs.",Space,Str "\182",Space,Str "\182",Space,Str "See?"],Str "more",Span ("",["comment-end"],[("id","2")]) [],Str ".",Space,Str "And",Space,Str "this",Space,Str "is",Space,Str "one",Space,Str "with",Space,Str "a",Space,Span ("",["comment-start"],[("id","3"),("author","Jesse Rosenthal"),("date","2016-06-22T14:35:00Z")]) [Str "Do",Space,Str "something."],Span ("",["comment-start"],[("id","4"),("author","Jesse Rosenthal"),("date","2016-06-22T14:36:00Z")]) [Str "Do",Space,Str "something",Space,Str "else."],Str "comment",Space,Str "in",Space,Str "a",Space,Str "comment",Span ("",["comment-end"],[("id","3")]) [Span ("",["comment-end"],[("id","4")]) []],Str "."]]
test/docx/drop_cap_nostrip.nativenew file+9
@@ -0,0 +1,9 @@
1+[Para [Str "Drop",Space,Str "cap."]
2+,Para []
3+,Para [Str "Next",Space,Str "paragraph."]
4+,Para []
5+,Para [Str "Drop",Space,Str "cap",Space,Str "in",Space,Str "margin."]
6+,Para []
7+,Para []
8+,Para []
9+,Para [Str "Drop",Space,Str "cap",Space,Str "(not",Space,Str "really)."]]