git.iko.soy/typeable/pandoc
on master help sign in
Change optVariables from [(String, String)] to Context Text.

In Text.Pandoc.App.Opt [API change].

commit3aa069e1d5853966964471e36b0099ddba0489fd authorJohn MacFarlane <jgm@berkeley.edu> dateTue 8 Oct 2019, 23:47 (7 years ago) parentaceee9c
4 files changed+37−27
src/Text/Pandoc/App/CommandLineOptions.hs+17−6 src/Text/Pandoc/App/Opt.hs+4−2 src/Text/Pandoc/App/OutputSettings.hs+15−18 stack.yaml+1−1
src/Text/Pandoc/App/CommandLineOptions.hs+17−6
@@ -18,6 +18,7 @@ module Text.Pandoc.App.CommandLineOptions (
1818 , options
1919 , engines
2020 , lookupHighlightStyle
21+ , setVariable
2122 ) where
2223import Prelude
2324import Control.Monad
@@ -59,6 +60,8 @@ import qualified Data.ByteString as BS
5960import qualified Data.ByteString.Lazy as B
6061import qualified Data.Map as M
6162import qualified Data.Text as T
63+import Data.Text (Text)
64+import Text.DocTemplates (ToContext(toVal), Context(..))
6265import qualified Text.Pandoc.UTF8 as UTF8
6366
6467parseOptions :: [OptDescr (Opt -> IO Opt)] -> Opt -> IO Opt
@@ -175,7 +178,8 @@ options =
175178 (ReqArg
176179 (\arg opt -> do
177180 let (key, val) = splitField arg
178− return opt{ optVariables = (key, val) : optVariables opt })
181+ return opt{ optVariables =
182+ setVariable key val $ optVariables opt })
179183 "KEY[:VALUE]")
180184 ""
181185
@@ -554,10 +558,11 @@ options =
554558
555559 , Option "T" ["title-prefix"]
556560 (ReqArg
557− (\arg opt -> do
558− let newvars = ("title-prefix", arg) : optVariables opt
559− return opt { optVariables = newvars,
560− optStandalone = True })
561+ (\arg opt ->
562+ return opt {
563+ optVariables =
564+ setVariable "title-prefix" arg $ optVariables opt,
565+ optStandalone = True })
561566 "STRING")
562567 "" -- "String to prefix to HTML window title"
563568
@@ -579,7 +584,8 @@ options =
579584 (ReqArg
580585 (\arg opt ->
581586 return opt { optVariables =
582− ("epub-cover-image", arg) : optVariables opt })
587+ setVariable "epub-cover-image" arg $
588+ optVariables opt })
583589 "FILE")
584590 "" -- "Path of epub cover image"
585591
@@ -970,6 +976,11 @@ deprecatedOption o msg =
970976 Right () -> return ()
971977 Left e -> E.throwIO e
972978
979+-- | Set text value in text context.
980+setVariable :: String -> String -> Context Text -> Context Text
981+setVariable key val (Context ctx) =
982+ Context $ M.insert (T.pack key) (toVal (T.pack val)) ctx
983+
973984-- On Windows with ghc 8.6+, we need to rewrite paths
974985-- beginning with \\ to \\?\UNC\. -- See #5127.
975986normalizePath :: FilePath -> FilePath
src/Text/Pandoc/App/Opt.hs+4−2
@@ -31,6 +31,8 @@ import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
3131 ObfuscationMethod (NoObfuscation),
3232 CiteMethod (Citeproc))
3333import Text.Pandoc.Shared (camelCaseToHyphenated)
34+import Text.DocTemplates (Context(..))
35+import Data.Text (Text)
3436import Data.Aeson (defaultOptions, Options(..))
3537import Data.Aeson.TH (deriveJSON)
3638
@@ -47,7 +49,7 @@ data Opt = Opt
4749 , optTableOfContents :: Bool -- ^ Include table of contents
4850 , optShiftHeadingLevelBy :: Int -- ^ Shift heading level by
4951 , optTemplate :: Maybe FilePath -- ^ Custom template
50− , optVariables :: [(String,String)] -- ^ Template variables to set
52+ , optVariables :: Context Text -- ^ Template variables to set
5153 , optMetadata :: [(String, String)] -- ^ Metadata fields to set
5254 , optMetadataFile :: [FilePath] -- ^ Name of YAML metadata file
5355 , optOutputFile :: Maybe FilePath -- ^ Name of output file
@@ -121,7 +123,7 @@ defaultOpts = Opt
121123 , optTableOfContents = False
122124 , optShiftHeadingLevelBy = 0
123125 , optTemplate = Nothing
124− , optVariables = []
126+ , optVariables = mempty
125127 , optMetadata = []
126128 , optMetadataFile = []
127129 , optOutputFile = Nothing
src/Text/Pandoc/App/OutputSettings.hs+15−18
@@ -20,9 +20,6 @@ module Text.Pandoc.App.OutputSettings
2020 ) where
2121import Prelude
2222import qualified Control.Exception as E
23−import qualified Data.Text as T
24−import qualified Data.Map as M
25−import Text.DocTemplates (Context(..), ToContext(toVal))
2623import Control.Monad
2724import Control.Monad.Except (catchError, throwError)
2825import Control.Monad.Trans
@@ -38,7 +35,8 @@ import System.IO (stdout)
3835import Text.Pandoc
3936import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths)
4037import Text.Pandoc.App.Opt (Opt (..))
41−import Text.Pandoc.App.CommandLineOptions (engines, lookupHighlightStyle)
38+import Text.Pandoc.App.CommandLineOptions (engines, lookupHighlightStyle,
39+ setVariable)
4240import Text.Pandoc.BCP47 (Lang (..), parseBCP47)
4341import qualified Text.Pandoc.UTF8 as UTF8
4442
@@ -97,8 +95,6 @@ optToOutputSettings opts = do
9795
9896 let standalone = optStandalone opts || not (isTextFormat format) || pdfOutput
9997
100− let addStringAsVariable varname s vars = return $ (varname, s) : vars
101−
10298 let addSyntaxMap existingmap f = do
10399 res <- liftIO (parseSyntaxDefinition f)
104100 case res of
@@ -117,21 +113,24 @@ optToOutputSettings opts = do
117113 let withList _ [] vars = return vars
118114 withList f (x:xs) vars = f x vars >>= withList f xs
119115
116+ let setVariableM k v = return . setVariable k v
117+
120118 let addContentsAsVariable varname fp vars = do
121119 s <- UTF8.toString . fst <$> fetchItem fp
122− return $ (varname, s) : vars
120+ setVariableM varname s vars
123121
124122 curdir <- liftIO getCurrentDirectory
125123
126124 variables <-
127− withList (addStringAsVariable "sourcefile")
128− (reverse $ optInputFiles opts)
129− (("outputfile", fromMaybe "-" (optOutputFile opts))
130− : optVariables opts)
125+ withList (setVariableM "sourcefile")
131126 -- we reverse this list because, unlike
132127 -- the other option lists here, it is
133128 -- not reversed when parsed from CLI arguments.
134129 -- See withList, above.
130+ (reverse $ optInputFiles opts)
131+ (optVariables opts)
132+ >>=
133+ setVariableM "outputfile" (fromMaybe "-" (optOutputFile opts))
135134 >>=
136135 withList (addContentsAsVariable "include-before")
137136 (optIncludeBeforeBody opts)
@@ -142,15 +141,15 @@ optToOutputSettings opts = do
142141 withList (addContentsAsVariable "header-includes")
143142 (optIncludeInHeader opts)
144143 >>=
145− withList (addStringAsVariable "css") (optCss opts)
144+ withList (setVariableM "css") (optCss opts)
146145 >>=
147− maybe return (addStringAsVariable "title-prefix")
146+ maybe return (setVariableM "title-prefix")
148147 (optTitlePrefix opts)
149148 >>=
150− maybe return (addStringAsVariable "epub-cover-image")
149+ maybe return (setVariableM "epub-cover-image")
151150 (optEpubCoverImage opts)
152151 >>=
153− addStringAsVariable "curdir" curdir
152+ setVariableM "curdir" curdir
154153 >>=
155154 (\vars -> if format == "dzslides"
156155 then do
@@ -160,10 +159,8 @@ optToOutputSettings opts = do
160159 let dzcore = unlines
161160 $ dropWhile (not . (dzline `isPrefixOf`))
162161 $ lines dztempl
163− return $ ("dzslides-core", dzcore) : vars
162+ setVariableM "dzslides-core" dzcore vars
164163 else return vars)
165− >>= fmap (Context . M.fromList) .
166− traverse (\(x,y) -> return (T.pack x, toVal (T.pack y)))
167164
168165 templStr <- case optTemplate opts of
169166 _ | not standalone -> return Nothing
stack.yaml+1−1
@@ -24,7 +24,7 @@ extra-deps:
2424- HsYAML-aeson-0.2.0.0
2525# - doctemplates-0.6.1
2626- git: https://github.com/jgm/doctemplates.git
27− commit: b0e92bd6e32eb1a8c021598b4e8a5f25b9c5cd40
27+ commit: 8c30b5955584ff96459999c4958e8a953fed214f
2828ghc-options:
2929 "$locals": -fhide-source-paths -Wno-missing-home-modules
3030resolver: lts-14.6