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
This commit is contained in:
Albert Krewinkel 2022-01-21 17:59:14 +01:00
parent 412596c30b
commit fbb9fbf9bb
No known key found for this signature in database
GPG key ID: 388DC0B21F631124

View file

@ -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