From fbb9fbf9bb0bf5e52211ffd9b67a2be7c29edce7 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Fri, 21 Jan 2022 17:59:14 +0100 Subject: [PATCH] Custom writer: preserve order of element attributes Attribute key-value pairs are marshaled as AttributeList, i.e., as a userdata type that behaves both like a list and a map. This allows to preserve the order of key-value pairs. Closes: #7489 --- src/Text/Pandoc/Writers/Custom.hs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Text/Pandoc/Writers/Custom.hs b/src/Text/Pandoc/Writers/Custom.hs index 70c03a016..52ae33c35 100644 --- a/src/Text/Pandoc/Writers/Custom.hs +++ b/src/Text/Pandoc/Writers/Custom.hs @@ -21,7 +21,6 @@ import Control.Arrow ((***)) import Control.Exception import Control.Monad (when) import Data.List (intersperse) -import qualified Data.Map as M import Data.Maybe (fromMaybe) import qualified Data.Text as T import Data.Text (Text, pack) @@ -32,13 +31,20 @@ import Text.DocTemplates (Context) import Control.Monad.IO.Class (MonadIO) import Text.Pandoc.Definition import Text.Pandoc.Lua (Global (..), runLua, setGlobals) +import Text.Pandoc.Lua.Marshal.Attr (pushAttributeList) import Text.Pandoc.Options import Text.Pandoc.Class (PandocMonad) import Text.Pandoc.Templates (renderTemplate) import Text.Pandoc.Writers.Shared -attrToMap :: Attr -> M.Map T.Text T.Text -attrToMap (id',classes,keyvals) = M.fromList +-- | List of key-value pairs that is pushed to Lua as AttributeList +-- userdata. +newtype AttributeList = AttributeList [(Text, Text)] +instance Pushable AttributeList where + push (AttributeList kvs) = pushAttributeList kvs + +attrToMap :: Attr -> AttributeList +attrToMap (id',classes,keyvals) = AttributeList $ ("id", id') : ("class", T.unwords classes) : keyvals