From fef5770591e7d3a185dc8d7dc2b70594732b0367 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 8 Nov 2017 21:54:23 -0800 Subject: Fix regression with --metadata. It should replace a metadata value set in the document itself, rather than creating a list including a new value. Closes #4054. --- src/Text/Pandoc/App.hs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/Text/Pandoc/App.hs') diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs index d9924d3a1..b2394e142 100644 --- a/src/Text/Pandoc/App.hs +++ b/src/Text/Pandoc/App.hs @@ -76,7 +76,7 @@ import qualified System.IO as IO (Newline (..)) import System.IO.Error (isDoesNotExistError) import Text.Pandoc import Text.Pandoc.BCP47 (Lang (..), parseBCP47) -import Text.Pandoc.Builder (setMeta) +import Text.Pandoc.Builder (setMeta, deleteMeta) import Text.Pandoc.Highlighting (highlightingStyles) import Text.Pandoc.Lua (LuaException (..), runLuaFilter) import Text.Pandoc.PDF (makePDF) @@ -494,7 +494,7 @@ convertWithOpts opts = do ( (if isJust (optExtractMedia opts) then fillMediaBag else return) - >=> return . flip (foldr addMetadata) metadata + >=> return . addMetadata metadata >=> applyLuaFilters datadir (optLuaFilters opts) format >=> maybe return extractMedia (optExtractMedia opts) >=> applyTransforms transforms @@ -722,8 +722,11 @@ defaultOpts = Opt , optStripComments = False } -addMetadata :: (String, String) -> Pandoc -> Pandoc -addMetadata (k, v) (Pandoc meta bs) = Pandoc meta' bs +addMetadata :: [(String, String)] -> Pandoc -> Pandoc +addMetadata kvs pdc = foldr addMeta (removeMetaKeys kvs pdc) kvs + +addMeta :: (String, String) -> Pandoc -> Pandoc +addMeta (k, v) (Pandoc meta bs) = Pandoc meta' bs where meta' = case lookupMeta k meta of Nothing -> setMeta k v' meta Just (MetaList xs) -> @@ -731,6 +734,9 @@ addMetadata (k, v) (Pandoc meta bs) = Pandoc meta' bs Just x -> setMeta k (MetaList [x, v']) meta v' = readMetaValue v +removeMetaKeys :: [(String,String)] -> Pandoc -> Pandoc +removeMetaKeys kvs pdc = foldr (deleteMeta . fst) pdc kvs + readMetaValue :: String -> MetaValue readMetaValue s = case decode (UTF8.fromString s) of Just (Yaml.String t) -> MetaString $ T.unpack t -- cgit v1.2.3