git.iko.soy/typeable/pandoc
on master help sign in
Fix list fields in Opt so they aren't reversed.

Previously optIncludeInHeader, etc. were in reverse order. This has been changed to promote #5881. Note also that the `sourcefile` variable used to be sometimes a string, sometimes a list (when there was more than one). Now it is always a list.

commit20309691c38eff32b16c0cb734ecc350caaba220 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 7 Nov 2019, 12:18 (7 years ago) parent9c7f75a
3 files changed+30−30
src/Text/Pandoc/App/CommandLineOptions.hs+4−4 src/Text/Pandoc/App/OutputSettings.hs+20−24 src/Text/Pandoc/Writers/HTML.hs+6−2
src/Text/Pandoc/App/CommandLineOptions.hs+4−4
@@ -278,7 +278,7 @@ options =
278278 , Option "H" ["include-in-header"]
279279 (ReqArg
280280 (\arg opt -> return opt{ optIncludeInHeader =
281− arg : optIncludeInHeader opt,
281+ optIncludeInHeader opt ++ [arg],
282282 optStandalone = True })
283283 "FILE")
284284 "" -- "File to include at end of header (implies -s)"
@@ -286,7 +286,7 @@ options =
286286 , Option "B" ["include-before-body"]
287287 (ReqArg
288288 (\arg opt -> return opt{ optIncludeBeforeBody =
289− arg : optIncludeBeforeBody opt,
289+ optIncludeBeforeBody opt ++ [arg],
290290 optStandalone = True })
291291 "FILE")
292292 "" -- "File to include before document body"
@@ -294,7 +294,7 @@ options =
294294 , Option "A" ["include-after-body"]
295295 (ReqArg
296296 (\arg opt -> return opt{ optIncludeAfterBody =
297− arg : optIncludeAfterBody opt,
297+ optIncludeAfterBody opt ++ [arg],
298298 optStandalone = True })
299299 "FILE")
300300 "" -- "File to include after document body"
@@ -583,7 +583,7 @@ options =
583583
584584 , Option "c" ["css"]
585585 (ReqArg
586− (\arg opt -> return opt{ optCss = arg : optCss opt })
586+ (\arg opt -> return opt{ optCss = optCss opt ++ [arg] })
587587 -- add new link to end, so it is included in proper order
588588 "URL")
589589 "" -- "Link to CSS style sheet"
src/Text/Pandoc/App/OutputSettings.hs+20−24
@@ -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)
src/Text/Pandoc/Writers/HTML.hs+6−2
@@ -223,8 +223,12 @@ writeHtmlString' st opts d = do
223223 case getField "pagetitle" context of
224224 Just (s :: Text) | not (T.null s) -> return context
225225 _ -> do
226− let fallback = maybe "Untitled" (takeBaseName . T.unpack) $
227− lookupContext "sourcefile" (writerVariables opts)
226+ let fallback =
227+ case lookupContext "sourcefile"
228+ (writerVariables opts) of
229+ Nothing -> "Untitled"
230+ Just [] -> "Untitled"
231+ Just (x:_) -> takeBaseName $ T.unpack x
228232 report $ NoTitleElement fallback
229233 return $ resetField "pagetitle" (T.pack fallback) context
230234 return $ render Nothing $ renderTemplate tpl