git.iko.soy/typeable/pandoc
on master help sign in
Added `--metadata/-M` option.

This is like `--variable/-V`, but actually adds to metadata, not just variables.

commit53f61019e27dcc14112136609a72b27e17e0eb06 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSun 1 Sep 2013, 15:37 (13 years ago) parent9b0b9b6
2 files changed+24−7
pandoc.cabal+1 pandoc.hs+23−7
pandoc.cabal+1
@@ -334,6 +334,7 @@ Library
334334
335335Executable pandoc
336336 Build-Depends: pandoc,
337+ pandoc-types >= 1.12 && < 1.13,
337338 base >= 4.2 && <5,
338339 directory >= 1 && < 1.3,
339340 filepath >= 1.1 && < 1.4,
pandoc.hs+23−7
@@ -31,6 +31,7 @@ writers.
3131-}
3232module Main where
3333import Text.Pandoc
34+import Text.Pandoc.Builder (setMeta)
3435import Text.Pandoc.PDF (makePDF)
3536import Text.Pandoc.Readers.LaTeX (handleIncludes)
3637import Text.Pandoc.Shared ( tabFilter, readDataFileUTF8, readDataFile,
@@ -112,6 +113,7 @@ data Opt = Opt
112113 , optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply
113114 , optTemplate :: Maybe FilePath -- ^ Custom template
114115 , optVariables :: [(String,String)] -- ^ Template variables to set
116+ , optMetadata :: [(String,String)] -- ^ Metadata fields to set
115117 , optOutputFile :: String -- ^ Name of output file
116118 , optNumberSections :: Bool -- ^ Number sections in LaTeX
117119 , optNumberOffset :: [Int] -- ^ Starting number for sections
@@ -166,6 +168,7 @@ defaultOpts = Opt
166168 , optTransforms = []
167169 , optTemplate = Nothing
168170 , optVariables = []
171+ , optMetadata = []
169172 , optOutputFile = "-" -- "-" means stdout
170173 , optNumberSections = False
171174 , optNumberOffset = [0,0,0,0,0,0]
@@ -321,6 +324,16 @@ options =
321324 "FILENAME")
322325 "" -- "Use custom template"
323326
327+ , Option "M" ["metadata"]
328+ (ReqArg
329+ (\arg opt -> do
330+ let (key,val) = case break (`elem` ":=") arg of
331+ (k,_:v) -> (k,v)
332+ (k,_) -> (k,"true")
333+ return opt{ optMetadata = (key,val) : optMetadata opt })
334+ "KEY[:VALUE]")
335+ ""
336+
324337 , Option "V" ["variable"]
325338 (ReqArg
326339 (\arg opt -> do
@@ -329,7 +342,7 @@ options =
329342 (k,_) -> (k,"true")
330343 return opt{ optVariables = (key,val) : optVariables opt })
331344 "KEY[:VALUE]")
332− "" -- "Use custom template"
345+ ""
333346
334347 , Option "D" ["print-default-template"]
335348 (ReqArg
@@ -844,6 +857,7 @@ main = do
844857 , optWriter = writerName
845858 , optParseRaw = parseRaw
846859 , optVariables = variables
860+ , optMetadata = metadata
847861 , optTableOfContents = toc
848862 , optTransforms = transforms
849863 , optTemplate = templatePath
@@ -1062,8 +1076,10 @@ main = do
10621076 handleIncludes' . convertTabs . intercalate "\n" >>=
10631077 reader readerOpts
10641078
1065− let doc0 = foldr ($) doc transforms
1066− doc1 <- foldrM ($) doc0 $ map ($ [writerName']) plugins
1079+
1080+ let doc0 = foldr (\(k,v) -> setMeta k (MetaString v)) doc metadata
1081+ let doc1 = foldr ($) doc0 transforms
1082+ doc2 <- foldrM ($) doc1 $ map ($ [writerName']) plugins
10671083
10681084 let writeBinary :: B.ByteString -> IO ()
10691085 writeBinary = B.writeFile (UTF8.encodePath outputFile)
@@ -1074,15 +1090,15 @@ main = do
10741090
10751091 case getWriter writerName' of
10761092 Left e -> err 9 e
1077− Right (IOStringWriter f) -> f writerOptions doc1 >>= writerFn outputFile
1078− Right (IOByteStringWriter f) -> f writerOptions doc1 >>= writeBinary
1093+ Right (IOStringWriter f) -> f writerOptions doc2 >>= writerFn outputFile
1094+ Right (IOByteStringWriter f) -> f writerOptions doc2 >>= writeBinary
10791095 Right (PureStringWriter f)
10801096 | pdfOutput -> do
1081− res <- makePDF latexEngine f writerOptions doc1
1097+ res <- makePDF latexEngine f writerOptions doc2
10821098 case res of
10831099 Right pdf -> writeBinary pdf
10841100 Left err' -> err 43 $ UTF8.toStringLazy err'
1085− | otherwise -> selfcontain (f writerOptions doc1 ++
1101+ | otherwise -> selfcontain (f writerOptions doc2 ++
10861102 ['\n' | not standalone'])
10871103 >>= writerFn outputFile . handleEntities
10881104 where htmlFormat = writerName' `elem`