From 9c7f75afb5d4e50ac915213eba0470c2d0ad304d Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 7 Nov 2019 10:48:38 -0800 Subject: [PATCH] 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`). --- MANUAL.txt | 6 +++--- cabal.project | 4 ++++ src/Text/Pandoc/App.hs | 12 ++++-------- src/Text/Pandoc/App/CommandLineOptions.hs | 2 +- src/Text/Pandoc/Readers/Vimwiki.hs | 4 +++- stack.yaml | 4 +++- test/command/multiple-metadata-blocks.md | 2 +- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/MANUAL.txt b/MANUAL.txt index 2e37d62a0..207f5a5ae 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -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` diff --git a/cabal.project b/cabal.project index 02610d598..d780a5d48 100644 --- a/cabal.project +++ b/cabal.project @@ -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 diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs index 79346855f..0d34eca11 100644 --- a/src/Text/Pandoc/App.hs +++ b/src/Text/Pandoc/App.hs @@ -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) diff --git a/src/Text/Pandoc/App/CommandLineOptions.hs b/src/Text/Pandoc/App/CommandLineOptions.hs index 7692cfac9..9f1905741 100644 --- a/src/Text/Pandoc/App/CommandLineOptions.hs +++ b/src/Text/Pandoc/App/CommandLineOptions.hs @@ -155,7 +155,7 @@ options = , Option "" ["metadata-file"] (ReqArg (\arg opt -> return opt{ optMetadataFiles = - normalizePath arg : optMetadataFiles opt }) + optMetadataFiles opt ++ [normalizePath arg] }) "FILE") "" diff --git a/src/Text/Pandoc/Readers/Vimwiki.hs b/src/Text/Pandoc/Readers/Vimwiki.hs index 57fa6fa1a..27b7d7245 100644 --- a/src/Text/Pandoc/Readers/Vimwiki.hs +++ b/src/Text/Pandoc/Readers/Vimwiki.hs @@ -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 $ diff --git a/stack.yaml b/stack.yaml index e85d101f6..0989fbed1 100644 --- a/stack.yaml +++ b/stack.yaml @@ -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 diff --git a/test/command/multiple-metadata-blocks.md b/test/command/multiple-metadata-blocks.md index 27c5e3dae..5adbfe423 100644 --- a/test/command/multiple-metadata-blocks.md +++ b/test/command/multiple-metadata-blocks.md @@ -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"])]}) [] ```