git.iko.soy/typeable/pandoc
on master help sign in
Added `--metadata/-M` option.
53f6101John MacFarlane
pandoc.cabal+1
@@ -334,6 +334,7 @@ Library
334
334
335Executable pandoc
335Executable pandoc
336 Build-Depends: pandoc,
336 Build-Depends: pandoc,
337+ pandoc-types >= 1.12 && < 1.13,
337 base >= 4.2 && <5,
338 base >= 4.2 && <5,
338 directory >= 1 && < 1.3,
339 directory >= 1 && < 1.3,
339 filepath >= 1.1 && < 1.4,
340 filepath >= 1.1 && < 1.4,
pandoc.hs+23−7
@@ -31,6 +31,7 @@ writers.
31-}
31-}
32module Main where
32module Main where
33import Text.Pandoc
33import Text.Pandoc
34+import Text.Pandoc.Builder (setMeta)
34import Text.Pandoc.PDF (makePDF)
35import Text.Pandoc.PDF (makePDF)
35import Text.Pandoc.Readers.LaTeX (handleIncludes)
36import Text.Pandoc.Readers.LaTeX (handleIncludes)
36import Text.Pandoc.Shared ( tabFilter, readDataFileUTF8, readDataFile,
37import Text.Pandoc.Shared ( tabFilter, readDataFileUTF8, readDataFile,
@@ -112,6 +113,7 @@ data Opt = Opt
112 , optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply
113 , optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply
113 , optTemplate :: Maybe FilePath -- ^ Custom template
114 , optTemplate :: Maybe FilePath -- ^ Custom template
114 , optVariables :: [(String,String)] -- ^ Template variables to set
115 , optVariables :: [(String,String)] -- ^ Template variables to set
116+ , optMetadata :: [(String,String)] -- ^ Metadata fields to set
115 , optOutputFile :: String -- ^ Name of output file
117 , optOutputFile :: String -- ^ Name of output file
116 , optNumberSections :: Bool -- ^ Number sections in LaTeX
118 , optNumberSections :: Bool -- ^ Number sections in LaTeX
117 , optNumberOffset :: [Int] -- ^ Starting number for sections
119 , optNumberOffset :: [Int] -- ^ Starting number for sections
@@ -166,6 +168,7 @@ defaultOpts = Opt
166 , optTransforms = []
168 , optTransforms = []
167 , optTemplate = Nothing
169 , optTemplate = Nothing
168 , optVariables = []
170 , optVariables = []
171+ , optMetadata = []
169 , optOutputFile = "-" -- "-" means stdout
172 , optOutputFile = "-" -- "-" means stdout
170 , optNumberSections = False
173 , optNumberSections = False
171 , optNumberOffset = [0,0,0,0,0,0]
174 , optNumberOffset = [0,0,0,0,0,0]
@@ -321,6 +324,16 @@ options =
321 "FILENAME")
324 "FILENAME")
322 "" -- "Use custom template"
325 "" -- "Use custom template"
323
326
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+
324 , Option "V" ["variable"]
337 , Option "V" ["variable"]
325 (ReqArg
338 (ReqArg
326 (\arg opt -> do
339 (\arg opt -> do
@@ -329,7 +342,7 @@ options =
329 (k,_) -> (k,"true")
342 (k,_) -> (k,"true")
330 return opt{ optVariables = (key,val) : optVariables opt })
343 return opt{ optVariables = (key,val) : optVariables opt })
331 "KEY[:VALUE]")
344 "KEY[:VALUE]")
332− "" -- "Use custom template"
345+ ""
333
346
334 , Option "D" ["print-default-template"]
347 , Option "D" ["print-default-template"]
335 (ReqArg
348 (ReqArg
@@ -844,6 +857,7 @@ main = do
844 , optWriter = writerName
857 , optWriter = writerName
845 , optParseRaw = parseRaw
858 , optParseRaw = parseRaw
846 , optVariables = variables
859 , optVariables = variables
860+ , optMetadata = metadata
847 , optTableOfContents = toc
861 , optTableOfContents = toc
848 , optTransforms = transforms
862 , optTransforms = transforms
849 , optTemplate = templatePath
863 , optTemplate = templatePath
@@ -1062,8 +1076,10 @@ main = do
1062 handleIncludes' . convertTabs . intercalate "\n" >>=
1076 handleIncludes' . convertTabs . intercalate "\n" >>=
1063 reader readerOpts
1077 reader readerOpts
1064
1078
1079+
1065− let doc0 = foldr ($) doc transforms
1080+ let doc0 = foldr (\(k,v) -> setMeta k (MetaString v)) doc metadata
1081+ let doc1 = foldr ($) doc0 transforms
1066− doc1 <- foldrM ($) doc0 $ map ($ [writerName']) plugins
1082+ doc2 <- foldrM ($) doc1 $ map ($ [writerName']) plugins
1067
1083
1068 let writeBinary :: B.ByteString -> IO ()
1084 let writeBinary :: B.ByteString -> IO ()
1069 writeBinary = B.writeFile (UTF8.encodePath outputFile)
1085 writeBinary = B.writeFile (UTF8.encodePath outputFile)
@@ -1074,15 +1090,15 @@ main = do
1074
1090
1075 case getWriter writerName' of
1091 case getWriter writerName' of
1076 Left e -> err 9 e
1092 Left e -> err 9 e
1077− Right (IOStringWriter f) -> f writerOptions doc1 >>= writerFn outputFile
1093+ Right (IOStringWriter f) -> f writerOptions doc2 >>= writerFn outputFile
1078− Right (IOByteStringWriter f) -> f writerOptions doc1 >>= writeBinary
1094+ Right (IOByteStringWriter f) -> f writerOptions doc2 >>= writeBinary
1079 Right (PureStringWriter f)
1095 Right (PureStringWriter f)
1080 | pdfOutput -> do
1096 | pdfOutput -> do
1081− res <- makePDF latexEngine f writerOptions doc1
1097+ res <- makePDF latexEngine f writerOptions doc2
1082 case res of
1098 case res of
1083 Right pdf -> writeBinary pdf
1099 Right pdf -> writeBinary pdf
1084 Left err' -> err 43 $ UTF8.toStringLazy err'
1100 Left err' -> err 43 $ UTF8.toStringLazy err'
1085− | otherwise -> selfcontain (f writerOptions doc1 ++
1101+ | otherwise -> selfcontain (f writerOptions doc2 ++
1086 ['\n' | not standalone'])
1102 ['\n' | not standalone'])
1087 >>= writerFn outputFile . handleEntities
1103 >>= writerFn outputFile . handleEntities
1088 where htmlFormat = writerName' `elem`
1104 where htmlFormat = writerName' `elem`