git.iko.soy/typeable/pandoc
on master help sign in
DOCX reader: Allow empty dates in comments and tracked changes (#6726)

For security reasons, some legal firms delete the date from comments and tracked changes. * Make date optional (Maybe) in tracked changes and comments datatypes * Add tests

commiteda5540719e9771b48f15aa7f431033163e1f161 authorDiego Balseiro <dbalseiro@stackbuilders.com> dateTue 6 Oct 2020, 23:03 (6 years ago) parenta27a0b5
8 files changed+42−23
src/Text/Pandoc/Readers/Docx.hs+10−5 src/Text/Pandoc/Readers/Docx/Parse.hs+7−7 src/Text/Pandoc/Writers/Docx.hs+7−11 test/Tests/Readers/Docx.hs+4 test/Tests/Writers/Docx.hs+5 test/docx/golden/track_changes_scrubbed_metadata.docx test/docx/track_changes_scrubbed_metadata.docx test/docx/track_changes_scrubbed_metadata.native+9
src/Text/Pandoc/Readers/Docx.hs+10−5
@@ -365,7 +365,7 @@ parPartToInlines' (ChangedRuns (TrackedChange Insertion (ChangeInfo _ author dat
365365 RejectChanges -> return mempty
366366 AllChanges -> do
367367 ils <- smushInlines <$> mapM runToInlines runs
368− let attr = ("", ["insertion"], [("author", author), ("date", date)])
368+ let attr = ("", ["insertion"], addAuthorAndDate author date)
369369 return $ spanWith attr ils
370370parPartToInlines' (ChangedRuns (TrackedChange Deletion (ChangeInfo _ author date)) runs) = do
371371 opts <- asks docxOptions
@@ -374,7 +374,7 @@ parPartToInlines' (ChangedRuns (TrackedChange Deletion (ChangeInfo _ author date
374374 RejectChanges -> smushInlines <$> mapM runToInlines runs
375375 AllChanges -> do
376376 ils <- smushInlines <$> mapM runToInlines runs
377− let attr = ("", ["deletion"], [("author", author), ("date", date)])
377+ let attr = ("", ["deletion"], addAuthorAndDate author date)
378378 return $ spanWith attr ils
379379parPartToInlines' (CommentStart cmtId author date bodyParts) = do
380380 opts <- asks docxOptions
@@ -382,7 +382,7 @@ parPartToInlines' (CommentStart cmtId author date bodyParts) = do
382382 AllChanges -> do
383383 blks <- smushBlocks <$> mapM bodyPartToBlocks bodyParts
384384 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)
386386 return $ spanWith attr ils
387387 _ -> return mempty
388388parPartToInlines' (CommentEnd cmtId) = do
@@ -593,7 +593,7 @@ bodyPartToBlocks (Paragraph pPr parparts)
593593 return mempty
594594 (Just (TrackedChange Insertion (ChangeInfo _ cAuthor cDate))
595595 , AllChanges) -> do
596− let attr = ("", ["paragraph-insertion"], [("author", cAuthor), ("date", cDate)])
596+ let attr = ("", ["paragraph-insertion"], addAuthorAndDate cAuthor cDate)
597597 insertMark = spanWith attr mempty
598598 transform <- parStyleToTransform pPr'
599599 return $ transform $
@@ -605,7 +605,7 @@ bodyPartToBlocks (Paragraph pPr parparts)
605605 handleInsertion
606606 (Just (TrackedChange Deletion (ChangeInfo _ cAuthor cDate))
607607 , AllChanges) -> do
608− let attr = ("", ["paragraph-deletion"], [("author", cAuthor), ("date", cDate)])
608+ let attr = ("", ["paragraph-deletion"], addAuthorAndDate cAuthor cDate)
609609 insertMark = spanWith attr mempty
610610 transform <- parStyleToTransform pPr'
611611 return $ transform $
@@ -732,3 +732,8 @@ docxToOutput :: PandocMonad m
732732docxToOutput opts (Docx (Document _ body)) =
733733 let dEnv = def { docxOptions = opts} in
734734 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
213213data ChangeType = Insertion | Deletion
214214 deriving Show
215215
216−data ChangeInfo = ChangeInfo ChangeId Author ChangeDate
216+data ChangeInfo = ChangeInfo ChangeId Author (Maybe ChangeDate)
217217 deriving Show
218218
219219data TrackedChange = TrackedChange ChangeType ChangeInfo
@@ -276,7 +276,7 @@ type Extent = Maybe (Double, Double)
276276
277277data ParPart = PlainRun Run
278278 | ChangedRuns TrackedChange [Run]
279− | CommentStart CommentId Author CommentDate [BodyPart]
279+ | CommentStart CommentId Author (Maybe CommentDate) [BodyPart]
280280 | CommentEnd CommentId
281281 | BookMark BookMarkId Anchor
282282 | InternalHyperLink Anchor [Run]
@@ -852,7 +852,7 @@ elemToCommentStart ns element
852852 | isElem ns "w" "comment" element
853853 , Just cmtId <- findAttrTextByName ns "w" "id" element
854854 , Just cmtAuthor <- findAttrTextByName ns "w" "author" element
855− , Just cmtDate <- findAttrTextByName ns "w" "date" element = do
855+ , cmtDate <- findAttrTextByName ns "w" "date" element = do
856856 bps <- mapD (elemToBodyPart ns) (elChildren element)
857857 return $ CommentStart cmtId cmtAuthor cmtDate bps
858858elemToCommentStart _ _ = throwError WrongElem
@@ -958,14 +958,14 @@ getTrackedChange ns element
958958 | isElem ns "w" "ins" element || isElem ns "w" "moveTo" element
959959 , Just cId <- findAttrTextByName ns "w" "id" element
960960 , Just cAuthor <- findAttrTextByName ns "w" "author" element
961− , Just cDate <- findAttrTextByName ns "w" "date" element =
962− Just $ TrackedChange Insertion (ChangeInfo cId cAuthor cDate)
961+ , mcDate <- findAttrTextByName ns "w" "date" element =
962+ Just $ TrackedChange Insertion (ChangeInfo cId cAuthor mcDate)
963963getTrackedChange ns element
964964 | isElem ns "w" "del" element || isElem ns "w" "moveFrom" element
965965 , Just cId <- findAttrTextByName ns "w" "id" element
966966 , Just cAuthor <- findAttrTextByName ns "w" "author" element
967− , Just cDate <- findAttrTextByName ns "w" "date" element =
968− Just $ TrackedChange Deletion (ChangeInfo cId cAuthor cDate)
967+ , mcDate <- findAttrTextByName ns "w" "date" element =
968+ Just $ TrackedChange Deletion (ChangeInfo cId cAuthor mcDate)
969969getTrackedChange _ _ = Nothing
970970
971971elemToParagraphStyle :: NameSpaces -> Element -> ParStyleMap -> ParagraphStyle
src/Text/Pandoc/Writers/Docx.hs+7−11
@@ -1244,33 +1244,29 @@ inlineToOpenXML' opts (Span (ident,classes,kvs) ils) = do
12441244 else id)
12451245 getChangeAuthorDate = do
12461246 defaultAuthor <- asks envChangesAuthor
1247− defaultDate <- asks envChangesDate
12481247 let author = fromMaybe defaultAuthor (lookup "author" kvs)
1249− date = fromMaybe defaultDate (lookup "date" kvs)
1250− return (author, date)
1248+ let mdate = lookup "date" kvs
1249+ return $ ("w:author", T.unpack author) :
1250+ maybe [] (\date -> [("w:date", T.unpack date)]) mdate
12511251 insmod <- if "insertion" `elem` classes
12521252 then do
1253− (author, date) <- getChangeAuthorDate
1253+ changeAuthorDate <- getChangeAuthorDate
12541254 insId <- gets stInsId
12551255 modify $ \s -> s{stInsId = insId + 1}
12561256 return $ \f -> do
12571257 x <- f
12581258 return [ mknode "w:ins"
1259− [("w:id", show insId),
1260− ("w:author", T.unpack author),
1261− ("w:date", T.unpack date)] x ]
1259+ (("w:id", show insId) : changeAuthorDate) x]
12621260 else return id
12631261 delmod <- if "deletion" `elem` classes
12641262 then do
1265− (author, date) <- getChangeAuthorDate
1263+ changeAuthorDate <- getChangeAuthorDate
12661264 delId <- gets stDelId
12671265 modify $ \s -> s{stDelId = delId + 1}
12681266 return $ \f -> local (\env->env{envInDel=True}) $ do
12691267 x <- f
12701268 return [mknode "w:del"
1271− [("w:id", show delId),
1272− ("w:author", T.unpack author),
1273− ("w:date", T.unpack date)] x]
1269+ (("w:id", show delId) : changeAuthorDate) x]
12741270 else return id
12751271 contents <- insmod $ delmod $ dirmod $ stylemod $ pmod
12761272 $ inlinesToOpenXML opts ils
test/Tests/Readers/Docx.hs+4
@@ -405,6 +405,10 @@ tests = [ testGroup "document"
405405 "paragraph insertion/deletion (all)"
406406 "docx/paragraph_insertion_deletion.docx"
407407 "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"
408412 , testForWarningsWithOpts def{readerTrackChanges=AcceptChanges}
409413 "comment warnings (accept -- no warnings)"
410414 "docx/comments_warning.docx"
test/Tests/Writers/Docx.hs+5
@@ -150,6 +150,11 @@ tests = [ testGroup "inlines"
150150 def
151151 "docx/comments.native"
152152 "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"
153158 ]
154159 , testGroup "custom styles"
155160 [ 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+]