git.iko.soy/typeable/pandoc
on master help sign in
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.

commit32afe857549f7f83c34fb3759820c4690f8ea380 authorJohn MacFarlane <fiddlosopher@gmail.com> dateTue 17 Sep 2013, 21:04 (13 years ago) parentc04d243
2 files changed+27−14
pandoc.cabal+1 pandoc.hs+26−14
pandoc.cabal+1
@@ -345,6 +345,7 @@ Executable pandoc
345345 extensible-exceptions >= 0.1 && < 0.2,
346346 highlighting-kate >= 0.5.5 && < 0.6,
347347 aeson >= 0.6 && < 0.7,
348+ containers >= 0.1 && < 0.6,
348349 HTTP >= 4000.0.5 && < 4000.3
349350 Ghc-Options: -rtsopts -with-rtsopts=-K16m -Wall -fno-warn-unused-do-bind
350351 Ghc-Prof-Options: -auto-all -caf-all -rtsopts -with-rtsopts=-K16m
pandoc.hs+26−14
@@ -60,6 +60,7 @@ import Network.URI (parseURI, isURI, URI(..))
6060import qualified Data.ByteString.Lazy as B
6161import qualified Data.ByteString as BS
6262import Data.Aeson (eitherDecode', encode)
63+import qualified Data.Map as M
6364
6465copyrightMessage :: String
6566copyrightMessage = "\nCopyright (C) 2006-2013 John MacFarlane\n" ++
@@ -113,7 +114,7 @@ data Opt = Opt
113114 , optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply
114115 , optTemplate :: Maybe FilePath -- ^ Custom template
115116 , optVariables :: [(String,String)] -- ^ Template variables to set
116− , optMetadata :: [(String,MetaValue)] -- ^ Metadata fields to set
117+ , optMetadata :: M.Map String MetaValue -- ^ Metadata fields to set
117118 , optOutputFile :: String -- ^ Name of output file
118119 , optNumberSections :: Bool -- ^ Number sections in LaTeX
119120 , optNumberOffset :: [Int] -- ^ Starting number for sections
@@ -168,7 +169,7 @@ defaultOpts = Opt
168169 , optTransforms = []
169170 , optTemplate = Nothing
170171 , optVariables = []
171− , optMetadata = []
172+ , optMetadata = M.empty
172173 , optOutputFile = "-" -- "-" means stdout
173174 , optNumberSections = False
174175 , optNumberOffset = [0,0,0,0,0,0]
@@ -329,7 +330,8 @@ options =
329330 let (key,val) = case break (`elem` ":=") arg of
330331 (k,_:v) -> (k, MetaString v)
331332 (k,_) -> (k, MetaBool True)
332− return opt{ optMetadata = (key,val) : optMetadata opt })
333+ return opt{ optMetadata = addMetadata key val
334+ $ optMetadata opt })
333335 "KEY[:VALUE]")
334336 ""
335337
@@ -656,27 +658,29 @@ options =
656658
657659 , Option "" ["bibliography"]
658660 (ReqArg
659− (\arg opt ->
660− return opt{ optMetadata = ("bibliography",MetaString arg) :
661− optMetadata opt
662− })
661+ (\arg opt -> return opt{ optMetadata = addMetadata
662+ "bibliography" (MetaString arg)
663+ $ optMetadata opt
664+ })
663665 "FILE")
664666 ""
665667
666668 , Option "" ["csl"]
667669 (ReqArg
668670 (\arg opt ->
669− return opt{ optMetadata = ("csl", MetaString arg) :
670− optMetadata opt })
671+ return opt{ optMetadata = addMetadata "csl"
672+ (MetaString arg)
673+ $ optMetadata opt })
671674 "FILE")
672675 ""
673676
674677 , Option "" ["citation-abbreviations"]
675678 (ReqArg
676679 (\arg opt ->
677− return opt{ optMetadata = ("citation-abbreviations",
678− MetaString arg) :
679− optMetadata opt })
680+ return opt{ optMetadata = addMetadata
681+ "citation-abbreviations"
682+ (MetaString arg)
683+ $ optMetadata opt })
680684 "FILE")
681685 ""
682686
@@ -776,6 +780,14 @@ options =
776780
777781 ]
778782
783+addMetadata :: String -> MetaValue -> M.Map String MetaValue
784+ -> M.Map String MetaValue
785+addMetadata k v m = case M.lookup k m of
786+ Nothing -> M.insert k v m
787+ Just (MetaList xs) -> M.insert k
788+ (MetaList (xs ++ [v])) m
789+ Just x -> M.insert k (MetaList [v, x]) m
790+
779791-- Returns usage message
780792usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String
781793usageMessage programName = usageInfo
@@ -933,7 +945,7 @@ main = do
933945 exitWith ExitSuccess
934946
935947 -- --bibliography implies -F pandoc-citeproc for backwards compatibility:
936− let filters' = case lookup "bibliography" metadata of
948+ let filters' = case M.lookup "bibliography" metadata of
937949 Just _ | all (\f -> takeBaseName f /= "pandoc-citeproc")
938950 filters -> "pandoc-citeproc" : filters
939951 _ -> filters
@@ -1109,7 +1121,7 @@ main = do
11091121 reader readerOpts
11101122
11111123
1112− let doc0 = foldr (\(k,v) -> setMeta k v) doc metadata
1124+ let doc0 = M.foldWithKey setMeta doc metadata
11131125 let doc1 = foldr ($) doc0 transforms
11141126 doc2 <- foldrM ($) doc1 $ map ($ [writerName']) plugins
11151127