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.
This commit is contained in:
John MacFarlane 2013-09-17 21:04:27 -07:00
parent c04d243795
commit 32afe85754
2 changed files with 27 additions and 14 deletions

View file

@ -345,6 +345,7 @@ Executable pandoc
extensible-exceptions >= 0.1 && < 0.2, extensible-exceptions >= 0.1 && < 0.2,
highlighting-kate >= 0.5.5 && < 0.6, highlighting-kate >= 0.5.5 && < 0.6,
aeson >= 0.6 && < 0.7, aeson >= 0.6 && < 0.7,
containers >= 0.1 && < 0.6,
HTTP >= 4000.0.5 && < 4000.3 HTTP >= 4000.0.5 && < 4000.3
Ghc-Options: -rtsopts -with-rtsopts=-K16m -Wall -fno-warn-unused-do-bind Ghc-Options: -rtsopts -with-rtsopts=-K16m -Wall -fno-warn-unused-do-bind
Ghc-Prof-Options: -auto-all -caf-all -rtsopts -with-rtsopts=-K16m Ghc-Prof-Options: -auto-all -caf-all -rtsopts -with-rtsopts=-K16m

View file

@ -60,6 +60,7 @@ import Network.URI (parseURI, isURI, URI(..))
import qualified Data.ByteString.Lazy as B import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import Data.Aeson (eitherDecode', encode) import Data.Aeson (eitherDecode', encode)
import qualified Data.Map as M
copyrightMessage :: String copyrightMessage :: String
copyrightMessage = "\nCopyright (C) 2006-2013 John MacFarlane\n" ++ copyrightMessage = "\nCopyright (C) 2006-2013 John MacFarlane\n" ++
@ -113,7 +114,7 @@ data Opt = Opt
, optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply , optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply
, optTemplate :: Maybe FilePath -- ^ Custom template , optTemplate :: Maybe FilePath -- ^ Custom template
, optVariables :: [(String,String)] -- ^ Template variables to set , 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 , optOutputFile :: String -- ^ Name of output file
, optNumberSections :: Bool -- ^ Number sections in LaTeX , optNumberSections :: Bool -- ^ Number sections in LaTeX
, optNumberOffset :: [Int] -- ^ Starting number for sections , optNumberOffset :: [Int] -- ^ Starting number for sections
@ -168,7 +169,7 @@ defaultOpts = Opt
, optTransforms = [] , optTransforms = []
, optTemplate = Nothing , optTemplate = Nothing
, optVariables = [] , optVariables = []
, optMetadata = [] , optMetadata = M.empty
, optOutputFile = "-" -- "-" means stdout , optOutputFile = "-" -- "-" means stdout
, optNumberSections = False , optNumberSections = False
, optNumberOffset = [0,0,0,0,0,0] , optNumberOffset = [0,0,0,0,0,0]
@ -329,7 +330,8 @@ options =
let (key,val) = case break (`elem` ":=") arg of let (key,val) = case break (`elem` ":=") arg of
(k,_:v) -> (k, MetaString v) (k,_:v) -> (k, MetaString v)
(k,_) -> (k, MetaBool True) (k,_) -> (k, MetaBool True)
return opt{ optMetadata = (key,val) : optMetadata opt }) return opt{ optMetadata = addMetadata key val
$ optMetadata opt })
"KEY[:VALUE]") "KEY[:VALUE]")
"" ""
@ -656,27 +658,29 @@ options =
, Option "" ["bibliography"] , Option "" ["bibliography"]
(ReqArg (ReqArg
(\arg opt -> (\arg opt -> return opt{ optMetadata = addMetadata
return opt{ optMetadata = ("bibliography",MetaString arg) : "bibliography" (MetaString arg)
optMetadata opt $ optMetadata opt
}) })
"FILE") "FILE")
"" ""
, Option "" ["csl"] , Option "" ["csl"]
(ReqArg (ReqArg
(\arg opt -> (\arg opt ->
return opt{ optMetadata = ("csl", MetaString arg) : return opt{ optMetadata = addMetadata "csl"
optMetadata opt }) (MetaString arg)
$ optMetadata opt })
"FILE") "FILE")
"" ""
, Option "" ["citation-abbreviations"] , Option "" ["citation-abbreviations"]
(ReqArg (ReqArg
(\arg opt -> (\arg opt ->
return opt{ optMetadata = ("citation-abbreviations", return opt{ optMetadata = addMetadata
MetaString arg) : "citation-abbreviations"
optMetadata opt }) (MetaString arg)
$ optMetadata opt })
"FILE") "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 -- Returns usage message
usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String
usageMessage programName = usageInfo usageMessage programName = usageInfo
@ -933,7 +945,7 @@ main = do
exitWith ExitSuccess exitWith ExitSuccess
-- --bibliography implies -F pandoc-citeproc for backwards compatibility: -- --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") Just _ | all (\f -> takeBaseName f /= "pandoc-citeproc")
filters -> "pandoc-citeproc" : filters filters -> "pandoc-citeproc" : filters
_ -> filters _ -> filters
@ -1109,7 +1121,7 @@ main = do
reader readerOpts reader readerOpts
let doc0 = foldr (\(k,v) -> setMeta k v) doc metadata let doc0 = M.foldWithKey setMeta doc metadata
let doc1 = foldr ($) doc0 transforms let doc1 = foldr ($) doc0 transforms
doc2 <- foldrM ($) doc1 $ map ($ [writerName']) plugins doc2 <- foldrM ($) doc1 $ map ($ [writerName']) plugins