git.iko.soy/typeable/pandoc
on master help sign in
introduce --metadata-file option
6aa5fcamb21
MANUAL.txt+13−1
@@ -578,6 +578,16 @@ Reader options
578 printed in some output formats) and metadata values will be escaped
578 printed in some output formats) and metadata values will be escaped
579 when inserted into the template.
579 when inserted into the template.
580
580
581+`--metadata-file=`*FILE*
582+
583+: Read metadata from the supplied YAML (or JSON) file.
584+ This option can be used with every input format, but string
585+ scalars in the YAML file will always be parsed as Markdown.
586+ Generally, the input will be handled the same as in
587+ [YAML metadata blocks][Extension: `yaml_metadata_block`].
588+ Metadata values specified inside the document, or by using `-M`,
589+ overwrite values specified with this option.
590+
581`-p`, `--preserve-tabs`
591`-p`, `--preserve-tabs`
582
592
583: Preserve tabs instead of converting them to spaces (the default).
593: Preserve tabs instead of converting them to spaces (the default).
@@ -3061,7 +3071,9 @@ and pass it to pandoc as an argument, along with your Markdown files:
3061 pandoc chap1.md chap2.md chap3.md metadata.yaml -s -o book.html
3071 pandoc chap1.md chap2.md chap3.md metadata.yaml -s -o book.html
3062
3072
3063Just be sure that the YAML file begins with `---` and ends with `---` or
3073Just be sure that the YAML file begins with `---` and ends with `---` or
3064−`...`.)
3074+`...`.) Alternatively, you can use the `--metadata-file` option. Using
3075+that approach however, you cannot reference content (like footnotes)
3076+from the main markdown input document.
3065
3077
3066Metadata will be taken from the fields of the YAML object and added to any
3078Metadata will be taken from the fields of the YAML object and added to any
3067existing document metadata. Metadata can contain lists and objects (nested
3079existing document metadata. Metadata can contain lists and objects (nested
src/Text/Pandoc/App.hs+17
@@ -89,6 +89,7 @@ import Text.Pandoc.Builder (setMeta, deleteMeta)
89import Text.Pandoc.Filter (Filter (JSONFilter, LuaFilter), applyFilters)
89import Text.Pandoc.Filter (Filter (JSONFilter, LuaFilter), applyFilters)
90import Text.Pandoc.Highlighting (highlightingStyles)
90import Text.Pandoc.Highlighting (highlightingStyles)
91import Text.Pandoc.PDF (makePDF)
91import Text.Pandoc.PDF (makePDF)
92+import Text.Pandoc.Readers.Markdown (yamlToMeta)
92import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
93import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
93import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
94import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
94 headerShift, isURI, ordNub, safeRead, tabFilter, uriPathToPath)
95 headerShift, isURI, ordNub, safeRead, tabFilter, uriPathToPath)
@@ -399,6 +400,10 @@ convertWithOpts opts = do
399 ("application/xml", jatsCSL)
400 ("application/xml", jatsCSL)
400 return $ ("csl", jatsEncoded) : optMetadata opts
401 return $ ("csl", jatsEncoded) : optMetadata opts
401 else return $ optMetadata opts
402 else return $ optMetadata opts
403+ metadataFromFile <-
404+ case optMetadataFile opts of
405+ Nothing -> return mempty
406+ Just file -> readFileLazy file >>= yamlToMeta
402
407
403 case lookup "lang" (optMetadata opts) of
408 case lookup "lang" (optMetadata opts) of
404 Just l -> case parseBCP47 l of
409 Just l -> case parseBCP47 l of
@@ -491,6 +496,7 @@ convertWithOpts opts = do
491 ( (if isJust (optExtractMedia opts)
496 ( (if isJust (optExtractMedia opts)
492 then fillMediaBag
497 then fillMediaBag
493 else return)
498 else return)
499+ >=> return . addNonPresentMetadata metadataFromFile
494 >=> return . addMetadata metadata
500 >=> return . addMetadata metadata
495 >=> applyTransforms transforms
501 >=> applyTransforms transforms
496 >=> applyFilters readerOpts filters' [format]
502 >=> applyFilters readerOpts filters' [format]
@@ -556,6 +562,7 @@ data Opt = Opt
556 , optTemplate :: Maybe FilePath -- ^ Custom template
562 , optTemplate :: Maybe FilePath -- ^ Custom template
557 , optVariables :: [(String,String)] -- ^ Template variables to set
563 , optVariables :: [(String,String)] -- ^ Template variables to set
558 , optMetadata :: [(String, String)] -- ^ Metadata fields to set
564 , optMetadata :: [(String, String)] -- ^ Metadata fields to set
565+ , optMetadataFile :: Maybe FilePath -- ^ Name of YAML metadata file
559 , optOutputFile :: Maybe FilePath -- ^ Name of output file
566 , optOutputFile :: Maybe FilePath -- ^ Name of output file
560 , optInputFiles :: [FilePath] -- ^ Names of input files
567 , optInputFiles :: [FilePath] -- ^ Names of input files
561 , optNumberSections :: Bool -- ^ Number sections in LaTeX
568 , optNumberSections :: Bool -- ^ Number sections in LaTeX
@@ -628,6 +635,7 @@ defaultOpts = Opt
628 , optTemplate = Nothing
635 , optTemplate = Nothing
629 , optVariables = []
636 , optVariables = []
630 , optMetadata = []
637 , optMetadata = []
638+ , optMetadataFile = Nothing
631 , optOutputFile = Nothing
639 , optOutputFile = Nothing
632 , optInputFiles = []
640 , optInputFiles = []
633 , optNumberSections = False
641 , optNumberSections = False
@@ -687,6 +695,9 @@ defaultOpts = Opt
687 , optStripComments = False
695 , optStripComments = False
688 }
696 }
689
697
698+addNonPresentMetadata :: Text.Pandoc.Meta -> Pandoc -> Pandoc
699+addNonPresentMetadata newmeta (Pandoc meta bs) = Pandoc (meta <> newmeta) bs
700+
690addMetadata :: [(String, String)] -> Pandoc -> Pandoc
701addMetadata :: [(String, String)] -> Pandoc -> Pandoc
691addMetadata kvs pdc = foldr addMeta (removeMetaKeys kvs pdc) kvs
702addMetadata kvs pdc = foldr addMeta (removeMetaKeys kvs pdc) kvs
692
703
@@ -963,6 +974,12 @@ options =
963 "KEY[:VALUE]")
974 "KEY[:VALUE]")
964 ""
975 ""
965
976
977+ , Option "" ["metadata-file"]
978+ (ReqArg
979+ (\arg opt -> return opt{ optMetadataFile = Just arg })
980+ "FILE")
981+ ""
982+
966 , Option "V" ["variable"]
983 , Option "V" ["variable"]
967 (ReqArg
984 (ReqArg
968 (\arg opt -> do
985 (\arg opt -> do
src/Text/Pandoc/Readers/Markdown.hs+14−2
@@ -31,7 +31,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31
31
32Conversion of markdown-formatted plain text to 'Pandoc' document.
32Conversion of markdown-formatted plain text to 'Pandoc' document.
33-}
33-}
34−module Text.Pandoc.Readers.Markdown ( readMarkdown ) where
34+module Text.Pandoc.Readers.Markdown ( readMarkdown, yamlToMeta ) where
35
35
36import Prelude
36import Prelude
37import Control.Monad
37import Control.Monad
@@ -246,11 +246,23 @@ yamlMetaBlock = try $ do
246 updateState $ \st -> st{ stateMeta' = (stateMeta' st) <> newMetaF }
246 updateState $ \st -> st{ stateMeta' = (stateMeta' st) <> newMetaF }
247 return mempty
247 return mempty
248
248
249+-- | Read a YAML string and convert it to pandoc metadata.
250+-- String scalars in the YAML are parsed as Markdown.
251+yamlToMeta :: PandocMonad m => BS.ByteString -> m Meta
252+yamlToMeta bstr = do
253+ let parser = do
254+ meta <- yamlBsToMeta bstr
255+ return $ runF meta defaultParserState
256+ parsed <- readWithM parser def ""
257+ case parsed of
258+ Right result -> return result
259+ Left e -> throwError e
260+
249yamlBsToMeta :: PandocMonad m => BS.ByteString -> MarkdownParser m (F Meta)
261yamlBsToMeta :: PandocMonad m => BS.ByteString -> MarkdownParser m (F Meta)
250yamlBsToMeta bstr = do
262yamlBsToMeta bstr = do
251 pos <- getPosition
263 pos <- getPosition
252 case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of
264 case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of
253− Right [YAML.Doc (YAML.Mapping _ o)] -> (fmap Meta) <$> yamlMap o
265+ Right ((YAML.Doc (YAML.Mapping _ o)):_) -> (fmap Meta) <$> yamlMap o
254 Right [] -> return . return $ mempty
266 Right [] -> return . return $ mempty
255 Right [YAML.Doc (YAML.Scalar YAML.SNull)] -> return . return $ mempty
267 Right [YAML.Doc (YAML.Scalar YAML.SNull)] -> return . return $ mempty
256 Right _ -> do
268 Right _ -> do