git.iko.soy/typeable/pandoc
on master help sign in
Support 'bibliography' and 'csl' fields in defaults file.

Move addMeta from T.P.App.CommandLineOptions to T.P.App.Opt. (not an api change because modules not exported)

commit22f484e9a9ab94109380e217b6ff88317bb72741 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 31 Jan 2020, 21:55 (7 years ago) parentfb3df6c
2 files changed+42−24
src/Text/Pandoc/App/CommandLineOptions.hs+1−23 src/Text/Pandoc/App/Opt.hs+41−1
src/Text/Pandoc/App/CommandLineOptions.hs+1−23
@@ -44,8 +44,7 @@ import System.FilePath
4444import System.IO (stdout)
4545import Text.DocTemplates (Val(..))
4646import Text.Pandoc
47−import Text.Pandoc.Builder (setMeta)
48−import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), IpynbOutput (..))
47+import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), IpynbOutput (..), addMeta)
4948import Text.Pandoc.Filter (Filter (..))
5049import Text.Pandoc.Highlighting (highlightingStyles)
5150import Text.Pandoc.Shared (ordNub, elemText, safeStrRead, defaultUserDataDirs)
@@ -1037,27 +1036,6 @@ setVariable key val (Context ctx) = Context $ M.alter go key ctx
10371036 go (Just (ListVal xs)) = Just $ ListVal $ xs ++ [toVal val]
10381037 go (Just x) = Just $ ListVal [x, toVal val]
10391038
1040−addMeta :: String -> String -> Meta -> Meta
1041−addMeta k v meta =
1042− case lookupMeta k' meta of
1043− Nothing -> setMeta k' v' meta
1044− Just (MetaList xs) ->
1045− setMeta k' (MetaList (xs ++ [v'])) meta
1046− Just x -> setMeta k' (MetaList [x, v']) meta
1047− where
1048− v' = readMetaValue v
1049− k' = T.pack k
1050−
1051−readMetaValue :: String -> MetaValue
1052−readMetaValue s
1053− | s == "true" = MetaBool True
1054− | s == "True" = MetaBool True
1055− | s == "TRUE" = MetaBool True
1056− | s == "false" = MetaBool False
1057− | s == "False" = MetaBool False
1058− | s == "FALSE" = MetaBool False
1059− | otherwise = MetaString $ T.pack s
1060−
10611039-- On Windows with ghc 8.6+, we need to rewrite paths
10621040-- beginning with \\ to \\?\UNC\. -- See #5127.
10631041normalizePath :: FilePath -> FilePath
src/Text/Pandoc/App/Opt.hs+41−1
@@ -21,10 +21,12 @@ module Text.Pandoc.App.Opt (
2121 , LineEnding (..)
2222 , IpynbOutput (..)
2323 , defaultOpts
24+ , addMeta
2425 ) where
2526import Prelude
2627import Data.Char (isLower, toLower)
2728import GHC.Generics hiding (Meta)
29+import Text.Pandoc.Builder (setMeta)
2830import Text.Pandoc.Filter (Filter (..))
2931import Text.Pandoc.Logging (Verbosity (WARNING))
3032import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
@@ -39,7 +41,7 @@ import Text.DocTemplates (Context(..), Val(..))
3941import Data.Text (Text, unpack)
4042import qualified Data.Text as T
4143import qualified Data.Map as M
42−import Text.Pandoc.Definition (Meta(..), MetaValue(..))
44+import Text.Pandoc.Definition (Meta(..), MetaValue(..), lookupMeta)
4345import Data.Aeson (defaultOptions, Options(..))
4446import Data.Aeson.TH (deriveJSON)
4547import Control.Applicative ((<|>))
@@ -342,6 +344,22 @@ doOpt (k',v) = do
342344 <|>
343345 (parseYAML v >>= \x -> return (\o -> o{ optCss = optCss o <>
344346 [unpack x] }))
347+ "bibliography" ->
348+ do let addItem x o = o{ optMetadata =
349+ addMeta "bibliography" (T.unpack x)
350+ (optMetadata o) }
351+ (parseYAML v >>= \(xs :: [Text]) -> return $ \o ->
352+ foldr addItem o xs)
353+ <|>
354+ (parseYAML v >>= \(x :: Text) -> return $ \o -> addItem x o)
355+ "csl" ->
356+ do let addItem x o = o{ optMetadata =
357+ addMeta "csl" (T.unpack x)
358+ (optMetadata o) }
359+ (parseYAML v >>= \(xs :: [Text]) -> return $ \o ->
360+ foldr addItem o xs)
361+ <|>
362+ (parseYAML v >>= \(x :: Text) -> return $ \o -> addItem x o)
345363 "ipynb-output" ->
346364 parseYAML v >>= \x -> return (\o -> o{ optIpynbOutput = x })
347365 "include-before-body" ->
@@ -465,6 +483,28 @@ valToMetaVal (ListVal xs) = MetaList $ map valToMetaVal xs
465483valToMetaVal (SimpleVal d) = MetaString $ render Nothing d
466484valToMetaVal NullVal = MetaString ""
467485
486+addMeta :: String -> String -> Meta -> Meta
487+addMeta k v meta =
488+ case lookupMeta k' meta of
489+ Nothing -> setMeta k' v' meta
490+ Just (MetaList xs) ->
491+ setMeta k' (MetaList (xs ++ [v'])) meta
492+ Just x -> setMeta k' (MetaList [x, v']) meta
493+ where
494+ v' = readMetaValue v
495+ k' = T.pack k
496+
497+readMetaValue :: String -> MetaValue
498+readMetaValue s
499+ | s == "true" = MetaBool True
500+ | s == "True" = MetaBool True
501+ | s == "TRUE" = MetaBool True
502+ | s == "false" = MetaBool False
503+ | s == "False" = MetaBool False
504+ | s == "FALSE" = MetaBool False
505+ | otherwise = MetaString $ T.pack s
506+
507+
468508-- see https://github.com/jgm/pandoc/pull/4083
469509-- using generic deriving caused long compilation times
470510$(deriveJSON