From 712d746320d2f2bd1ee345dfa903b8d20d359cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edwin=20T=C3=B6r=C3=B6k?= Date: Tue, 19 Dec 2023 20:13:41 +0000 Subject: fix(docx): sort inline elements in schema order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #9273 ``` [ { "Description": "The element has unexpected child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:b'.", "Path": { "NamespacesDefinitions": [ "xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"" ], "Namespaces": { }, "XPath": "/w:document[1]/w:body[1]/w:p[1]/w:r[7]/w:rPr[1]", "PartUri": "/word/document.xml" }, "Id": "Sch_UnexpectedElementContentExpectingComplex", "ErrorType": "Schema" } ] ``` Signed-off-by: Edwin Török --- src/Text/Pandoc/Writers/Docx.hs | 61 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Writers/Docx.hs b/src/Text/Pandoc/Writers/Docx.hs index 44c0e900d..afafd6926 100644 --- a/src/Text/Pandoc/Writers/Docx.hs +++ b/src/Text/Pandoc/Writers/Docx.hs @@ -36,7 +36,8 @@ import Control.Monad.State.Strict ( StateT(runStateT), gets, modify ) import qualified Data.ByteString.Lazy as BL import Data.Containers.ListUtils (nubOrd) import Data.Char (isSpace, isLetter) -import Data.List (intercalate, isPrefixOf, isSuffixOf) +import Data.List (intercalate, isPrefixOf, isSuffixOf, sortBy) +import Data.Ord (comparing) import Data.String (fromString) import qualified Data.Map as M import Data.Maybe (fromMaybe, isNothing, mapMaybe, maybeToList, isJust) @@ -77,9 +78,63 @@ import Text.Pandoc.XML.Light as XML import Data.Generics (mkT, everywhere) import Text.Collate.Lang (renderLang, Lang(..)) +-- from wml.xsd EG_RPrBase +rPrTagOrder :: M.Map Text Int +rPrTagOrder = + M.fromList + (zip [ "rStyle" + , "rFonts" + , "b" + , "bCs" + , "i" + , "iCs" + , "caps" + , "smallCaps" + , "strike" + , "dstrike" + , "outline" + , "shadow" + , "emboss" + , "imprint" + , "noProof" + , "snapToGrid" + , "vanish" + , "webHidden" + , "color" + , "spacing" + , "w" + , "kern" + , "position" + , "sz" + , "szCs" + , "highlight" + , "u" + , "effect" + , "bdr" + , "shd" + , "fitText" + , "vertAlign" + , "rtl" + , "cs" + , "em" + , "lang" + , "eastAsianLayout" + , "specVanish" + , "oMath" + ] [0..]) + +sortSquashed :: [Element] -> [Element] +sortSquashed l = + sortBy (comparing tagIndex) l + where + tagIndex :: Element -> Int + tagIndex el = + fromMaybe 0 (M.lookup tag rPrTagOrder) + where tag = (qName . elName) el + squashProps :: EnvProps -> [Element] -squashProps (EnvProps Nothing es) = es -squashProps (EnvProps (Just e) es) = e : es +squashProps (EnvProps Nothing es) = sortSquashed es +squashProps (EnvProps (Just e) es) = sortSquashed (e : es) renumIdMap :: Int -> [Element] -> M.Map Text Text renumIdMap _ [] = M.empty -- cgit v1.2.3