@@ -19,6 +19,9 @@ module Text.Pandoc.App.OutputSettings
1919 , optToOutputSettings
2020 ) where
2121import Prelude
22+import qualified Data.Map as M
23+import qualified Data.Text as T
24+import Text.DocTemplates (toVal, Context(..))
2225import qualified Control.Exception as E
2326import Control.Monad
2427import Control.Monad.Except (catchError, throwError)
@@ -111,41 +114,34 @@ optToOutputSettings opts = do
111114 hlStyle <- maybe (return Nothing) (fmap Just . lookupHighlightStyle)
112115 (optHighlightStyle opts)
113116
114− -- note: this reverses the list constructed in option parsing,
115− -- which in turn was reversed from the command-line order,
116− -- so we end up with the correct order in the variable list:
117− let withList _ [] vars = return vars
118− withList f (x:xs) vars = f x vars >>= withList f xs
119−
120117 let setVariableM k v = return . setVariable k v
121118
122− let addContentsAsVariable varname fp vars = do
123− s <- UTF8.toString . fst <$> fetchItem fp
124− setVariableM varname s vars
119+ let setListVariableM k vs =
120+ return . Context .
121+ (M.insert (T.pack k) (toVal $ map T.pack vs)) . unContext
122+
123+ let getStringContents fp = UTF8.toString . fst <$> fetchItem fp
124+
125+ let setFilesVariableM k fps ctx = do
126+ xs <- mapM getStringContents fps
127+ setListVariableM k xs ctx
125128
126129 curdir <- liftIO getCurrentDirectory
127130
128131 variables <-
129− withList (setVariableM "sourcefile")
130− -- we reverse this list because, unlike
131− -- the other option lists here, it is
132− -- not reversed when parsed from CLI arguments.
133− -- See withList, above.
134− (reverse $ optInputFiles opts)
135− (optVariables opts)
132+ return (optVariables opts)
133+ >>=
134+ setListVariableM "sourcefile" (optInputFiles opts)
136135 >>=
137− setVariableM "outputfile" (fromMaybe "-" (optOutputFile opts))
136+ setVariableM "outputfile" outputFile
138137 >>=
139− withList (addContentsAsVariable "include-before")
140− (optIncludeBeforeBody opts)
138+ setFilesVariableM "include-before" (optIncludeBeforeBody opts)
141139 >>=
142− withList (addContentsAsVariable "include-after")
143− (optIncludeAfterBody opts)
140+ setFilesVariableM "include-after" (optIncludeAfterBody opts)
144141 >>=
145− withList (addContentsAsVariable "header-includes")
146− (optIncludeInHeader opts)
142+ setFilesVariableM "header-includes" (optIncludeInHeader opts)
147143 >>=
148− withList (setVariableM "css") (optCss opts)
144+ setListVariableM "css" (optCss opts)
149145 >>=
150146 maybe return (setVariableM "title-prefix")
151147 (optTitlePrefix opts)