git.iko.soy/typeable/pandoc
on master help sign in
[Docx Reader] Only use bCs/iCs on runs with rtl or cs property

Fixes #6514

commite2c6fa41d5eec084b7ee1ff6a040bff2369cc497 authorNikolay Yakimov <root@livid.pp.ru> dateMon 13 Jul 2020, 19:03 (6 years ago) parent6195df7
3 files changed+59−55
src/Text/Pandoc/Readers/Docx.hs+44−49 src/Text/Pandoc/Readers/Docx/Parse.hs+3 src/Text/Pandoc/Readers/Docx/Parse/Styles.hs+12−6
src/Text/Pandoc/Readers/Docx.hs+44−49
@@ -261,46 +261,43 @@ resolveDependentRunStyle rPr
261261 | otherwise = return rPr
262262
263263runStyleToTransform :: PandocMonad m => RunStyle -> DocxContext m (Inlines -> Inlines)
264−runStyleToTransform rPr
265− | Just sn <- getStyleName <$> rParentStyle rPr
266− , sn `elem` spansToKeep = do
267− transform <- runStyleToTransform rPr{rParentStyle = Nothing}
268− return $ spanWith ("", [normalizeToClassName sn], []) . transform
269− | Just s <- rParentStyle rPr = do
270− ei <- extraInfo spanWith s
271− transform <- runStyleToTransform rPr{rParentStyle = Nothing}
272− return $ ei . transform
273− | Just True <- isItalic rPr = do
274− transform <- runStyleToTransform rPr{isItalic = Nothing}
275− return $ emph . transform
276− | Just True <- isBold rPr = do
277− transform <- runStyleToTransform rPr{isBold = Nothing}
278− return $ strong . transform
279− | Just True <- isSmallCaps rPr = do
280− transform <- runStyleToTransform rPr{isSmallCaps = Nothing}
281− return $ smallcaps . transform
282− | Just True <- isStrike rPr = do
283− transform <- runStyleToTransform rPr{isStrike = Nothing}
284− return $ strikeout . transform
285− | Just True <- isRTL rPr = do
286− transform <- runStyleToTransform rPr{isRTL = Nothing}
287− return $ spanWith ("",[],[("dir","rtl")]) . transform
288− | Just False <- isRTL rPr = do
289− transform <- runStyleToTransform rPr{isRTL = Nothing}
290− inBidi <- asks docxInBidi
291− return $ if inBidi
292− then spanWith ("",[],[("dir","ltr")]) . transform
293− else transform
294− | Just SupScrpt <- rVertAlign rPr = do
295− transform <- runStyleToTransform rPr{rVertAlign = Nothing}
296− return $ superscript . transform
297− | Just SubScrpt <- rVertAlign rPr = do
298− transform <- runStyleToTransform rPr{rVertAlign = Nothing}
299− return $ subscript . transform
300− | Just "single" <- rUnderline rPr = do
301− transform <- runStyleToTransform rPr{rUnderline = Nothing}
302− return $ Pandoc.underline . transform
303− | otherwise = return id
264+runStyleToTransform rPr' = do
265+ opts <- asks docxOptions
266+ inBidi <- asks docxInBidi
267+ let styles = isEnabled Ext_styles opts
268+ ctl = (Just True == isRTL rPr') || (Just True == isForceCTL rPr')
269+ italic rPr | ctl = isItalicCTL rPr
270+ | otherwise = isItalic rPr
271+ bold rPr | ctl = isBoldCTL rPr
272+ | otherwise = isBold rPr
273+ go rPr
274+ | Just sn <- getStyleName <$> rParentStyle rPr
275+ , sn `elem` spansToKeep =
276+ spanWith ("", [normalizeToClassName sn], [])
277+ . go rPr{rParentStyle = Nothing}
278+ | styles, Just s <- rParentStyle rPr =
279+ spanWith (extraAttr s) . go rPr{rParentStyle = Nothing}
280+ | Just True <- italic rPr =
281+ emph . go rPr{isItalic = Nothing, isItalicCTL = Nothing}
282+ | Just True <- bold rPr =
283+ strong . go rPr{isBold = Nothing, isBoldCTL = Nothing}
284+ | Just True <- isSmallCaps rPr =
285+ smallcaps . go rPr{isSmallCaps = Nothing}
286+ | Just True <- isStrike rPr =
287+ strikeout . go rPr{isStrike = Nothing}
288+ | Just True <- isRTL rPr =
289+ spanWith ("",[],[("dir","rtl")]) . go rPr{isRTL = Nothing}
290+ | inBidi, Just False <- isRTL rPr =
291+ spanWith ("",[],[("dir","ltr")]) . go rPr{isRTL = Nothing}
292+ | Just SupScrpt <- rVertAlign rPr =
293+ superscript . go rPr{rVertAlign = Nothing}
294+ | Just SubScrpt <- rVertAlign rPr = do
295+ subscript . go rPr{rVertAlign = Nothing}
296+ | Just "single" <- rUnderline rPr = do
297+ Pandoc.underline . go rPr{rUnderline = Nothing}
298+ | otherwise = id
299+ return $ go rPr'
300+
304301
305302runToInlines :: PandocMonad m => Run -> DocxContext m Inlines
306303runToInlines (Run rs runElems)
@@ -512,13 +509,8 @@ trimSps (Many ils) = Many $ Seq.dropWhileL isSp $Seq.dropWhileR isSp ils
512509 isSp LineBreak = True
513510 isSp _ = False
514511
515−extraInfo :: (Eq (StyleName a), PandocMonad m, HasStyleName a)
516− => (Attr -> i -> i) -> a -> DocxContext m (i -> i)
517−extraInfo f s = do
518− opts <- asks docxOptions
519− return $ if isEnabled Ext_styles opts
520− then f ("", [], [("custom-style", fromStyleName $ getStyleName s)])
521− else id
512+extraAttr :: (Eq (StyleName a), HasStyleName a) => a -> Attr
513+extraAttr s = ("", [], [("custom-style", fromStyleName $ getStyleName s)])
522514
523515parStyleToTransform :: PandocMonad m => ParagraphStyle -> DocxContext m (Blocks -> Blocks)
524516parStyleToTransform pPr = case pStyle pPr of
@@ -534,8 +526,11 @@ parStyleToTransform pPr = case pStyle pPr of
534526 | otherwise -> do
535527 let pPr' = pPr { pStyle = cs }
536528 transform <- parStyleToTransform pPr'
537− ei <- extraInfo divWith c
538− return $ ei . (if isBlockQuote c then blockQuote else id) . transform
529+ styles <- asks (isEnabled Ext_styles . docxOptions)
530+ return $
531+ (if styles then divWith (extraAttr c) else id)
532+ . (if isBlockQuote c then blockQuote else id)
533+ . transform
539534 []
540535 | Just left <- indentation pPr >>= leftParIndent -> do
541536 let pPr' = pPr { indentation = Nothing }
src/Text/Pandoc/Readers/Docx/Parse.hs+3
@@ -259,10 +259,13 @@ newtype Cell = Cell [BodyPart]
259259leftBiasedMergeRunStyle :: RunStyle -> RunStyle -> RunStyle
260260leftBiasedMergeRunStyle a b = RunStyle
261261 { isBold = isBold a <|> isBold b
262+ , isBoldCTL = isBoldCTL a <|> isBoldCTL b
262263 , isItalic = isItalic a <|> isItalic b
264+ , isItalicCTL = isItalicCTL a <|> isItalicCTL b
263265 , isSmallCaps = isSmallCaps a <|> isSmallCaps b
264266 , isStrike = isStrike a <|> isStrike b
265267 , isRTL = isRTL a <|> isRTL b
268+ , isForceCTL = isForceCTL a <|> isForceCTL b
266269 , rVertAlign = rVertAlign a <|> rVertAlign b
267270 , rUnderline = rUnderline a <|> rUnderline b
268271 , rParentStyle = rParentStyle a
src/Text/Pandoc/Readers/Docx/Parse/Styles.hs+12−6
@@ -44,7 +44,6 @@ module Text.Pandoc.Readers.Docx.Parse.Styles (
4444 ) where
4545import Codec.Archive.Zip
4646import Control.Applicative ((<|>))
47−import Control.Monad.Except
4847import Data.Function (on)
4948import Data.String (IsString(..))
5049import qualified Data.Map as M
@@ -101,10 +100,13 @@ data CharStyle = CharStyle { cStyleId :: CharStyleId
101100 } deriving (Show)
102101
103102data RunStyle = RunStyle { isBold :: Maybe Bool
103+ , isBoldCTL :: Maybe Bool
104104 , isItalic :: Maybe Bool
105+ , isItalicCTL :: Maybe Bool
105106 , isSmallCaps :: Maybe Bool
106107 , isStrike :: Maybe Bool
107108 , isRTL :: Maybe Bool
109+ , isForceCTL :: Maybe Bool
108110 , rVertAlign :: Maybe VertAlign
109111 , rUnderline :: Maybe String
110112 , rParentStyle :: Maybe CharStyle
@@ -121,10 +123,13 @@ data ParStyle = ParStyle { headingLev :: Maybe (ParaStyleName, Int)
121123
122124defaultRunStyle :: RunStyle
123125defaultRunStyle = RunStyle { isBold = Nothing
126+ , isBoldCTL = Nothing
124127 , isItalic = Nothing
128+ , isItalicCTL = Nothing
125129 , isSmallCaps = Nothing
126130 , isStrike = Nothing
127131 , isRTL = Nothing
132+ , isForceCTL = Nothing
128133 , rVertAlign = Nothing
129134 , rUnderline = Nothing
130135 , rParentStyle = Nothing
@@ -240,20 +245,21 @@ elemToCharStyle :: NameSpaces
240245elemToCharStyle ns element parentStyle
241246 = CharStyle <$> (CharStyleId <$> findAttrTextByName ns "w" "styleId" element)
242247 <*> getElementStyleName ns element
243− <*> (Just $ elemToRunStyle ns element parentStyle)
248+ <*> Just (elemToRunStyle ns element parentStyle)
244249
245250elemToRunStyle :: NameSpaces -> Element -> Maybe CharStyle -> RunStyle
246251elemToRunStyle ns element parentStyle
247252 | Just rPr <- findChildByName ns "w" "rPr" element =
248253 RunStyle
249254 {
250− isBold = checkOnOff ns rPr (elemName ns "w" "b") `mplus`
251− checkOnOff ns rPr (elemName ns "w" "bCs")
252− , isItalic = checkOnOff ns rPr (elemName ns "w" "i") `mplus`
253− checkOnOff ns rPr (elemName ns "w" "iCs")
255+ isBold = checkOnOff ns rPr (elemName ns "w" "b")
256+ , isBoldCTL = checkOnOff ns rPr (elemName ns "w" "bCs")
257+ , isItalic = checkOnOff ns rPr (elemName ns "w" "i")
258+ , isItalicCTL = checkOnOff ns rPr (elemName ns "w" "iCs")
254259 , isSmallCaps = checkOnOff ns rPr (elemName ns "w" "smallCaps")
255260 , isStrike = checkOnOff ns rPr (elemName ns "w" "strike")
256261 , isRTL = checkOnOff ns rPr (elemName ns "w" "rtl")
262+ , isForceCTL = checkOnOff ns rPr (elemName ns "w" "cs")
257263 , rVertAlign =
258264 findChildByName ns "w" "vertAlign" rPr >>=
259265 findAttrByName ns "w" "val" >>=