git.iko.soy/typeable/pandoc
on master help sign in
Code cleanup
commit65c80822e7900e92b4bba67912da77062654cc26 authorNikolay Yakimov <root@livid.pp.ru> dateTue 3 Mar 2015, 13:08 (12 years ago) parent409111f
2 files changed+22−26
src/Text/Pandoc/Readers/Docx/StyleMap.hs+12−12 src/Text/Pandoc/Writers/Docx.hs+10−14
src/Text/Pandoc/Readers/Docx/StyleMap.hs+12−12
@@ -1,7 +1,4 @@
1−module Text.Pandoc.Readers.Docx.StyleMap ( StyleMap
2− , ParaStyleMap
3− , CharStyleMap
4− , StyleMaps(..)
1+module Text.Pandoc.Readers.Docx.StyleMap ( StyleMaps(..)
52 , defaultStyleMaps
63 , getStyleMaps
74 , getStyleId
@@ -58,23 +55,26 @@ getStyleMaps :: Element -> StyleMaps
5855getStyleMaps docElem = fromMaybe state' $ execStateT genStyleMap state'
5956 where
6057 state' = defaultStyleMaps {sNameSpaces = elemToNameSpaces docElem}
61− insertPara key val = modify $ \s ->
62− s { sParaStyleMap = insert key val $ sParaStyleMap s }
63− insertChar key val = modify $ \s ->
64− s { sCharStyleMap = insert key val $ sCharStyleMap s }
6558 genStyleItem e = do
6659 styleType <- getStyleType e
67− nameVal <- getNameVal e
6860 styleId <- getAttrStyleId e
69− let nameValLC = map toLower nameVal
61+ nameValLowercase <- map toLower `fmap` getNameVal e
7062 case styleType of
71− ParaStyle -> insertPara nameValLC styleId
72− CharStyle -> insertChar nameValLC styleId
63+ ParaStyle -> modParaStyleMap $ insert nameValLowercase styleId
64+ CharStyle -> modCharStyleMap $ insert nameValLowercase styleId
7365 genStyleMap = do
7466 style <- elemName' "style"
7567 let styles = findChildren style docElem
7668 forM_ styles genStyleItem
7769
70+modParaStyleMap :: (ParaStyleMap -> ParaStyleMap) -> StateM ()
71+modParaStyleMap f = modify $ \s ->
72+ s {sParaStyleMap = f $ sParaStyleMap s}
73+
74+modCharStyleMap :: (CharStyleMap -> CharStyleMap) -> StateM ()
75+modCharStyleMap f = modify $ \s ->
76+ s {sCharStyleMap = f $ sCharStyleMap s}
77+
7878getStyleType :: Element -> StateM StyleType
7979getStyleType e = do
8080 styleTypeStr <- getAttrType e
src/Text/Pandoc/Writers/Docx.hs+10−14
@@ -620,27 +620,23 @@ writeOpenXML opts (Pandoc meta blocks) = do
620620blocksToOpenXML :: WriterOptions -> [Block] -> WS [Element]
621621blocksToOpenXML opts bls = concat `fmap` mapM (blockToOpenXML opts) bls
622622
623−pStyle :: String -> StyleMaps -> Element
624−pStyle sty m = mknode "w:pStyle" [("w:val",sty')] ()
625− where
626− sty' = getStyleId sty $ sParaStyleMap m
627−
628623pCustomStyle :: String -> Element
629624pCustomStyle sty = mknode "w:pStyle" [("w:val",sty)] ()
630625
631626pStyleM :: String -> WS XML.Element
632−pStyleM = (`fmap` gets stStyleMaps) . pStyle
633−
634−rStyle :: String -> StyleMaps -> Element
635−rStyle sty m = mknode "w:rStyle" [("w:val",sty')] ()
636− where
637− sty' = getStyleId sty $ sCharStyleMap m
627+pStyleM styleName = do
628+ styleMaps <- gets stStyleMaps
629+ let sty' = getStyleId styleName $ sParaStyleMap styleMaps
630+ return $ mknode "w:pStyle" [("w:val",sty')] ()
638631
639632rCustomStyle :: String -> Element
640633rCustomStyle sty = mknode "w:rStyle" [("w:val",sty)] ()
641634
642635rStyleM :: String -> WS XML.Element
643−rStyleM = (`fmap` gets stStyleMaps) . rStyle
636+rStyleM styleName = do
637+ styleMaps <- gets stStyleMaps
638+ let sty' = getStyleId styleName $ sCharStyleMap styleMaps
639+ return $ mknode "w:rStyle" [("w:val",sty')] ()
644640
645641getUniqueId :: MonadIO m => m String
646642-- the + 20 is to ensure that there are no clashes with the rIds
@@ -689,10 +685,10 @@ blockToOpenXML opts (Para lst) = do
689685 paraProps <- getParaProps $ case lst of
690686 [Math DisplayMath _] -> True
691687 _ -> False
692− sm <- gets stStyleMaps
688+ bodyTextStyle <- pStyleM "Body Text"
693689 let paraProps' = case paraProps of
694690 [] | isFirstPara -> [mknode "w:pPr" [] [pCustomStyle "FirstParagraph"]]
695− [] -> [mknode "w:pPr" [] [pStyle "Body Text" sm]]
691+ [] -> [mknode "w:pPr" [] [bodyTextStyle]]
696692 ps -> ps
697693 modify $ \s -> s { stFirstPara = False }
698694 contents <- inlinesToOpenXML opts lst