Skip to content

Commit

Permalink
fix(docx): sort inline elements in schema order
Browse files Browse the repository at this point in the history
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 <edwin@etorok.net>
  • Loading branch information
edwintorok authored and jgm committed Dec 19, 2023
1 parent c6892be commit 712d746
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions src/Text/Pandoc/Writers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Binary file modified test/docx/golden/custom_style_preserve.docx
Binary file not shown.
Binary file modified test/docx/golden/inline_formatting.docx
Binary file not shown.

0 comments on commit 712d746

Please sign in to comment.