git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: simplify custom-style reading code.
3e10caaJesse Rosenthal
src/Text/Pandoc/Readers/Docx.hs+23−41
@@ -142,11 +142,10 @@ instance Default DState where
142
142
143data DEnv = DEnv { docxOptions :: ReaderOptions
143data DEnv = DEnv { docxOptions :: ReaderOptions
144 , docxInHeaderBlock :: Bool
144 , docxInHeaderBlock :: Bool
145− , docxCustomStyleAlready :: Bool
146 }
145 }
147
146
148instance Default DEnv where
147instance Default DEnv where
149− def = DEnv def False False
148+ def = DEnv def False
150
149
151type DocxContext m = ReaderT DEnv (StateT DState m)
150type DocxContext m = ReaderT DEnv (StateT DState m)
152
151
@@ -283,16 +282,6 @@ resolveDependentRunStyle rPr
283 , rStyle = rStyle rPr }
282 , rStyle = rStyle rPr }
284 | otherwise = return rPr
283 | otherwise = return rPr
285
284
286−extraRunStyleInfo :: PandocMonad m => RunStyle -> DocxContext m (Inlines -> Inlines)
287−extraRunStyleInfo rPr
288− | Just (s, _) <- rStyle rPr = do
289− already <- asks docxCustomStyleAlready
290− opts <- asks docxOptions
291− return $ if isEnabled Ext_styles opts && not already
292− then spanWith ("", [], [("custom-style", s)])
293− else id
294− | otherwise = return id
295−
296runStyleToTransform :: PandocMonad m => RunStyle -> DocxContext m (Inlines -> Inlines)
285runStyleToTransform :: PandocMonad m => RunStyle -> DocxContext m (Inlines -> Inlines)
297runStyleToTransform rPr
286runStyleToTransform rPr
298 | Just (s, _) <- rStyle rPr
287 | Just (s, _) <- rStyle rPr
@@ -300,42 +289,35 @@ runStyleToTransform rPr
300 let rPr' = rPr{rStyle = Nothing}
289 let rPr' = rPr{rStyle = Nothing}
301 transform <- runStyleToTransform rPr'
290 transform <- runStyleToTransform rPr'
302 return $ spanWith ("", [s], []) . transform
291 return $ spanWith ("", [s], []) . transform
292+ | Just (s, _) <- rStyle rPr = do
293+ opts <- asks docxOptions
294+ let extraInfo = if isEnabled Ext_styles opts
295+ then spanWith ("", [], [("custom-style", s)])
296+ else id
297+ transform <- runStyleToTransform rPr {rStyle = Nothing}
298+ return $ extraInfo . transform
303 | Just True <- isItalic rPr = do
299 | Just True <- isItalic rPr = do
304− extraInfo <- extraRunStyleInfo rPr
305− transform <- local (\e -> e{docxCustomStyleAlready = True}) $
306− runStyleToTransform rPr {isItalic = Nothing}
300+ transform <- runStyleToTransform rPr {isItalic = Nothing}
307− return $ extraInfo . emph . transform
301+ return $ emph . transform
308 | Just True <- isBold rPr = do
302 | Just True <- isBold rPr = do
309− extraInfo <- extraRunStyleInfo rPr
310− transform <- local (\e -> e{docxCustomStyleAlready = True}) $
311− runStyleToTransform rPr {isBold = Nothing}
303+ transform <- runStyleToTransform rPr {isBold = Nothing}
312− return $ extraInfo . strong . transform
304+ return $ strong . transform
313 | Just True <- isSmallCaps rPr = do
305 | Just True <- isSmallCaps rPr = do
314− extraInfo <- extraRunStyleInfo rPr
315− transform <- local (\e -> e{docxCustomStyleAlready = True}) $
316− runStyleToTransform rPr {isSmallCaps = Nothing}
306+ transform <- runStyleToTransform rPr {isSmallCaps = Nothing}
317− return $ extraInfo . smallcaps . transform
307+ return $ smallcaps . transform
318 | Just True <- isStrike rPr = do
308 | Just True <- isStrike rPr = do
319− extraInfo <- extraRunStyleInfo rPr
320− transform <- local (\e -> e{docxCustomStyleAlready = True}) $
321− runStyleToTransform rPr {isStrike = Nothing}
309+ transform <- runStyleToTransform rPr {isStrike = Nothing}
322− return $ extraInfo . strikeout . transform
310+ return $ strikeout . transform
323 | Just SupScrpt <- rVertAlign rPr = do
311 | Just SupScrpt <- rVertAlign rPr = do
324− extraInfo <- extraRunStyleInfo rPr
325− transform <- local (\e -> e{docxCustomStyleAlready = True}) $
326− runStyleToTransform rPr {rVertAlign = Nothing}
312+ transform <- runStyleToTransform rPr {rVertAlign = Nothing}
327− return $ extraInfo . superscript . transform
313+ return $ superscript . transform
328 | Just SubScrpt <- rVertAlign rPr = do
314 | Just SubScrpt <- rVertAlign rPr = do
329− extraInfo <- extraRunStyleInfo rPr
330− transform <- local (\e -> e{docxCustomStyleAlready = True}) $
331− runStyleToTransform rPr {rVertAlign = Nothing}
315+ transform <- runStyleToTransform rPr {rVertAlign = Nothing}
332− return $ extraInfo . subscript . transform
316+ return $ subscript . transform
333 | Just "single" <- rUnderline rPr = do
317 | Just "single" <- rUnderline rPr = do
334− extraInfo <- extraRunStyleInfo rPr
335− transform <- local (\e -> e{docxCustomStyleAlready = True}) $
336− runStyleToTransform rPr {rUnderline = Nothing}
318+ transform <- runStyleToTransform rPr {rUnderline = Nothing}
337− return $ extraInfo . underlineSpan . transform
319+ return $ underlineSpan . transform
338− | otherwise = extraRunStyleInfo rPr
320+ | otherwise = return id
339
321
340runToInlines :: PandocMonad m => Run -> DocxContext m Inlines
322runToInlines :: PandocMonad m => Run -> DocxContext m Inlines
341runToInlines (Run rs runElems)
323runToInlines (Run rs runElems)