Change merge behavior for metadata.

Previously, if a document contained two YAML metadata blocks
that set the same field, the conflict would be resolved in favor
of the first. Now it is resolved in favor of the second (due to
a change in pandoc-types).

This makes the behavior more uniform with other things in pandoc
(such as reference links and `--metadata-file`).
This commit is contained in:
John MacFarlane 2019-11-07 10:48:38 -08:00
parent e299212bf7
commit 9c7f75afb5
7 changed files with 19 additions and 15 deletions

View file

@ -3883,9 +3883,9 @@ given a role by external processors.) Field names must not be
interpretable as YAML numbers or boolean values (so, for
example, `yes`, `True`, and `15` cannot be used as field names).
A document may contain multiple metadata blocks. The metadata fields will
be combined through a *left-biased union*: if two metadata blocks attempt
to set the same field, the value from the first block will be taken.
A document may contain multiple metadata blocks. If two
metadata blocks attempt to set the same field, the value from
the second block will be taken.
When pandoc is used with `-t markdown` to create a Markdown document,
a YAML metadata block will be produced only if the `-s/--standalone`

View file

@ -13,3 +13,7 @@ source-repository-package
location: https://github.com/jgm/pandoc-citeproc
tag: 0.16.3.1
source-repository-package
type: git
location: git: https://github.com/jgm/pandoc-types
tag: 00f7bb79e79d7cfd3523880dbc64ba3ea46c3da2

View file

@ -230,12 +230,8 @@ convertWithOpts opts = do
metadataFromFile <-
case optMetadataFiles opts of
[] -> return mempty
paths -> mapM readFileLazy paths >>= mapM (yamlToMeta readerOpts)
>>= return . (foldr1 (<>))
-- Note: this list is in reverse order from the order on the
-- command line. So this code ensures that metadata files
-- specified later in the command line take precedence over
-- those specified earlier.
paths -> mapM readFileLazy paths >>=
fmap mconcat . mapM (yamlToMeta readerOpts)
let transforms = (case optShiftHeadingLevelBy opts of
0 -> id
@ -286,8 +282,8 @@ convertWithOpts opts = do
( (if isJust (optExtractMedia opts)
then fillMediaBag
else return)
>=> return . adjustMetadata (<> metadataFromFile)
>=> return . adjustMetadata (metadata <>)
>=> return . adjustMetadata (metadataFromFile <>)
>=> return . adjustMetadata (<> metadata)
>=> applyTransforms transforms
>=> applyFilters readerOpts filters' [format]
>=> maybe return extractMedia (optExtractMedia opts)

View file

@ -155,7 +155,7 @@ options =
, Option "" ["metadata-file"]
(ReqArg
(\arg opt -> return opt{ optMetadataFiles =
normalizePath arg : optMetadataFiles opt })
optMetadataFiles opt ++ [normalizePath arg] })
"FILE")
""

View file

@ -427,7 +427,9 @@ ph s = try $ do
contents <- trimInlines . mconcat <$> manyTill inline (lookAhead newline)
--use lookAhead because of placeholder in the whitespace parser
let meta' = B.setMeta s contents nullMeta
updateState $ \st -> st { stateMeta = stateMeta st <> meta' }
-- this order ensures that later values will be ignored in favor
-- of earlier ones:
updateState $ \st -> st { stateMeta = meta' <> stateMeta st }
noHtmlPh :: PandocMonad m => VwParser m ()
noHtmlPh = try $

View file

@ -13,7 +13,7 @@ packages:
- '.'
extra-deps:
- pandoc-citeproc-0.16.3.1
- pandoc-types-1.17.6.1
#- pandoc-types-1.17.6.1
- texmath-0.11.3
- haddock-library-1.8.0
- skylighting-0.8.2.3
@ -23,6 +23,8 @@ extra-deps:
- HsYAML-0.2.0.0
- HsYAML-aeson-0.2.0.0
- doctemplates-0.7
- git: https://github.com/jgm/pandoc-types
commit: 00f7bb79e79d7cfd3523880dbc64ba3ea46c3da2
ghc-options:
"$locals": -fhide-source-paths -Wno-missing-home-modules
resolver: lts-14.6

View file

@ -10,6 +10,6 @@ foo: bar
foo: bim
...
^D
Pandoc (Meta {unMeta = fromList [("foo",MetaInlines [Str "bar"])]})
Pandoc (Meta {unMeta = fromList [("foo",MetaInlines [Str "bim"])]})
[]
```