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.
This commit is contained in:
John MacFarlane 2017-11-08 21:54:23 -08:00
parent c6338fa883
commit fef5770591
2 changed files with 24 additions and 4 deletions

View file

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

14
test/command/4054.md Normal file
View file

@ -0,0 +1,14 @@
```
% pandoc -t native -s -M title=New
% Old
^D
Pandoc (Meta {unMeta = fromList [("title",MetaString "New")]})
[]
```
```
% pandoc -t native -s -M foo=1 -M foo=2
^D
Pandoc (Meta {unMeta = fromList [("foo",MetaList [MetaString "1",MetaString "2"])]})
[]
```