git.iko.soy/typeable/pandoc
on master help sign in
DOCX reader: Allow empty dates in comments and tracked changes (#6726)
eda5540Diego Balseiro
src/Text/Pandoc/Readers/Docx.hs+10−5
@@ -365,7 +365,7 @@ parPartToInlines' (ChangedRuns (TrackedChange Insertion (ChangeInfo _ author dat
365 RejectChanges -> return mempty
365 RejectChanges -> return mempty
366 AllChanges -> do
366 AllChanges -> do
367 ils <- smushInlines <$> mapM runToInlines runs
367 ils <- smushInlines <$> mapM runToInlines runs
368− let attr = ("", ["insertion"], [("author", author), ("date", date)])
368+ let attr = ("", ["insertion"], addAuthorAndDate author date)
369 return $ spanWith attr ils
369 return $ spanWith attr ils
370parPartToInlines' (ChangedRuns (TrackedChange Deletion (ChangeInfo _ author date)) runs) = do
370parPartToInlines' (ChangedRuns (TrackedChange Deletion (ChangeInfo _ author date)) runs) = do
371 opts <- asks docxOptions
371 opts <- asks docxOptions
@@ -374,7 +374,7 @@ parPartToInlines' (ChangedRuns (TrackedChange Deletion (ChangeInfo _ author date
374 RejectChanges -> smushInlines <$> mapM runToInlines runs
374 RejectChanges -> smushInlines <$> mapM runToInlines runs
375 AllChanges -> do
375 AllChanges -> do
376 ils <- smushInlines <$> mapM runToInlines runs
376 ils <- smushInlines <$> mapM runToInlines runs
377− let attr = ("", ["deletion"], [("author", author), ("date", date)])
377+ let attr = ("", ["deletion"], addAuthorAndDate author date)
378 return $ spanWith attr ils
378 return $ spanWith attr ils
379parPartToInlines' (CommentStart cmtId author date bodyParts) = do
379parPartToInlines' (CommentStart cmtId author date bodyParts) = do
380 opts <- asks docxOptions
380 opts <- asks docxOptions
@@ -382,7 +382,7 @@ parPartToInlines' (CommentStart cmtId author date bodyParts) = do
382 AllChanges -> do
382 AllChanges -> do
383 blks <- smushBlocks <$> mapM bodyPartToBlocks bodyParts
383 blks <- smushBlocks <$> mapM bodyPartToBlocks bodyParts
384 ils <- blocksToInlinesWarn cmtId blks
384 ils <- blocksToInlinesWarn cmtId blks
385− let attr = ("", ["comment-start"], [("id", cmtId), ("author", author), ("date", date)])
385+ let attr = ("", ["comment-start"], ("id", cmtId) : addAuthorAndDate author date)
386 return $ spanWith attr ils
386 return $ spanWith attr ils
387 _ -> return mempty
387 _ -> return mempty
388parPartToInlines' (CommentEnd cmtId) = do
388parPartToInlines' (CommentEnd cmtId) = do
@@ -593,7 +593,7 @@ bodyPartToBlocks (Paragraph pPr parparts)
593 return mempty
593 return mempty
594 (Just (TrackedChange Insertion (ChangeInfo _ cAuthor cDate))
594 (Just (TrackedChange Insertion (ChangeInfo _ cAuthor cDate))
595 , AllChanges) -> do
595 , AllChanges) -> do
596− let attr = ("", ["paragraph-insertion"], [("author", cAuthor), ("date", cDate)])
596+ let attr = ("", ["paragraph-insertion"], addAuthorAndDate cAuthor cDate)
597 insertMark = spanWith attr mempty
597 insertMark = spanWith attr mempty
598 transform <- parStyleToTransform pPr'
598 transform <- parStyleToTransform pPr'
599 return $ transform $
599 return $ transform $
@@ -605,7 +605,7 @@ bodyPartToBlocks (Paragraph pPr parparts)
605 handleInsertion
605 handleInsertion
606 (Just (TrackedChange Deletion (ChangeInfo _ cAuthor cDate))
606 (Just (TrackedChange Deletion (ChangeInfo _ cAuthor cDate))
607 , AllChanges) -> do
607 , AllChanges) -> do
608− let attr = ("", ["paragraph-deletion"], [("author", cAuthor), ("date", cDate)])
608+ let attr = ("", ["paragraph-deletion"], addAuthorAndDate cAuthor cDate)
609 insertMark = spanWith attr mempty
609 insertMark = spanWith attr mempty
610 transform <- parStyleToTransform pPr'
610 transform <- parStyleToTransform pPr'
611 return $ transform $
611 return $ transform $
@@ -732,3 +732,8 @@ docxToOutput :: PandocMonad m
732docxToOutput opts (Docx (Document _ body)) =
732docxToOutput opts (Docx (Document _ body)) =
733 let dEnv = def { docxOptions = opts} in
733 let dEnv = def { docxOptions = opts} in
734 evalDocxContext (bodyToOutput body) dEnv def
734 evalDocxContext (bodyToOutput body) dEnv def
735+
736+addAuthorAndDate :: T.Text -> Maybe T.Text -> [(T.Text, T.Text)]
737+addAuthorAndDate author mdate =
738+ ("author", author) : maybe [] (\date -> [("date", date)]) mdate
739+
src/Text/Pandoc/Readers/Docx/Parse.hs+7−7
@@ -213,7 +213,7 @@ data ParIndentation = ParIndentation { leftParIndent :: Maybe Integer
213data ChangeType = Insertion | Deletion
213data ChangeType = Insertion | Deletion
214 deriving Show
214 deriving Show
215
215
216−data ChangeInfo = ChangeInfo ChangeId Author ChangeDate
216+data ChangeInfo = ChangeInfo ChangeId Author (Maybe ChangeDate)
217 deriving Show
217 deriving Show
218
218
219data TrackedChange = TrackedChange ChangeType ChangeInfo
219data TrackedChange = TrackedChange ChangeType ChangeInfo
@@ -276,7 +276,7 @@ type Extent = Maybe (Double, Double)
276
276
277data ParPart = PlainRun Run
277data ParPart = PlainRun Run
278 | ChangedRuns TrackedChange [Run]
278 | ChangedRuns TrackedChange [Run]
279− | CommentStart CommentId Author CommentDate [BodyPart]
279+ | CommentStart CommentId Author (Maybe CommentDate) [BodyPart]
280 | CommentEnd CommentId
280 | CommentEnd CommentId
281 | BookMark BookMarkId Anchor
281 | BookMark BookMarkId Anchor
282 | InternalHyperLink Anchor [Run]
282 | InternalHyperLink Anchor [Run]
@@ -852,7 +852,7 @@ elemToCommentStart ns element
852 | isElem ns "w" "comment" element
852 | isElem ns "w" "comment" element
853 , Just cmtId <- findAttrTextByName ns "w" "id" element
853 , Just cmtId <- findAttrTextByName ns "w" "id" element
854 , Just cmtAuthor <- findAttrTextByName ns "w" "author" element
854 , Just cmtAuthor <- findAttrTextByName ns "w" "author" element
855− , Just cmtDate <- findAttrTextByName ns "w" "date" element = do
855+ , cmtDate <- findAttrTextByName ns "w" "date" element = do
856 bps <- mapD (elemToBodyPart ns) (elChildren element)
856 bps <- mapD (elemToBodyPart ns) (elChildren element)
857 return $ CommentStart cmtId cmtAuthor cmtDate bps
857 return $ CommentStart cmtId cmtAuthor cmtDate bps
858elemToCommentStart _ _ = throwError WrongElem
858elemToCommentStart _ _ = throwError WrongElem
@@ -958,14 +958,14 @@ getTrackedChange ns element
958 | isElem ns "w" "ins" element || isElem ns "w" "moveTo" element
958 | isElem ns "w" "ins" element || isElem ns "w" "moveTo" element
959 , Just cId <- findAttrTextByName ns "w" "id" element
959 , Just cId <- findAttrTextByName ns "w" "id" element
960 , Just cAuthor <- findAttrTextByName ns "w" "author" element
960 , Just cAuthor <- findAttrTextByName ns "w" "author" element
961− , Just cDate <- findAttrTextByName ns "w" "date" element =
961+ , mcDate <- findAttrTextByName ns "w" "date" element =
962− Just $ TrackedChange Insertion (ChangeInfo cId cAuthor cDate)
962+ Just $ TrackedChange Insertion (ChangeInfo cId cAuthor mcDate)
963getTrackedChange ns element
963getTrackedChange ns element
964 | isElem ns "w" "del" element || isElem ns "w" "moveFrom" element
964 | isElem ns "w" "del" element || isElem ns "w" "moveFrom" element
965 , Just cId <- findAttrTextByName ns "w" "id" element
965 , Just cId <- findAttrTextByName ns "w" "id" element
966 , Just cAuthor <- findAttrTextByName ns "w" "author" element
966 , Just cAuthor <- findAttrTextByName ns "w" "author" element
967− , Just cDate <- findAttrTextByName ns "w" "date" element =
967+ , mcDate <- findAttrTextByName ns "w" "date" element =
968− Just $ TrackedChange Deletion (ChangeInfo cId cAuthor cDate)
968+ Just $ TrackedChange Deletion (ChangeInfo cId cAuthor mcDate)
969getTrackedChange _ _ = Nothing
969getTrackedChange _ _ = Nothing
970
970
971elemToParagraphStyle :: NameSpaces -> Element -> ParStyleMap -> ParagraphStyle
971elemToParagraphStyle :: NameSpaces -> Element -> ParStyleMap -> ParagraphStyle
src/Text/Pandoc/Writers/Docx.hs+7−11
@@ -1244,33 +1244,29 @@ inlineToOpenXML' opts (Span (ident,classes,kvs) ils) = do
1244 else id)
1244 else id)
1245 getChangeAuthorDate = do
1245 getChangeAuthorDate = do
1246 defaultAuthor <- asks envChangesAuthor
1246 defaultAuthor <- asks envChangesAuthor
1247− defaultDate <- asks envChangesDate
1248 let author = fromMaybe defaultAuthor (lookup "author" kvs)
1247 let author = fromMaybe defaultAuthor (lookup "author" kvs)
1249− date = fromMaybe defaultDate (lookup "date" kvs)
1248+ let mdate = lookup "date" kvs
1250− return (author, date)
1249+ return $ ("w:author", T.unpack author) :
1250+ maybe [] (\date -> [("w:date", T.unpack date)]) mdate
1251 insmod <- if "insertion" `elem` classes
1251 insmod <- if "insertion" `elem` classes
1252 then do
1252 then do
1253− (author, date) <- getChangeAuthorDate
1253+ changeAuthorDate <- getChangeAuthorDate
1254 insId <- gets stInsId
1254 insId <- gets stInsId
1255 modify $ \s -> s{stInsId = insId + 1}
1255 modify $ \s -> s{stInsId = insId + 1}
1256 return $ \f -> do
1256 return $ \f -> do
1257 x <- f
1257 x <- f
1258 return [ mknode "w:ins"
1258 return [ mknode "w:ins"
1259− [("w:id", show insId),
1259+ (("w:id", show insId) : changeAuthorDate) x]
1260− ("w:author", T.unpack author),
1261− ("w:date", T.unpack date)] x ]
1262 else return id
1260 else return id
1263 delmod <- if "deletion" `elem` classes
1261 delmod <- if "deletion" `elem` classes
1264 then do
1262 then do
1265− (author, date) <- getChangeAuthorDate
1263+ changeAuthorDate <- getChangeAuthorDate
1266 delId <- gets stDelId
1264 delId <- gets stDelId
1267 modify $ \s -> s{stDelId = delId + 1}
1265 modify $ \s -> s{stDelId = delId + 1}
1268 return $ \f -> local (\env->env{envInDel=True}) $ do
1266 return $ \f -> local (\env->env{envInDel=True}) $ do
1269 x <- f
1267 x <- f
1270 return [mknode "w:del"
1268 return [mknode "w:del"
1271− [("w:id", show delId),
1269+ (("w:id", show delId) : changeAuthorDate) x]
1272− ("w:author", T.unpack author),
1273− ("w:date", T.unpack date)] x]
1274 else return id
1270 else return id
1275 contents <- insmod $ delmod $ dirmod $ stylemod $ pmod
1271 contents <- insmod $ delmod $ dirmod $ stylemod $ pmod
1276 $ inlinesToOpenXML opts ils
1272 $ inlinesToOpenXML opts ils
test/Tests/Readers/Docx.hs+4
@@ -405,6 +405,10 @@ tests = [ testGroup "document"
405 "paragraph insertion/deletion (all)"
405 "paragraph insertion/deletion (all)"
406 "docx/paragraph_insertion_deletion.docx"
406 "docx/paragraph_insertion_deletion.docx"
407 "docx/paragraph_insertion_deletion_all.native"
407 "docx/paragraph_insertion_deletion_all.native"
408+ , testCompareWithOpts def{readerTrackChanges=AllChanges}
409+ "paragraph insertion/deletion (all)"
410+ "docx/track_changes_scrubbed_metadata.docx"
411+ "docx/track_changes_scrubbed_metadata.native"
408 , testForWarningsWithOpts def{readerTrackChanges=AcceptChanges}
412 , testForWarningsWithOpts def{readerTrackChanges=AcceptChanges}
409 "comment warnings (accept -- no warnings)"
413 "comment warnings (accept -- no warnings)"
410 "docx/comments_warning.docx"
414 "docx/comments_warning.docx"
test/Tests/Writers/Docx.hs+5
@@ -150,6 +150,11 @@ tests = [ testGroup "inlines"
150 def
150 def
151 "docx/comments.native"
151 "docx/comments.native"
152 "docx/golden/comments.docx"
152 "docx/golden/comments.docx"
153+ , docxTest
154+ "scrubbed metadata"
155+ def
156+ "docx/track_changes_scrubbed_metadata.native"
157+ "docx/golden/track_changes_scrubbed_metadata.docx"
153 ]
158 ]
154 , testGroup "custom styles"
159 , testGroup "custom styles"
155 [ docxTest "custom styles without reference.docx"
160 [ docxTest "custom styles without reference.docx"
test/docx/golden/track_changes_scrubbed_metadata.docxnew file

Binary file not shown.

test/docx/track_changes_scrubbed_metadata.docxnew file

Binary file not shown.

test/docx/track_changes_scrubbed_metadata.nativenew file+9
@@ -0,0 +1,9 @@
1+[Para [ Str "Here", Space, Str "is", Space, Str "a", Space
2+ , Span ("",["deletion"],[("author","Author")]) [Str "dummy"]
3+ , Span ("",["insertion"],[("author","Author")]) [Str "test"]
4+ , Space
5+ , Span ("",["comment-start"],[("id","3"),("author","Author")])
6+ [Str "With",Space,Str "a",Space,Str "comment!"]
7+ , Str "document",Span ("",["comment-end"],[("id","3")]) [],Str "."
8+ ]
9+]