git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: Handle alternate content

Some word functions -- especially graphics -- give various choices for content so there can be backwards compatibility. This follows the largely undocumented feature by working through the choices until we find one that works. Note that we had to split out the processing of child elems of runs into a separate function so we can recurse properly. Any processing of an element *within* a run (other than a plain run) should go into `childElemToRun`.

commit28c7617f19c4d6dd69e2aa9c904af13e11e4e639 authorJesse Rosenthal <jrosenthal@jhu.edu> dateFri 18 Mar 2016, 09:38 (11 years ago) parent7f4a404
1 file changed+37−14
src/Text/Pandoc/Readers/Docx/Parse.hs+37−14
src/Text/Pandoc/Readers/Docx/Parse.hs+37−14
@@ -717,36 +717,58 @@ elemToExtent drawingElem =
717717 getDim at = findElement (QName "extent" (Just wp_ns) (Just "wp")) drawingElem
718718 >>= findAttr (QName at Nothing Nothing) >>= safeRead
719719
720−elemToRun :: NameSpaces -> Element -> D Run
721−elemToRun ns element
722− | isElem ns "w" "r" element
723− , Just drawingElem <- findChild (elemName ns "w" "drawing") element =
720+
721+childElemToRun :: NameSpaces -> Element -> D Run
722+childElemToRun ns element
723+ | isElem ns "w" "drawing" element =
724724 let a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
725− drawing = findElement (QName "blip" (Just a_ns) (Just "a")) drawingElem
725+ drawing = findElement (QName "blip" (Just a_ns) (Just "a")) element
726726 >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
727727 in
728728 case drawing of
729729 Just s -> expandDrawingId s >>=
730− (\(fp, bs) -> return $ InlineDrawing fp bs $ elemToExtent drawingElem)
730+ (\(fp, bs) -> return $ InlineDrawing fp bs $ elemToExtent element)
731731 Nothing -> throwError WrongElem
732−elemToRun ns element
733− | isElem ns "w" "r" element
734− , Just ref <- findChild (elemName ns "w" "footnoteReference") element
735− , Just fnId <- findAttr (elemName ns "w" "id") ref = do
732+childElemToRun ns element
733+ | isElem ns "w" "footnoteReference" element
734+ , Just fnId <- findAttr (elemName ns "w" "id") element = do
736735 notes <- asks envNotes
737736 case lookupFootnote fnId notes of
738737 Just e -> do bps <- local (\r -> r {envLocation=InFootnote}) $ mapD (elemToBodyPart ns) (elChildren e)
739738 return $ Footnote bps
740739 Nothing -> return $ Footnote []
741−elemToRun ns element
742− | isElem ns "w" "r" element
743− , Just ref <- findChild (elemName ns "w" "endnoteReference") element
744− , Just enId <- findAttr (elemName ns "w" "id") ref = do
740+childElemToRun ns element
741+ | isElem ns "w" "endnoteReference" element
742+ , Just enId <- findAttr (elemName ns "w" "id") element = do
745743 notes <- asks envNotes
746744 case lookupEndnote enId notes of
747745 Just e -> do bps <- local (\r -> r {envLocation=InEndnote}) $ mapD (elemToBodyPart ns) (elChildren e)
748746 return $ Endnote bps
749747 Nothing -> return $ Endnote []
748+childElemToRun _ _ = throwError WrongElem
749+
750+elemToRun :: NameSpaces -> Element -> D Run
751+elemToRun ns element
752+ | isElem ns "w" "r" element
753+ , Just altCont <- findChild (elemName ns "mc" "AlternateContent") element =
754+ do let choices = findChildren (elemName ns "mc" "Choice") altCont
755+ choiceChildren = map head $ filter (not . null) $ map elChildren choices
756+ outputs <- mapD (childElemToRun ns) choiceChildren
757+ case outputs of
758+ r : _ -> return r
759+ [] -> throwError WrongElem
760+elemToRun ns element
761+ | isElem ns "w" "r" element
762+ , Just drawingElem <- findChild (elemName ns "w" "drawing") element =
763+ childElemToRun ns drawingElem
764+elemToRun ns element
765+ | isElem ns "w" "r" element
766+ , Just ref <- findChild (elemName ns "w" "footnoteReference") element =
767+ childElemToRun ns ref
768+elemToRun ns element
769+ | isElem ns "w" "r" element
770+ , Just ref <- findChild (elemName ns "w" "endnoteReference") element =
771+ childElemToRun ns ref
750772elemToRun ns element
751773 | isElem ns "w" "r" element = do
752774 runElems <- elemToRunElems ns element
@@ -955,3 +977,4 @@ elemToRunElems _ _ = throwError WrongElem
955977
956978setFont :: Maybe Font -> ReaderEnv -> ReaderEnv
957979setFont f s = s{envFont = f}
980+