git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: Allow numbering in the style file.

This allows inherited styles with numbering (lists). It works like this: 1. check to see if the style has numbering info. 2. if the paragraph has explicit numbering info in the doc that takes precedence. 3. if not we use the numbering info in the style, if it's there. 4. otherwise normal paragraph. We no longer assume it's not a numbering element if it doesn't have an explicit level---we just set that level to 1. (In the style files, the examples I've seen don't have that explicit level.)

commita01913b45c00891006a935f0d4f0a0cf2e054190 authorJesse Rosenthal <jrosenthal@jhu.edu> dateFri 13 Feb 2015, 14:07 (12 years ago) parent3480189
1 file changed+33−21
src/Text/Pandoc/Readers/Docx/Parse.hs+33−21
src/Text/Pandoc/Readers/Docx/Parse.hs+33−21
@@ -158,6 +158,7 @@ data ParagraphStyle = ParagraphStyle { pStyle :: [String]
158158 , indentation :: Maybe ParIndentation
159159 , dropCap :: Bool
160160 , pHeading :: Maybe (String, Int)
161+ , pNumInfo :: Maybe (String, String)
161162 , pBlockQuote :: Maybe Bool
162163 }
163164 deriving Show
@@ -167,6 +168,7 @@ defaultParagraphStyle = ParagraphStyle { pStyle = []
167168 , indentation = Nothing
168169 , dropCap = False
169170 , pHeading = Nothing
171+ , pNumInfo = Nothing
170172 , pBlockQuote = Nothing
171173 }
172174
@@ -224,6 +226,7 @@ data RunStyle = RunStyle { isBold :: Maybe Bool
224226
225227data ParStyleData = ParStyleData { headingLev :: Maybe (String, Int)
226228 , isBlockQuote :: Maybe Bool
229+ , numInfo :: Maybe (String, String)
227230 , psStyle :: Maybe ParStyle}
228231 deriving Show
229232
@@ -546,20 +549,6 @@ elemToParIndentation ns element | isElem ns "w" "ind" element =
546549 stringToInteger}
547550elemToParIndentation _ _ = Nothing
548551
549−
550−elemToNumInfo :: NameSpaces -> Element -> Maybe (String, String)
551−elemToNumInfo ns element | isElem ns "w" "p" element = do
552− let pPr = findChild (elemName ns "w" "pPr") element
553− numPr = pPr >>= findChild (elemName ns "w" "numPr")
554− lvl <- numPr >>=
555− findChild (elemName ns "w" "ilvl") >>=
556− findAttr (elemName ns "w" "val")
557− numId <- numPr >>=
558− findChild (elemName ns "w" "numId") >>=
559− findAttr (elemName ns "w" "val")
560− return (numId, lvl)
561−elemToNumInfo _ _ = Nothing
562−
563552testBitMask :: String -> Int -> Bool
564553testBitMask bitMaskS n =
565554 case (reads ("0x" ++ bitMaskS) :: [(Int, String)]) of
@@ -578,20 +567,28 @@ elemToBodyPart ns element
578567 return $ OMathPara expsLst
579568elemToBodyPart ns element
580569 | isElem ns "w" "p" element
581− , Just (numId, lvl) <- elemToNumInfo ns element = do
570+ , Just (numId, lvl) <- getNumInfo ns element = do
582571 sty <- asks envParStyles
583572 let parstyle = elemToParagraphStyle ns element sty
584573 parparts <- mapD (elemToParPart ns) (elChildren element)
585574 num <- asks envNumbering
586575 case lookupLevel numId lvl num of
587− Just levelInfo -> return $ ListItem parstyle numId lvl levelInfo parparts
588− Nothing -> throwError WrongElem
576+ Just levelInfo -> return $ ListItem parstyle numId lvl levelInfo parparts
577+ Nothing -> throwError WrongElem
589578elemToBodyPart ns element
590579 | isElem ns "w" "p" element = do
591− sty <- asks envParStyles
592− let parstyle = elemToParagraphStyle ns element sty
593− parparts <- mapD (elemToParPart ns) (elChildren element)
594− return $ Paragraph parstyle parparts
580+ sty <- asks envParStyles
581+ let parstyle = elemToParagraphStyle ns element sty
582+ parparts <- mapD (elemToParPart ns) (elChildren element)
583+ case pNumInfo parstyle of
584+ Just (numId, lvl) -> do
585+ num <- asks envNumbering
586+ case lookupLevel numId lvl num of
587+ Just levelInfo ->
588+ return $ ListItem parstyle numId lvl levelInfo parparts
589+ Nothing ->
590+ throwError WrongElem
591+ Nothing -> return $ Paragraph parstyle parparts
595592elemToBodyPart ns element
596593 | isElem ns "w" "tbl" element = do
597594 let caption' = findChild (elemName ns "w" "tblPr") element
@@ -771,6 +768,7 @@ elemToParagraphStyle ns element sty
771768 Just _ -> True
772769 Nothing -> False
773770 , pHeading = getParStyleField headingLev sty style
771+ , pNumInfo = getParStyleField numInfo sty style
774772 , pBlockQuote = getParStyleField isBlockQuote sty style
775773 }
776774elemToParagraphStyle _ _ _ = defaultParagraphStyle
@@ -857,12 +855,26 @@ getBlockQuote ns element
857855 , styleName `elem` blockQuoteStyleNames = Just True
858856getBlockQuote _ _ = Nothing
859857
858+getNumInfo :: NameSpaces -> Element -> Maybe (String, String)
859+getNumInfo ns element = do
860+ let numPr = findChild (elemName ns "w" "pPr") element >>=
861+ findChild (elemName ns "w" "numPr")
862+ lvl = fromMaybe "0" (numPr >>=
863+ findChild (elemName ns "w" "ilvl") >>=
864+ findAttr (elemName ns "w" "val"))
865+ numId <- numPr >>=
866+ findChild (elemName ns "w" "numId") >>=
867+ findAttr (elemName ns "w" "val")
868+ return (numId, lvl)
869+
870+
860871elemToParStyleData :: NameSpaces -> Element -> Maybe ParStyle -> ParStyleData
861872elemToParStyleData ns element parentStyle =
862873 ParStyleData
863874 {
864875 headingLev = getHeaderLevel ns element
865876 , isBlockQuote = getBlockQuote ns element
877+ , numInfo = getNumInfo ns element
866878 , psStyle = parentStyle
867879 }
868880