2014-06-17 07:44:40 +02:00
|
|
|
module Tests.Readers.Docx (tests) where
|
2014-06-15 20:55:17 +02:00
|
|
|
|
|
|
|
import Text.Pandoc.Options
|
|
|
|
import Text.Pandoc.Readers.Native
|
|
|
|
import Text.Pandoc.Definition
|
|
|
|
import Tests.Helpers
|
|
|
|
import Test.Framework
|
2014-07-31 04:31:38 +02:00
|
|
|
import qualified Data.ByteString as BS
|
2014-06-15 20:55:17 +02:00
|
|
|
import qualified Data.ByteString.Lazy as B
|
2014-07-31 04:31:38 +02:00
|
|
|
import qualified Data.ByteString.Char8 as B8
|
|
|
|
import qualified Data.ByteString.Base64 as B64
|
2014-06-17 07:44:40 +02:00
|
|
|
import Text.Pandoc.Readers.Docx
|
2014-06-21 00:26:15 +02:00
|
|
|
import Text.Pandoc.Writers.Native (writeNative)
|
|
|
|
import qualified Data.Map as M
|
2014-06-15 20:55:17 +02:00
|
|
|
|
2014-06-21 00:26:15 +02:00
|
|
|
-- We define a wrapper around pandoc that doesn't normalize in the
|
|
|
|
-- tests. Since we do our own normalization, we want to make sure
|
|
|
|
-- we're doing it right.
|
|
|
|
|
|
|
|
data NoNormPandoc = NoNormPandoc {unNoNorm :: Pandoc}
|
|
|
|
deriving Show
|
|
|
|
|
|
|
|
noNorm :: Pandoc -> NoNormPandoc
|
|
|
|
noNorm = NoNormPandoc
|
|
|
|
|
|
|
|
instance ToString NoNormPandoc where
|
|
|
|
toString d = writeNative def{ writerStandalone = s } $ toPandoc d
|
|
|
|
where s = case d of
|
|
|
|
NoNormPandoc (Pandoc (Meta m) _)
|
|
|
|
| M.null m -> False
|
|
|
|
| otherwise -> True
|
|
|
|
|
|
|
|
instance ToPandoc NoNormPandoc where
|
|
|
|
toPandoc = unNoNorm
|
|
|
|
|
|
|
|
compareOutput :: ReaderOptions
|
|
|
|
-> FilePath
|
|
|
|
-> FilePath
|
|
|
|
-> IO (NoNormPandoc, NoNormPandoc)
|
2014-06-19 18:05:16 +02:00
|
|
|
compareOutput opts docxFile nativeFile = do
|
2014-06-15 20:55:17 +02:00
|
|
|
df <- B.readFile docxFile
|
|
|
|
nf <- Prelude.readFile nativeFile
|
2014-07-30 18:46:53 +02:00
|
|
|
let (p, _) = readDocx opts df
|
|
|
|
return $ (noNorm p, noNorm (readNative nf))
|
2014-06-15 20:55:17 +02:00
|
|
|
|
2014-06-19 18:05:16 +02:00
|
|
|
testCompareWithOptsIO :: ReaderOptions -> String -> FilePath -> FilePath -> IO Test
|
|
|
|
testCompareWithOptsIO opts name docxFile nativeFile = do
|
|
|
|
(dp, np) <- compareOutput opts docxFile nativeFile
|
2014-06-15 20:55:17 +02:00
|
|
|
return $ test id name (dp, np)
|
|
|
|
|
2014-06-19 18:05:16 +02:00
|
|
|
testCompareWithOpts :: ReaderOptions -> String -> FilePath -> FilePath -> Test
|
|
|
|
testCompareWithOpts opts name docxFile nativeFile =
|
|
|
|
buildTest $ testCompareWithOptsIO opts name docxFile nativeFile
|
|
|
|
|
2014-06-15 20:55:17 +02:00
|
|
|
testCompare :: String -> FilePath -> FilePath -> Test
|
2014-06-19 18:05:16 +02:00
|
|
|
testCompare = testCompareWithOpts def
|
2014-06-15 20:55:17 +02:00
|
|
|
|
2014-07-31 04:31:38 +02:00
|
|
|
testCompareMediaIO :: String -> FilePath -> FilePath -> FilePath -> IO Test
|
|
|
|
testCompareMediaIO name docxFile mediaPath mediaFile = do
|
|
|
|
df <- B.readFile docxFile
|
|
|
|
mf <- B.readFile mediaFile
|
|
|
|
let (_, mb) = readDocx def df
|
|
|
|
dBytes = case M.lookup mediaPath mb of
|
|
|
|
Just bs -> bs
|
|
|
|
Nothing -> error "Media file not found"
|
|
|
|
d64 = B8.unpack $ B64.encode $ BS.concat $ B.toChunks dBytes
|
|
|
|
m64 = B8.unpack $ B64.encode $ BS.concat $ B.toChunks mf
|
|
|
|
return $ test id name (d64, m64)
|
|
|
|
|
|
|
|
testCompareMedia :: String -> FilePath -> FilePath -> FilePath -> Test
|
|
|
|
testCompareMedia name docxFile mediaPath mediaFile =
|
|
|
|
buildTest $ testCompareMediaIO name docxFile mediaPath mediaFile
|
|
|
|
|
2014-06-15 20:55:17 +02:00
|
|
|
|
|
|
|
tests :: [Test]
|
|
|
|
tests = [ testGroup "inlines"
|
|
|
|
[ testCompare
|
|
|
|
"font formatting"
|
|
|
|
"docx.inline_formatting.docx"
|
|
|
|
"docx.inline_formatting.native"
|
|
|
|
, testCompare
|
|
|
|
"hyperlinks"
|
|
|
|
"docx.links.docx"
|
|
|
|
"docx.links.native"
|
|
|
|
, testCompare
|
|
|
|
"inline image with reference output"
|
|
|
|
"docx.image.docx"
|
|
|
|
"docx.image_no_embed.native"
|
|
|
|
, testCompare
|
|
|
|
"handling unicode input"
|
|
|
|
"docx.unicode.docx"
|
2014-06-20 01:33:22 +02:00
|
|
|
"docx.unicode.native"
|
|
|
|
, testCompare
|
|
|
|
"literal tabs"
|
|
|
|
"docx.tabs.docx"
|
|
|
|
"docx.tabs.native"
|
2014-06-21 23:58:32 +02:00
|
|
|
, testCompare
|
|
|
|
"normalizing inlines"
|
|
|
|
"docx.normalize.docx"
|
|
|
|
"docx.normalize.native"
|
2014-06-22 07:58:41 +02:00
|
|
|
, testCompare
|
|
|
|
"normalizing inlines deep inside blocks"
|
|
|
|
"docx.deep_normalize.docx"
|
|
|
|
"docx.deep_normalize.native"
|
2014-06-23 21:40:34 +02:00
|
|
|
, testCompare
|
|
|
|
"move trailing spaces outside of formatting"
|
|
|
|
"docx.trailing_spaces_in_formatting.docx"
|
|
|
|
"docx.trailing_spaces_in_formatting.native"
|
2014-06-24 16:33:49 +02:00
|
|
|
, testCompare
|
|
|
|
"inline code (with VerbatimChar style)"
|
|
|
|
"docx.inline_code.docx"
|
|
|
|
"docx.inline_code.native"
|
2014-06-20 01:33:22 +02:00
|
|
|
]
|
2014-06-15 20:55:17 +02:00
|
|
|
, testGroup "blocks"
|
|
|
|
[ testCompare
|
|
|
|
"headers"
|
|
|
|
"docx.headers.docx"
|
|
|
|
"docx.headers.native"
|
2014-07-15 11:25:32 +02:00
|
|
|
, testCompare
|
|
|
|
"headers already having auto identifiers"
|
|
|
|
"docx.already_auto_ident.docx"
|
|
|
|
"docx.already_auto_ident.native"
|
2014-06-15 20:55:17 +02:00
|
|
|
, testCompare
|
|
|
|
"lists"
|
|
|
|
"docx.lists.docx"
|
|
|
|
"docx.lists.native"
|
2014-06-24 17:48:23 +02:00
|
|
|
, testCompare
|
|
|
|
"definition lists"
|
|
|
|
"docx.definition_list.docx"
|
|
|
|
"docx.definition_list.native"
|
2014-06-15 20:55:17 +02:00
|
|
|
, testCompare
|
|
|
|
"footnotes and endnotes"
|
|
|
|
"docx.notes.docx"
|
|
|
|
"docx.notes.native"
|
|
|
|
, testCompare
|
|
|
|
"blockquotes (parsing indent as blockquote)"
|
|
|
|
"docx.block_quotes.docx"
|
|
|
|
"docx.block_quotes_parse_indent.native"
|
2014-06-30 05:37:00 +02:00
|
|
|
, testCompare
|
|
|
|
"hanging indents"
|
|
|
|
"docx.hanging_indent.docx"
|
|
|
|
"docx.hanging_indent.native"
|
2014-06-15 20:55:17 +02:00
|
|
|
, testCompare
|
|
|
|
"tables"
|
|
|
|
"docx.tables.docx"
|
|
|
|
"docx.tables.native"
|
2014-06-24 16:33:49 +02:00
|
|
|
, testCompare
|
|
|
|
"code block"
|
|
|
|
"docx.codeblock.docx"
|
|
|
|
"docx.codeblock.native"
|
|
|
|
|
2014-06-15 20:55:17 +02:00
|
|
|
]
|
2014-06-25 17:09:28 +02:00
|
|
|
, testGroup "track changes"
|
|
|
|
[ testCompare
|
2014-06-25 22:13:59 +02:00
|
|
|
"insertion (default)"
|
2014-06-25 17:09:28 +02:00
|
|
|
"docx.track_changes_insertion.docx"
|
2014-06-25 22:13:59 +02:00
|
|
|
"docx.track_changes_insertion_accept.native"
|
|
|
|
, testCompareWithOpts def{readerTrackChanges=AcceptChanges}
|
|
|
|
"insert insertion (accept)"
|
|
|
|
"docx.track_changes_insertion.docx"
|
|
|
|
"docx.track_changes_insertion_accept.native"
|
|
|
|
, testCompareWithOpts def{readerTrackChanges=RejectChanges}
|
|
|
|
"remove insertion (reject)"
|
|
|
|
"docx.track_changes_insertion.docx"
|
|
|
|
"docx.track_changes_insertion_reject.native"
|
2014-06-25 17:09:28 +02:00
|
|
|
, testCompare
|
2014-06-25 22:13:59 +02:00
|
|
|
"deletion (default)"
|
|
|
|
"docx.track_changes_deletion.docx"
|
|
|
|
"docx.track_changes_deletion_accept.native"
|
|
|
|
, testCompareWithOpts def{readerTrackChanges=AcceptChanges}
|
|
|
|
"remove deletion (accept)"
|
|
|
|
"docx.track_changes_deletion.docx"
|
|
|
|
"docx.track_changes_deletion_accept.native"
|
|
|
|
, testCompareWithOpts def{readerTrackChanges=RejectChanges}
|
|
|
|
"insert deletion (reject)"
|
|
|
|
"docx.track_changes_deletion.docx"
|
|
|
|
"docx.track_changes_deletion_reject.native"
|
|
|
|
, testCompareWithOpts def{readerTrackChanges=AllChanges}
|
|
|
|
"keep insertion (all)"
|
|
|
|
"docx.track_changes_deletion.docx"
|
|
|
|
"docx.track_changes_deletion_all.native"
|
|
|
|
, testCompareWithOpts def{readerTrackChanges=AllChanges}
|
|
|
|
"keep deletion (all)"
|
2014-06-25 17:09:28 +02:00
|
|
|
"docx.track_changes_deletion.docx"
|
2014-06-25 22:13:59 +02:00
|
|
|
"docx.track_changes_deletion_all.native"
|
2014-06-25 17:09:28 +02:00
|
|
|
]
|
2014-07-27 21:11:18 +02:00
|
|
|
, testGroup "metadata"
|
|
|
|
[ testCompareWithOpts def{readerStandalone=True}
|
|
|
|
"metadata fields"
|
|
|
|
"docx.metadata.docx"
|
|
|
|
"docx.metadata.native"
|
|
|
|
, testCompareWithOpts def{readerStandalone=True}
|
|
|
|
"stop recording metadata with normal text"
|
|
|
|
"docx.metadata_after_normal.docx"
|
|
|
|
"docx.metadata_after_normal.native"
|
|
|
|
]
|
|
|
|
|
2014-06-15 20:55:17 +02:00
|
|
|
]
|
|
|
|
|