From 32afe857549f7f83c34fb3759820c4690f8ea380 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 17 Sep 2013 21:04:27 -0700 Subject: [PATCH] Allow `--metadata` to be repeated for the same key to form a list. This also has the effect that `--bibliography` can be repeated, as before. --- pandoc.cabal | 1 + pandoc.hs | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/pandoc.cabal b/pandoc.cabal index dc933fdb1..ace9a5afe 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -345,6 +345,7 @@ Executable pandoc extensible-exceptions >= 0.1 && < 0.2, highlighting-kate >= 0.5.5 && < 0.6, aeson >= 0.6 && < 0.7, + containers >= 0.1 && < 0.6, HTTP >= 4000.0.5 && < 4000.3 Ghc-Options: -rtsopts -with-rtsopts=-K16m -Wall -fno-warn-unused-do-bind Ghc-Prof-Options: -auto-all -caf-all -rtsopts -with-rtsopts=-K16m diff --git a/pandoc.hs b/pandoc.hs index ad01ad2ca..1ee550940 100644 --- a/pandoc.hs +++ b/pandoc.hs @@ -60,6 +60,7 @@ import Network.URI (parseURI, isURI, URI(..)) import qualified Data.ByteString.Lazy as B import qualified Data.ByteString as BS import Data.Aeson (eitherDecode', encode) +import qualified Data.Map as M copyrightMessage :: String copyrightMessage = "\nCopyright (C) 2006-2013 John MacFarlane\n" ++ @@ -113,7 +114,7 @@ data Opt = Opt , optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply , optTemplate :: Maybe FilePath -- ^ Custom template , optVariables :: [(String,String)] -- ^ Template variables to set - , optMetadata :: [(String,MetaValue)] -- ^ Metadata fields to set + , optMetadata :: M.Map String MetaValue -- ^ Metadata fields to set , optOutputFile :: String -- ^ Name of output file , optNumberSections :: Bool -- ^ Number sections in LaTeX , optNumberOffset :: [Int] -- ^ Starting number for sections @@ -168,7 +169,7 @@ defaultOpts = Opt , optTransforms = [] , optTemplate = Nothing , optVariables = [] - , optMetadata = [] + , optMetadata = M.empty , optOutputFile = "-" -- "-" means stdout , optNumberSections = False , optNumberOffset = [0,0,0,0,0,0] @@ -329,7 +330,8 @@ options = let (key,val) = case break (`elem` ":=") arg of (k,_:v) -> (k, MetaString v) (k,_) -> (k, MetaBool True) - return opt{ optMetadata = (key,val) : optMetadata opt }) + return opt{ optMetadata = addMetadata key val + $ optMetadata opt }) "KEY[:VALUE]") "" @@ -656,27 +658,29 @@ options = , Option "" ["bibliography"] (ReqArg - (\arg opt -> - return opt{ optMetadata = ("bibliography",MetaString arg) : - optMetadata opt - }) + (\arg opt -> return opt{ optMetadata = addMetadata + "bibliography" (MetaString arg) + $ optMetadata opt + }) "FILE") "" , Option "" ["csl"] (ReqArg (\arg opt -> - return opt{ optMetadata = ("csl", MetaString arg) : - optMetadata opt }) + return opt{ optMetadata = addMetadata "csl" + (MetaString arg) + $ optMetadata opt }) "FILE") "" , Option "" ["citation-abbreviations"] (ReqArg (\arg opt -> - return opt{ optMetadata = ("citation-abbreviations", - MetaString arg) : - optMetadata opt }) + return opt{ optMetadata = addMetadata + "citation-abbreviations" + (MetaString arg) + $ optMetadata opt }) "FILE") "" @@ -776,6 +780,14 @@ options = ] +addMetadata :: String -> MetaValue -> M.Map String MetaValue + -> M.Map String MetaValue +addMetadata k v m = case M.lookup k m of + Nothing -> M.insert k v m + Just (MetaList xs) -> M.insert k + (MetaList (xs ++ [v])) m + Just x -> M.insert k (MetaList [v, x]) m + -- Returns usage message usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String usageMessage programName = usageInfo @@ -933,7 +945,7 @@ main = do exitWith ExitSuccess -- --bibliography implies -F pandoc-citeproc for backwards compatibility: - let filters' = case lookup "bibliography" metadata of + let filters' = case M.lookup "bibliography" metadata of Just _ | all (\f -> takeBaseName f /= "pandoc-citeproc") filters -> "pandoc-citeproc" : filters _ -> filters @@ -1109,7 +1121,7 @@ main = do reader readerOpts - let doc0 = foldr (\(k,v) -> setMeta k v) doc metadata + let doc0 = M.foldWithKey setMeta doc metadata let doc1 = foldr ($) doc0 transforms doc2 <- foldrM ($) doc1 $ map ($ [writerName']) plugins