git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1372 from jkr/track-changes

Rudimentary track-changes support

commite3beae5845d63d3d8e23dd565a2c02fe9ded6145 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 25 Jun 2014, 12:50 (12 years ago) parent5ba4760a2b6ab8
7 files changed+39−9
src/Text/Pandoc/Readers/Docx.hs+3 src/Text/Pandoc/Readers/Docx/Parse.hs+24−9 tests/Tests/Readers/Docx.hs+10 tests/docx.track_changes_deletion.docx tests/docx.track_changes_deletion_only_ins.native+1 tests/docx.track_changes_insertion.docx tests/docx.track_changes_insertion_only_ins.native+1
src/Text/Pandoc/Readers/Docx.hs+3
@@ -234,6 +234,9 @@ runToInlines opts docx@(Docx _ notes _ _ _) (Endnote fnId) =
234234
235235parPartToInlines :: ReaderOptions -> Docx -> ParPart -> [Inline]
236236parPartToInlines opts docx (PlainRun r) = runToInlines opts docx r
237+parPartToInlines opts docx (Insertion _ _ _ runs) =
238+ concatMap (runToInlines opts docx) runs
239+parPartToInlines _ _ (Deletion _ _ _ _) = []
237240parPartToInlines _ _ (BookMark _ anchor) | anchor `elem` dummyAnchors = []
238241parPartToInlines _ _ (BookMark _ anchor) = [Span (anchor, ["anchor"], []) []]
239242parPartToInlines _ (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
281281 $ map (elemToBodyPart ns) $ filterChildrenName (isParOrTbl ns) element
282282elemToBody _ _ = Nothing
283283
284−isRunOrLinkOrBookmark :: NameSpaces -> QName -> Bool
285−isRunOrLinkOrBookmark ns q = qName q `elem` ["r", "hyperlink", "bookmarkStart"] &&
286− qURI q == (lookup "w" ns)
287−
288284elemToNumInfo :: NameSpaces -> Element -> Maybe (String, String)
289285elemToNumInfo ns element
290286 | qName (elName element) == "p" &&
@@ -319,9 +315,8 @@ elemToBodyPart ns element
319315 | qName (elName element) == "p" &&
320316 qURI (elName element) == (lookup "w" ns) =
321317 let parstyle = elemToParagraphStyle ns element
322− parparts = mapMaybe id
323− $ map (elemToParPart ns)
324− $ filterChildrenName (isRunOrLinkOrBookmark ns) element
318+ parparts = mapMaybe (elemToParPart ns)
319+ $ elChildren element
325320 in
326321 case elemToNumInfo ns element of
327322 Just (numId, lvl) -> Just $ ListItem parstyle numId lvl parparts
@@ -455,6 +450,8 @@ elemToCell ns element
455450elemToCell _ _ = Nothing
456451
457452data ParPart = PlainRun Run
453+ | Insertion ChangeId Author ChangeDate [Run]
454+ | Deletion ChangeId Author ChangeDate [Run]
458455 | BookMark BookMarkId Anchor
459456 | InternalHyperLink Anchor [Run]
460457 | ExternalHyperLink RelId [Run]
@@ -539,7 +536,7 @@ elemToRun _ _ = Nothing
539536
540537elemToRunElem :: NameSpaces -> Element -> Maybe RunElem
541538elemToRunElem ns element
542− | qName (elName element) == "t" &&
539+ | (qName (elName element) == "t" || qName (elName element) == "delText") &&
543540 qURI (elName element) == (lookup "w" ns) =
544541 Just $ TextRun (strContent element)
545542 | qName (elName element) == "br" &&
@@ -579,6 +576,22 @@ elemToParPart ns element
579576 Nothing -> do
580577 r <- elemToRun ns element
581578 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
582595elemToParPart ns element
583596 | qName (elName element) == "bookmarkStart" &&
584597 qURI (elName element) == (lookup "w" ns) = do
@@ -604,4 +617,6 @@ type Target = String
604617type Anchor = String
605618type BookMarkId = String
606619type 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"
122122 "docx.codeblock.native"
123123
124124 ]
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+ ]
125135 ]
126136
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."]]