git.iko.soy/typeable/pandoc
on master help sign in
T.P.App.Opt: Changed optMetadata to Meta, to allow structured values.

[API change] The current behavior of the `--metadata` option stays the same.

commit5419988f225d1debcf9735f5de75a269db143bba authorJohn MacFarlane <jgm@berkeley.edu> dateWed 9 Oct 2019, 11:46 (7 years ago) parent3351dcf
4 files changed+48−52
src/Text/Pandoc/App.hs+16−38 src/Text/Pandoc/App/CommandLineOptions.hs+28−4 src/Text/Pandoc/App/Opt.hs+4−3 src/Text/Pandoc/App/OutputSettings.hs−7
src/Text/Pandoc/App.hs+16−38
@@ -50,7 +50,7 @@ import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), defaultOpts)
5050import Text.Pandoc.App.CommandLineOptions (parseOptions, options)
5151import Text.Pandoc.App.OutputSettings (OutputSettings (..), optToOutputSettings)
5252import Text.Pandoc.BCP47 (Lang (..), parseBCP47)
53−import Text.Pandoc.Builder (setMeta, deleteMeta)
53+import Text.Pandoc.Builder (setMeta)
5454import Text.Pandoc.Filter (Filter (JSONFilter, LuaFilter), applyFilters)
5555import Text.Pandoc.PDF (makePDF)
5656import Text.Pandoc.Readers.Markdown (yamlToMeta)
@@ -58,6 +58,7 @@ import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
5858import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
5959 headerShift, isURI, tabFilter, uriPathToPath, filterIpynbOutput,
6060 defaultUserDataDirs)
61+import Text.Pandoc.Writers.Shared (lookupMetaString)
6162import qualified Text.Pandoc.UTF8 as UTF8
6263#ifndef _WINDOWS
6364import System.Posix.IO (stdOutput)
@@ -79,7 +80,8 @@ convertWithOpts opts = do
7980 let isPandocCiteproc (JSONFilter f) = takeBaseName f == "pandoc-citeproc"
8081 isPandocCiteproc _ = False
8182 -- --bibliography implies -F pandoc-citeproc for backwards compatibility:
82− let needsCiteproc = isJust (lookup "bibliography" (optMetadata opts)) &&
83+ let needsCiteproc = isJust (lookupMeta "bibliography"
84+ (optMetadata opts)) &&
8385 optCiteMethod opts `notElem` [Natbib, Biblatex] &&
8486 all (not . isPandocCiteproc) filters
8587 let filters' = if needsCiteproc then JSONFilter "pandoc-citeproc" : filters
@@ -195,20 +197,21 @@ convertWithOpts opts = do
195197 Just f -> UTF8.toString <$> readFileStrict f
196198
197199 metadata <- if format == "jats" &&
198− isNothing (lookup "csl" (optMetadata opts)) &&
199− isNothing (lookup "citation-style" (optMetadata opts))
200+ isNothing (lookupMeta "csl" (optMetadata opts)) &&
201+ isNothing (lookupMeta "citation-style"
202+ (optMetadata opts))
200203 then do
201204 jatsCSL <- readDataFile "jats.csl"
202205 let jatsEncoded = makeDataURI
203206 ("application/xml", jatsCSL)
204− return $ ("csl", jatsEncoded) : optMetadata opts
207+ return $ setMeta "csl" jatsEncoded $ optMetadata opts
205208 else return $ optMetadata opts
206209
207− case lookup "lang" (optMetadata opts) of
208− Just l -> case parseBCP47 l of
209− Left _ -> return ()
210+ case lookupMetaString "lang" (optMetadata opts) of
211+ "" -> setTranslations $ Lang "en" "" "US" []
212+ l -> case parseBCP47 l of
213+ Left _ -> report $ InvalidLang l
210214 Right l' -> setTranslations l'
211− Nothing -> setTranslations $ Lang "en" "" "US" []
212215
213216 let readerOpts = def{
214217 readerStandalone = standalone
@@ -279,8 +282,8 @@ convertWithOpts opts = do
279282 ( (if isJust (optExtractMedia opts)
280283 then fillMediaBag
281284 else return)
282− >=> return . addNonPresentMetadata metadataFromFile
283− >=> return . addMetadata metadata
285+ >=> return . adjustMetadata (<> metadataFromFile)
286+ >=> return . adjustMetadata (metadata <>)
284287 >=> applyTransforms transforms
285288 >=> applyFilters readerOpts filters' [format]
286289 >=> maybe return extractMedia (optExtractMedia opts)
@@ -319,33 +322,8 @@ htmlFormat = (`elem` ["html","html4","html5","s5","slidy",
319322isTextFormat :: String -> Bool
320323isTextFormat s = s `notElem` ["odt","docx","epub2","epub3","epub","pptx"]
321324
322−addNonPresentMetadata :: Text.Pandoc.Meta -> Pandoc -> Pandoc
323−addNonPresentMetadata newmeta (Pandoc meta bs) = Pandoc (meta <> newmeta) bs
324−
325−addMetadata :: [(String, String)] -> Pandoc -> Pandoc
326−addMetadata kvs pdc = foldr addMeta (removeMetaKeys kvs pdc) kvs
327−
328−addMeta :: (String, String) -> Pandoc -> Pandoc
329−addMeta (k, v) (Pandoc meta bs) = Pandoc meta' bs
330− where meta' = case lookupMeta k meta of
331− Nothing -> setMeta k v' meta
332− Just (MetaList xs) ->
333− setMeta k (MetaList (xs ++ [v'])) meta
334− Just x -> setMeta k (MetaList [x, v']) meta
335− v' = readMetaValue v
336−
337−removeMetaKeys :: [(String,String)] -> Pandoc -> Pandoc
338−removeMetaKeys kvs pdc = foldr (deleteMeta . fst) pdc kvs
339−
340−readMetaValue :: String -> MetaValue
341−readMetaValue s
342− | s == "true" = MetaBool True
343− | s == "True" = MetaBool True
344− | s == "TRUE" = MetaBool True
345− | s == "false" = MetaBool False
346− | s == "False" = MetaBool False
347− | s == "FALSE" = MetaBool False
348− | otherwise = MetaString s
325+adjustMetadata :: (Meta -> Meta) -> Pandoc -> Pandoc
326+adjustMetadata f (Pandoc meta bs) = Pandoc (f meta) bs
349327
350328-- Transformations of a Pandoc document post-parsing:
351329
src/Text/Pandoc/App/CommandLineOptions.hs+28−4
@@ -41,6 +41,7 @@ import System.Exit (exitSuccess)
4141import System.FilePath
4242import System.IO (stdout)
4343import Text.Pandoc
44+import Text.Pandoc.Builder (setMeta)
4445import Text.Pandoc.App.Opt (Opt (..), LineEnding (..))
4546import Text.Pandoc.Filter (Filter (..))
4647import Text.Pandoc.Highlighting (highlightingStyles)
@@ -163,7 +164,8 @@ options =
163164 (ReqArg
164165 (\arg opt -> do
165166 let (key, val) = splitField arg
166− return opt{ optMetadata = (key, val) : optMetadata opt })
167+ return opt{ optMetadata = addMeta key val $
168+ optMetadata opt })
167169 "KEY[:VALUE]")
168170 ""
169171
@@ -626,7 +628,8 @@ options =
626628 , Option "" ["bibliography"]
627629 (ReqArg
628630 (\arg opt -> return opt{ optMetadata =
629− ("bibliography", arg) : optMetadata opt })
631+ addMeta "bibliography" arg $
632+ optMetadata opt })
630633 "FILE")
631634 ""
632635
@@ -634,7 +637,7 @@ options =
634637 (ReqArg
635638 (\arg opt ->
636639 return opt{ optMetadata =
637− ("csl", arg) : optMetadata opt })
640+ addMeta "csl" arg $ optMetadata opt })
638641 "FILE")
639642 ""
640643
@@ -642,7 +645,8 @@ options =
642645 (ReqArg
643646 (\arg opt ->
644647 return opt{ optMetadata =
645− ("citation-abbreviations", arg): optMetadata opt })
648+ addMeta "citation-abbreviations" arg $
649+ optMetadata opt })
646650 "FILE")
647651 ""
648652
@@ -981,6 +985,26 @@ setVariable :: String -> String -> Context Text -> Context Text
981985setVariable key val (Context ctx) =
982986 Context $ M.insert (T.pack key) (toVal (T.pack val)) ctx
983987
988+addMeta :: String -> String -> Meta -> Meta
989+addMeta k v meta =
990+ case lookupMeta k meta of
991+ Nothing -> setMeta k v' meta
992+ Just (MetaList xs) ->
993+ setMeta k (MetaList (xs ++ [v'])) meta
994+ Just x -> setMeta k (MetaList [x, v']) meta
995+ where
996+ v' = readMetaValue v
997+
998+readMetaValue :: String -> MetaValue
999+readMetaValue s
1000+ | s == "true" = MetaBool True
1001+ | s == "True" = MetaBool True
1002+ | s == "TRUE" = MetaBool True
1003+ | s == "false" = MetaBool False
1004+ | s == "False" = MetaBool False
1005+ | s == "FALSE" = MetaBool False
1006+ | otherwise = MetaString s
1007+
9841008-- On Windows with ghc 8.6+, we need to rewrite paths
9851009-- beginning with \\ to \\?\UNC\. -- See #5127.
9861010normalizePath :: FilePath -> FilePath
src/Text/Pandoc/App/Opt.hs+4−3
@@ -21,7 +21,7 @@ module Text.Pandoc.App.Opt (
2121 ) where
2222import Prelude
2323import Data.Char (isLower, toLower)
24−import GHC.Generics
24+import GHC.Generics hiding (Meta)
2525import Text.Pandoc.Filter (Filter (..))
2626import Text.Pandoc.Logging (Verbosity (WARNING))
2727import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
@@ -35,6 +35,7 @@ import Text.DocTemplates (Context(..))
3535import Data.Text (Text)
3636import Data.Aeson (defaultOptions, Options(..))
3737import Data.Aeson.TH (deriveJSON)
38+import Text.Pandoc.Definition (Meta)
3839
3940-- | The type of line-endings to be used when writing plain-text.
4041data LineEnding = LF | CRLF | Native deriving (Show, Generic)
@@ -50,7 +51,7 @@ data Opt = Opt
5051 , optShiftHeadingLevelBy :: Int -- ^ Shift heading level by
5152 , optTemplate :: Maybe FilePath -- ^ Custom template
5253 , optVariables :: Context Text -- ^ Template variables to set
53− , optMetadata :: [(String, String)] -- ^ Metadata fields to set
54+ , optMetadata :: Meta -- ^ Metadata fields to set
5455 , optMetadataFile :: [FilePath] -- ^ Name of YAML metadata file
5556 , optOutputFile :: Maybe FilePath -- ^ Name of output file
5657 , optInputFiles :: [FilePath] -- ^ Names of input files
@@ -124,7 +125,7 @@ defaultOpts = Opt
124125 , optShiftHeadingLevelBy = 0
125126 , optTemplate = Nothing
126127 , optVariables = mempty
127− , optMetadata = []
128+ , optMetadata = mempty
128129 , optMetadataFile = []
129130 , optOutputFile = Nothing
130131 , optInputFiles = []
src/Text/Pandoc/App/OutputSettings.hs−7
@@ -37,7 +37,6 @@ import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths)
3737import Text.Pandoc.App.Opt (Opt (..))
3838import Text.Pandoc.App.CommandLineOptions (engines, lookupHighlightStyle,
3939 setVariable)
40−import Text.Pandoc.BCP47 (Lang (..), parseBCP47)
4140import qualified Text.Pandoc.UTF8 as UTF8
4241
4342-- | Settings specifying how document output should be produced.
@@ -197,12 +196,6 @@ optToOutputSettings opts = do
197196 Left e -> throwError $ PandocTemplateError e
198197 Right t -> return $ Just t
199198
200− case lookup "lang" (optMetadata opts) of
201− Just l -> case parseBCP47 l of
202− Left _ -> return ()
203− Right l' -> setTranslations l'
204− Nothing -> setTranslations $ Lang "en" "" "US" []
205−
206199 let writerOpts = def {
207200 writerTemplate = templ
208201 , writerVariables = variables