git.iko.soy/typeable/pandoc
on master help sign in
[Docx Reader] Code clean-up

Reduce code duplication, remove redundant brackets, use newtype instead of data where appropriate

commitfd14ad52618c98928ab83aa43689158cc788c4a8 authorNikolay Yakimov <root@livid.pp.ru> dateSun 8 Sep 2019, 16:47 (7 years ago) parent6ceed95
2 files changed+39−63
src/Text/Pandoc/Readers/Docx.hs+25−45 src/Text/Pandoc/Readers/Docx/Parse.hs+14−18
src/Text/Pandoc/Readers/Docx.hs+25−45
@@ -2,6 +2,7 @@
22{-# LANGUAGE CPP #-}
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE PatternGuards #-}
5+{-# LANGUAGE MultiWayIf #-}
56{- |
67 Module : Text.Pandoc.Readers.Docx
78 Copyright : Copyright (C) 2014-2019 Jesse Rosenthal
@@ -129,7 +130,7 @@ instance Default DEnv where
129130type DocxContext m = ReaderT DEnv (StateT DState m)
130131
131132evalDocxContext :: PandocMonad m => DocxContext m a -> DEnv -> DState -> m a
132−evalDocxContext ctx env st = flip evalStateT st $flip runReaderT env ctx
133+evalDocxContext ctx env st = flip evalStateT st $ runReaderT ctx env
133134
134135-- This is empty, but we put it in for future-proofing.
135136spansToKeep :: [String]
@@ -537,15 +538,6 @@ parStyleToTransform pPr
537538 let pPr' = pPr { pStyle = cs, indentation = Nothing}
538539 transform <- parStyleToTransform pPr'
539540 return $ divWith ("", [c], []) . transform
540− | (c:cs) <- pStyle pPr
541− , Just True <- pBlockQuote pPr = do
542− opts <- asks docxOptions
543− let pPr' = pPr { pStyle = cs }
544− transform <- parStyleToTransform pPr'
545− let extraInfo = if isEnabled Ext_styles opts
546− then divWith ("", [], [("custom-style", c)])
547− else id
548− return $ extraInfo . blockQuote . transform
549541 | (c:cs) <- pStyle pPr = do
550542 opts <- asks docxOptions
551543 let pPr' = pPr { pStyle = cs}
@@ -553,22 +545,15 @@ parStyleToTransform pPr
553545 let extraInfo = if isEnabled Ext_styles opts
554546 then divWith ("", [], [("custom-style", c)])
555547 else id
556− return $ extraInfo . transform
548+ return $ extraInfo . (if fromMaybe False (pBlockQuote pPr) then blockQuote else id) . transform
557549 | null (pStyle pPr)
558− , Just left <- indentation pPr >>= leftParIndent
559− , Just hang <- indentation pPr >>= hangingParIndent = do
550+ , Just left <- indentation pPr >>= leftParIndent = do
560551 let pPr' = pPr { indentation = Nothing }
552+ hang = fromMaybe 0 $ indentation pPr >>= hangingParIndent
561553 transform <- parStyleToTransform pPr'
562− return $ case (left - hang) > 0 of
563− True -> blockQuote . transform
564− False -> transform
565− | null (pStyle pPr),
566− Just left <- indentation pPr >>= leftParIndent = do
567− let pPr' = pPr { indentation = Nothing }
568− transform <- parStyleToTransform pPr'
569− return $ case left > 0 of
570− True -> blockQuote . transform
571− False -> transform
554+ return $ if (left - hang) > 0
555+ then blockQuote . transform
556+ else transform
572557parStyleToTransform _ = return id
573558
574559bodyPartToBlocks :: PandocMonad m => BodyPart -> DocxContext m Blocks
@@ -585,7 +570,7 @@ bodyPartToBlocks (Paragraph pPr parparts)
585570 makeHeaderAnchor $
586571 headerWith ("", delete style (pStyle pPr), []) n ils
587572 | otherwise = do
588− ils <- (trimSps . smushInlines) <$> mapM parPartToInlines parparts
573+ ils <- trimSps . smushInlines <$> mapM parPartToInlines parparts
589574 prevParaIls <- gets docxPrevPara
590575 dropIls <- gets docxDropCap
591576 let ils' = dropIls <> ils
@@ -596,21 +581,21 @@ bodyPartToBlocks (Paragraph pPr parparts)
596581 let ils'' = prevParaIls <>
597582 (if isNull prevParaIls then mempty else space) <>
598583 ils'
584+ handleInsertion = do
585+ modify $ \s -> s {docxPrevPara = mempty}
586+ transform <- parStyleToTransform pPr
587+ return $ transform $ para ils''
599588 opts <- asks docxOptions
600− case () of
601−
602− _ | isNull ils'' && not (isEnabled Ext_empty_paragraphs opts) ->
589+ if | isNull ils'' && not (isEnabled Ext_empty_paragraphs opts) ->
603590 return mempty
604− _ | Just (TrackedChange Insertion _) <- pChange pPr
605− , AcceptChanges <- readerTrackChanges opts -> do
606− modify $ \s -> s {docxPrevPara = mempty}
607− transform <- parStyleToTransform pPr
608− return $ transform $ para ils''
609− _ | Just (TrackedChange Insertion _) <- pChange pPr
591+ | Just (TrackedChange Insertion _) <- pChange pPr
592+ , AcceptChanges <- readerTrackChanges opts ->
593+ handleInsertion
594+ | Just (TrackedChange Insertion _) <- pChange pPr
610595 , RejectChanges <- readerTrackChanges opts -> do
611596 modify $ \s -> s {docxPrevPara = ils''}
612597 return mempty
613− _ | Just (TrackedChange Insertion cInfo) <- pChange pPr
598+ | Just (TrackedChange Insertion cInfo) <- pChange pPr
614599 , AllChanges <- readerTrackChanges opts
615600 , ChangeInfo _ cAuthor cDate <- cInfo -> do
616601 let attr = ("", ["paragraph-insertion"], [("author", cAuthor), ("date", cDate)])
@@ -618,16 +603,14 @@ bodyPartToBlocks (Paragraph pPr parparts)
618603 transform <- parStyleToTransform pPr
619604 return $ transform $
620605 para $ ils'' <> insertMark
621− _ | Just (TrackedChange Deletion _) <- pChange pPr
606+ | Just (TrackedChange Deletion _) <- pChange pPr
622607 , AcceptChanges <- readerTrackChanges opts -> do
623608 modify $ \s -> s {docxPrevPara = ils''}
624609 return mempty
625− _ | Just (TrackedChange Deletion _) <- pChange pPr
626− , RejectChanges <- readerTrackChanges opts -> do
627− modify $ \s -> s {docxPrevPara = mempty}
628− transform <- parStyleToTransform pPr
629− return $ transform $ para ils''
630− _ | Just (TrackedChange Deletion cInfo) <- pChange pPr
610+ | Just (TrackedChange Deletion _) <- pChange pPr
611+ , RejectChanges <- readerTrackChanges opts ->
612+ handleInsertion
613+ | Just (TrackedChange Deletion cInfo) <- pChange pPr
631614 , AllChanges <- readerTrackChanges opts
632615 , ChangeInfo _ cAuthor cDate <- cInfo -> do
633616 let attr = ("", ["paragraph-deletion"], [("author", cAuthor), ("date", cDate)])
@@ -635,10 +618,7 @@ bodyPartToBlocks (Paragraph pPr parparts)
635618 transform <- parStyleToTransform pPr
636619 return $ transform $
637620 para $ ils'' <> insertMark
638− _ | otherwise -> do
639− modify $ \s -> s {docxPrevPara = mempty}
640− transform <- parStyleToTransform pPr
641− return $ transform $ para ils''
621+ | otherwise -> handleInsertion
642622bodyPartToBlocks (ListItem pPr numId lvl (Just levelInfo) parparts) = do
643623 -- We check whether this current numId has previously been used,
644624 -- since Docx expects us to pick up where we left off.
src/Text/Pandoc/Readers/Docx/Parse.hs+14−18
@@ -121,9 +121,9 @@ unwrap :: NameSpaces -> Content -> [Content]
121121unwrap ns (Elem element)
122122 | isElem ns "w" "sdt" element
123123 , Just sdtContent <- findChildByName ns "w" "sdtContent" element
124− = concatMap ((unwrap ns) . Elem) (elChildren sdtContent)
124+ = concatMap (unwrap ns . Elem) (elChildren sdtContent)
125125 | isElem ns "w" "smartTag" element
126− = concatMap ((unwrap ns) . Elem) (elChildren element)
126+ = concatMap (unwrap ns . Elem) (elChildren element)
127127unwrap _ content = [content]
128128
129129unwrapChild :: NameSpaces -> Content -> Content
@@ -149,13 +149,13 @@ walkDocument ns element =
149149 _ -> Nothing
150150
151151
152−data Docx = Docx Document
152+newtype Docx = Docx Document
153153 deriving Show
154154
155155data Document = Document NameSpaces Body
156156 deriving Show
157157
158−data Body = Body [BodyPart]
158+newtype Body = Body [BodyPart]
159159 deriving Show
160160
161161type Media = [(FilePath, B.ByteString)]
@@ -242,16 +242,16 @@ data BodyPart = Paragraph ParagraphStyle [ParPart]
242242
243243type TblGrid = [Integer]
244244
245−data TblLook = TblLook {firstRowFormatting::Bool}
245+newtype TblLook = TblLook {firstRowFormatting::Bool}
246246 deriving Show
247247
248248defaultTblLook :: TblLook
249249defaultTblLook = TblLook{firstRowFormatting = False}
250250
251−data Row = Row [Cell]
251+newtype Row = Row [Cell]
252252 deriving Show
253253
254−data Cell = Cell [BodyPart]
254+newtype Cell = Cell [BodyPart]
255255 deriving Show
256256
257257-- (width, height) in EMUs
@@ -495,7 +495,7 @@ filePathToRelType "word/_rels/endnotes.xml.rels" _ = Just InEndnote
495495-- -- to see if it's a documentPath, we have to check against the dynamic
496496-- -- docPath specified in "_rels/.rels"
497497filePathToRelType path docXmlPath =
498− if path == "word/_rels/" ++ (takeFileName docXmlPath) ++ ".rels"
498+ if path == "word/_rels/" ++ takeFileName docXmlPath ++ ".rels"
499499 then Just InDocument
500500 else Nothing
501501
@@ -537,7 +537,7 @@ lookupLevel numId ilvl (Numbering _ numbs absNumbs) = do
537537 case lvlOverride of
538538 Just (LevelOverride _ _ (Just lvl')) -> Just lvl'
539539 Just (LevelOverride _ (Just strt) _) ->
540− lookup ilvl $ map (\(Level i fmt s _) -> (i, (Level i fmt s (Just strt)))) lvls
540+ lookup ilvl $ map (\(Level i fmt s _) -> (i, Level i fmt s (Just strt))) lvls
541541 _ ->
542542 lookup ilvl $ map (\l@(Level i _ _ _) -> (i, l)) lvls
543543
@@ -703,23 +703,19 @@ elemToBodyPart ns element
703703elemToBodyPart ns element
704704 | isElem ns "w" "p" element
705705 , Just (numId, lvl) <- getNumInfo ns element = do
706− sty <- asks envParStyles
707− let parstyle = elemToParagraphStyle ns element sty
706+ parstyle <- elemToParagraphStyle ns element <$> asks envParStyles
708707 parparts <- mapD (elemToParPart ns) (elChildren element)
709− num <- asks envNumbering
710− let levelInfo = lookupLevel numId lvl num
708+ levelInfo <- lookupLevel numId lvl <$> asks envNumbering
711709 return $ ListItem parstyle numId lvl levelInfo parparts
712710elemToBodyPart ns element
713711 | isElem ns "w" "p" element = do
714− sty <- asks envParStyles
715− let parstyle = elemToParagraphStyle ns element sty
712+ parstyle <- elemToParagraphStyle ns element <$> asks envParStyles
716713 parparts <- mapD (elemToParPart ns) (elChildren element)
717714 -- Word uses list enumeration for numbered headings, so we only
718715 -- want to infer a list from the styles if it is NOT a heading.
719716 case pHeading parstyle of
720717 Nothing | Just (numId, lvl) <- pNumInfo parstyle -> do
721− num <- asks envNumbering
722− let levelInfo = lookupLevel numId lvl num
718+ levelInfo <- lookupLevel numId lvl <$> asks envNumbering
723719 return $ ListItem parstyle numId lvl levelInfo parparts
724720 _ -> return $ Paragraph parstyle parparts
725721elemToBodyPart ns element
@@ -727,7 +723,7 @@ elemToBodyPart ns element
727723 let caption' = findChildByName ns "w" "tblPr" element
728724 >>= findChildByName ns "w" "tblCaption"
729725 >>= findAttrByName ns "w" "val"
730− caption = (fromMaybe "" caption')
726+ caption = fromMaybe "" caption'
731727 grid' = case findChildByName ns "w" "tblGrid" element of
732728 Just g -> elemToTblGrid ns g
733729 Nothing -> return []