git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1372 from jkr/track-changes
e3beae5John MacFarlane
src/Text/Pandoc/Readers/Docx.hs+3
@@ -234,6 +234,9 @@ runToInlines opts docx@(Docx _ notes _ _ _) (Endnote fnId) =
234
234
235parPartToInlines :: ReaderOptions -> Docx -> ParPart -> [Inline]
235parPartToInlines :: ReaderOptions -> Docx -> ParPart -> [Inline]
236parPartToInlines opts docx (PlainRun r) = runToInlines opts docx r
236parPartToInlines opts docx (PlainRun r) = runToInlines opts docx r
237+parPartToInlines opts docx (Insertion _ _ _ runs) =
238+ concatMap (runToInlines opts docx) runs
239+parPartToInlines _ _ (Deletion _ _ _ _) = []
237parPartToInlines _ _ (BookMark _ anchor) | anchor `elem` dummyAnchors = []
240parPartToInlines _ _ (BookMark _ anchor) | anchor `elem` dummyAnchors = []
238parPartToInlines _ _ (BookMark _ anchor) = [Span (anchor, ["anchor"], []) []]
241parPartToInlines _ _ (BookMark _ anchor) = [Span (anchor, ["anchor"], []) []]
239parPartToInlines _ (Docx _ _ _ rels _) (Drawing relid) =
242parPartToInlines _ (Docx _ _ _ rels _) (Drawing relid) =
src/Text/Pandoc/Readers/Docx/Parse.hs+24−9
@@ -281,10 +281,6 @@ elemToBody ns element | qName (elName element) == "body" && qURI (elName element
281 $ map (elemToBodyPart ns) $ filterChildrenName (isParOrTbl ns) element
281 $ map (elemToBodyPart ns) $ filterChildrenName (isParOrTbl ns) element
282elemToBody _ _ = Nothing
282elemToBody _ _ = Nothing
283
283
284−isRunOrLinkOrBookmark :: NameSpaces -> QName -> Bool
285−isRunOrLinkOrBookmark ns q = qName q `elem` ["r", "hyperlink", "bookmarkStart"] &&
286− qURI q == (lookup "w" ns)
287−
288elemToNumInfo :: NameSpaces -> Element -> Maybe (String, String)
284elemToNumInfo :: NameSpaces -> Element -> Maybe (String, String)
289elemToNumInfo ns element
285elemToNumInfo ns element
290 | qName (elName element) == "p" &&
286 | qName (elName element) == "p" &&
@@ -319,9 +315,8 @@ elemToBodyPart ns element
319 | qName (elName element) == "p" &&
315 | qName (elName element) == "p" &&
320 qURI (elName element) == (lookup "w" ns) =
316 qURI (elName element) == (lookup "w" ns) =
321 let parstyle = elemToParagraphStyle ns element
317 let parstyle = elemToParagraphStyle ns element
322− parparts = mapMaybe id
318+ parparts = mapMaybe (elemToParPart ns)
323− $ map (elemToParPart ns)
319+ $ elChildren element
324− $ filterChildrenName (isRunOrLinkOrBookmark ns) element
325 in
320 in
326 case elemToNumInfo ns element of
321 case elemToNumInfo ns element of
327 Just (numId, lvl) -> Just $ ListItem parstyle numId lvl parparts
322 Just (numId, lvl) -> Just $ ListItem parstyle numId lvl parparts
@@ -455,6 +450,8 @@ elemToCell ns element
455elemToCell _ _ = Nothing
450elemToCell _ _ = Nothing
456
451
457data ParPart = PlainRun Run
452data ParPart = PlainRun Run
453+ | Insertion ChangeId Author ChangeDate [Run]
454+ | Deletion ChangeId Author ChangeDate [Run]
458 | BookMark BookMarkId Anchor
455 | BookMark BookMarkId Anchor
459 | InternalHyperLink Anchor [Run]
456 | InternalHyperLink Anchor [Run]
460 | ExternalHyperLink RelId [Run]
457 | ExternalHyperLink RelId [Run]
@@ -539,7 +536,7 @@ elemToRun _ _ = Nothing
539
536
540elemToRunElem :: NameSpaces -> Element -> Maybe RunElem
537elemToRunElem :: NameSpaces -> Element -> Maybe RunElem
541elemToRunElem ns element
538elemToRunElem ns element
542− | qName (elName element) == "t" &&
539+ | (qName (elName element) == "t" || qName (elName element) == "delText") &&
543 qURI (elName element) == (lookup "w" ns) =
540 qURI (elName element) == (lookup "w" ns) =
544 Just $ TextRun (strContent element)
541 Just $ TextRun (strContent element)
545 | qName (elName element) == "br" &&
542 | qName (elName element) == "br" &&
@@ -579,6 +576,22 @@ elemToParPart ns element
579 Nothing -> do
576 Nothing -> do
580 r <- elemToRun ns element
577 r <- elemToRun ns element
581 return $ PlainRun r
578 return $ PlainRun r
579+elemToParPart ns element
580+ | qName (elName element) == "ins" &&
581+ qURI (elName element) == (lookup "w" ns) = do
582+ cId <- findAttr (QName "id" (lookup "w" ns) (Just "w")) element
583+ cAuthor <- findAttr (QName "author" (lookup "w" ns) (Just "w")) element
584+ cDate <- findAttr (QName "date" (lookup "w" ns) (Just "w")) element
585+ let runs = mapMaybe (elemToRun ns) (elChildren element)
586+ return $ Insertion cId cAuthor cDate runs
587+elemToParPart ns element
588+ | qName (elName element) == "del" &&
589+ qURI (elName element) == (lookup "w" ns) = do
590+ cId <- findAttr (QName "id" (lookup "w" ns) (Just "w")) element
591+ cAuthor <- findAttr (QName "author" (lookup "w" ns) (Just "w")) element
592+ cDate <- findAttr (QName "date" (lookup "w" ns) (Just "w")) element
593+ let runs = mapMaybe (elemToRun ns) (elChildren element)
594+ return $ Deletion cId cAuthor cDate runs
582elemToParPart ns element
595elemToParPart ns element
583 | qName (elName element) == "bookmarkStart" &&
596 | qName (elName element) == "bookmarkStart" &&
584 qURI (elName element) == (lookup "w" ns) = do
597 qURI (elName element) == (lookup "w" ns) = do
@@ -604,4 +617,6 @@ type Target = String
604type Anchor = String
617type Anchor = String
605type BookMarkId = String
618type BookMarkId = String
606type RelId = String
619type RelId = String
607−
620+type ChangeId = String
621+type Author = String
622+type ChangeDate = String
tests/Tests/Readers/Docx.hs+10
@@ -122,5 +122,15 @@ tests = [ testGroup "inlines"
122 "docx.codeblock.native"
122 "docx.codeblock.native"
123
123
124 ]
124 ]
125+ , testGroup "track changes"
126+ [ testCompare
127+ "insert insertion (insertions only)"
128+ "docx.track_changes_insertion.docx"
129+ "docx.track_changes_insertion_only_ins.native"
130+ , testCompare
131+ "skip deletion (insertions only)"
132+ "docx.track_changes_deletion.docx"
133+ "docx.track_changes_deletion_only_ins.native"
134+ ]
125 ]
135 ]
126
136
tests/docx.track_changes_deletion.docxnew file

Binary file not shown.

tests/docx.track_changes_deletion_only_ins.nativenew file+1
@@ -0,0 +1 @@
1+[Para [Str "This",Space,Str "is",Space,Str "a",Space,Str "text",Space,Str "with",Space,Str "a",Space,Str "deletion."]]
tests/docx.track_changes_insertion.docxnew file

Binary file not shown.

tests/docx.track_changes_insertion_only_ins.nativenew file+1
@@ -0,0 +1 @@
1+[Para [Str "This",Space,Str "is",Space,Str "a",Space,Str "text",Space,Str "with",Space,Str "two",Space,Str "exciting",Space,Str "insertions."]]