From 4a99e142ecbea4c9f16729468e45223ba200fcef Mon Sep 17 00:00:00 2001
From: Jesse Rosenthal <jrosenthal@jhu.edu>
Date: Wed, 2 Nov 2016 12:28:56 -0400
Subject: Docx Reader: abstract out function to avoid code repetition.

---
 src/Text/Pandoc/Readers/Docx/Parse.hs | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

(limited to 'src')

diff --git a/src/Text/Pandoc/Readers/Docx/Parse.hs b/src/Text/Pandoc/Readers/Docx/Parse.hs
index a821d0693..94e3e29cf 100644
--- a/src/Text/Pandoc/Readers/Docx/Parse.hs
+++ b/src/Text/Pandoc/Readers/Docx/Parse.hs
@@ -645,18 +645,23 @@ expandDrawingId s = do
         Nothing -> throwError DocxError
     Nothing -> throwError DocxError
 
+getTitleAndAlt :: NameSpaces -> Element -> (String, String)
+getTitleAndAlt ns element =
+  let mbDocPr = findChild (elemName ns "wp" "inline") element >>=
+                findChild (elemName ns "wp" "docPr")
+      title = case mbDocPr >>= findAttr (elemName ns "" "title") of
+                Just title' -> title'
+                Nothing     -> ""
+      alt = case mbDocPr >>= findAttr (elemName ns "" "descr") of
+              Just alt' -> alt'
+              Nothing   -> ""
+  in (title, alt)
+
 elemToParPart :: NameSpaces -> Element -> D ParPart
 elemToParPart ns element
   | isElem ns "w" "r" element
   , Just drawingElem <- findChild (elemName ns "w" "drawing") element =
-    let mbDocPr = findChild (elemName ns "wp" "inline") drawingElem >>=
-                  findChild (elemName ns "wp" "docPr")
-        alt = case mbDocPr >>= findAttr (elemName ns "" "descr") of
-                Just alt' -> alt'
-                Nothing   -> ""
-        title = case mbDocPr >>= findAttr (elemName ns "" "title") of
-                Just title' -> title'
-                Nothing     -> ""
+    let (title, alt) = getTitleAndAlt ns drawingElem
         a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
         drawing = findElement (QName "blip" (Just a_ns) (Just "a")) element
                   >>= findAttr (elemName ns "r" "embed")
@@ -760,14 +765,7 @@ elemToExtent drawingElem =
 childElemToRun :: NameSpaces -> Element -> D Run
 childElemToRun ns element
   | isElem ns "w" "drawing" element =
-    let mbDocPr = findChild (elemName ns "wp" "inline") element >>=
-                  findChild (elemName ns "wp" "docPr")
-        alt = case mbDocPr >>= findAttr (elemName ns "" "descr") of
-                Just alt' -> alt'
-                Nothing   -> ""
-        title = case mbDocPr >>= findAttr (elemName ns "" "title") of
-                Just title' -> title'
-                Nothing     -> ""
+    let (title, alt) = getTitleAndAlt ns element
         a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
         drawing = findElement (QName "blip" (Just a_ns) (Just "a")) element
                   >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
-- 
cgit v1.2.3