aboutsummaryrefslogtreecommitdiff
path: root/pandoc.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <fiddlosopher@gmail.com>2013-09-17 21:04:27 -0700
committerJohn MacFarlane <fiddlosopher@gmail.com>2013-09-17 21:04:27 -0700
commit32afe857549f7f83c34fb3759820c4690f8ea380 (patch)
tree58f485e25238c4f9c3768b1328e6b780589da08a /pandoc.hs
parentc04d2437953fd7de45b1c9fd6c5483f79c4885e0 (diff)
downloadpandoc-32afe857549f7f83c34fb3759820c4690f8ea380.tar.gz
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.
Diffstat (limited to 'pandoc.hs')
-rw-r--r--pandoc.hs40
1 files changed, 26 insertions, 14 deletions
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