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 interpretable as YAML numbers or boolean values (so, for
example, `yes`, `True`, and `15` cannot be used as field names). example, `yes`, `True`, and `15` cannot be used as field names).
A document may contain multiple metadata blocks. The metadata fields will A document may contain multiple metadata blocks. If two
be combined through a *left-biased union*: if two metadata blocks attempt metadata blocks attempt to set the same field, the value from
to set the same field, the value from the first block will be taken. the second block will be taken.
When pandoc is used with `-t markdown` to create a Markdown document, When pandoc is used with `-t markdown` to create a Markdown document,
a YAML metadata block will be produced only if the `-s/--standalone` 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 location: https://github.com/jgm/pandoc-citeproc
tag: 0.16.3.1 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 <- metadataFromFile <-
case optMetadataFiles opts of case optMetadataFiles opts of
[] -> return mempty [] -> return mempty
paths -> mapM readFileLazy paths >>= mapM (yamlToMeta readerOpts) paths -> mapM readFileLazy paths >>=
>>= return . (foldr1 (<>)) fmap mconcat . mapM (yamlToMeta readerOpts)
-- 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.
let transforms = (case optShiftHeadingLevelBy opts of let transforms = (case optShiftHeadingLevelBy opts of
0 -> id 0 -> id
@ -286,8 +282,8 @@ convertWithOpts opts = do
( (if isJust (optExtractMedia opts) ( (if isJust (optExtractMedia opts)
then fillMediaBag then fillMediaBag
else return) else return)
>=> return . adjustMetadata (<> metadataFromFile) >=> return . adjustMetadata (metadataFromFile <>)
>=> return . adjustMetadata (metadata <>) >=> return . adjustMetadata (<> metadata)
>=> applyTransforms transforms >=> applyTransforms transforms
>=> applyFilters readerOpts filters' [format] >=> applyFilters readerOpts filters' [format]
>=> maybe return extractMedia (optExtractMedia opts) >=> maybe return extractMedia (optExtractMedia opts)

View file

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

View file

@ -427,7 +427,9 @@ ph s = try $ do
contents <- trimInlines . mconcat <$> manyTill inline (lookAhead newline) contents <- trimInlines . mconcat <$> manyTill inline (lookAhead newline)
--use lookAhead because of placeholder in the whitespace parser --use lookAhead because of placeholder in the whitespace parser
let meta' = B.setMeta s contents nullMeta 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 :: PandocMonad m => VwParser m ()
noHtmlPh = try $ noHtmlPh = try $

View file

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

View file

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