git.iko.soy/typeable/pandoc
on master help sign in
Fix and simply latex engine code in App.

Fixes #3931.

commit582cb4b505b774fe8b7424363bcf310e97871c53 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 19 Sep 2017, 17:22 (9 years ago) parentd791783
1 file changed+44−47
src/Text/Pandoc/App.hs+44−47
src/Text/Pandoc/App.hs+44−47
@@ -50,7 +50,7 @@ import qualified Data.ByteString as BS
5050import qualified Data.ByteString.Lazy as B
5151import Data.Char (toLower, toUpper)
5252import Data.Foldable (foldrM)
53−import Data.List (intercalate, isPrefixOf, isSuffixOf, sort)
53+import Data.List (intercalate, isPrefixOf, isSuffixOf, sort, find)
5454import qualified Data.Map as M
5555import Data.Maybe (fromMaybe, isJust, isNothing)
5656import qualified Data.Set as Set
@@ -86,7 +86,7 @@ import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
8686import Text.Pandoc.PDF (makePDF)
8787import Text.Pandoc.Process (pipeProcess)
8888import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
89−import Text.Pandoc.Shared (headerShift, isURI, openURL,
89+import Text.Pandoc.Shared (headerShift, isURI, openURL, ordNub,
9090 safeRead, tabFilter, eastAsianLineBreakFilter)
9191import qualified Text.Pandoc.UTF8 as UTF8
9292import Text.Pandoc.XML (toEntities)
@@ -124,56 +124,48 @@ parseOptions options' defaults = do
124124latexEngines :: [String]
125125latexEngines = ["pdflatex", "lualatex", "xelatex"]
126126
127−defaultLatexEngine :: String
128−defaultLatexEngine = "pdflatex"
129−
130127htmlEngines :: [String]
131128htmlEngines = ["wkhtmltopdf", "weasyprint", "prince"]
132129
133−defaultHtmlEngine :: String
134−defaultHtmlEngine = "wkhtmltopdf"
130+engines :: [(String, String)]
131+engines = map ("html",) htmlEngines ++
132+ map ("html5",) latexEngines ++
133+ map ("latex",) latexEngines ++
134+ map ("beamer",) latexEngines ++
135+ [ ("ms", "pdfroff")
136+ , ("context", "context")
137+ ]
135138
136139pdfEngines :: [String]
137−pdfEngines = latexEngines ++ htmlEngines ++ ["context", "pdfroff"]
140+pdfEngines = ordNub $ map snd engines
138141
139142pdfWriterAndProg :: Maybe String -- ^ user-specified writer name
140143 -> Maybe String -- ^ user-specified pdf-engine
141144 -> IO (String, Maybe String) -- ^ IO (writerName, maybePdfEngineProg)
142145pdfWriterAndProg mWriter mEngine = do
143146 let panErr msg = liftIO $ E.throwIO $ PandocAppError msg
144− case go mWriter mEngine of
145− (Right writ, Right prog) -> return (writ, Just prog)
146− (Left err, _) -> panErr err
147− (_, Left err) -> panErr err
147+ case go (baseWriterName <$> mWriter) mEngine of
148+ Right (writ, prog) -> return (writ, Just prog)
149+ Left err -> panErr err
148150 where
149− go Nothing Nothing = (Right "latex", Right defaultLatexEngine)
150− go (Just writer) Nothing = (Right writer, engineForWriter writer)
151− go Nothing (Just engine) = (writerForEngine engine, Right engine)
151+ go Nothing Nothing = Right ("latex", "pdflatex")
152+ go (Just writer) Nothing = (writer,) <$> engineForWriter writer
153+ go Nothing (Just engine) = (,engine) <$> writerForEngine engine
152154 go (Just writer) (Just engine) =
153− let (Right shouldFormat) = writerForEngine engine
154− userFormat = case map toLower writer of
155− "html5" -> "html"
156− x -> x
157− in if userFormat == shouldFormat
158− then (Right writer, Right engine)
159− else (Left $ "pdf-engine " ++ engine ++ " is not compatible with output format "
160− ++ writer ++ ", please use `-t " ++ shouldFormat ++ "`", Left "")
161−
162− writerForEngine "context" = Right "context"
163− writerForEngine "pdfroff" = Right "ms"
164− writerForEngine en
165− | takeBaseName en `elem` latexEngines = Right "latex"
166− | takeBaseName en `elem` htmlEngines = Right "html"
167− writerForEngine _ = Left "pdf-engine not known"
168−
169− engineForWriter "context" = Right "context"
170− engineForWriter "ms" = Right "pdfroff"
171− engineForWriter "latex" = Right defaultLatexEngine
172− engineForWriter "beamer" = Right defaultLatexEngine
173− engineForWriter format
174− | format `elem` ["html", "html5"] = Right defaultHtmlEngine
175− | otherwise = Left $ "cannot produce pdf output with output format " ++ format
155+ case find (== (writer, engine)) engines of
156+ Just _ -> Right (writer, engine)
157+ Nothing -> Left $ "pdf-engine " ++ engine ++
158+ " is not compatible with output format " ++ writer
159+
160+ writerForEngine eng = case [f | (f,e) <- engines, e == eng] of
161+ fmt : _ -> Right fmt
162+ [] -> Left $
163+ "pdf-engine " ++ eng ++ " not known"
176164
165+ engineForWriter w = case [e | (f,e) <- engines, f == w] of
166+ eng : _ -> Right eng
167+ [] -> Left $
168+ "cannot produce pdf output from " ++ w
177169
178170convertWithOpts :: Opt -> IO ()
179171convertWithOpts opts = do
@@ -223,18 +215,19 @@ convertWithOpts opts = do
223215 (if any isURI sources
224216 then "html"
225217 else "markdown") sources
226− Just x -> map toLower x
218+ Just x -> x
227219
228220 let nonPdfWriterName Nothing = defaultWriterName outputFile
229− nonPdfWriterName (Just x) = map toLower x
221+ nonPdfWriterName (Just x) = x
230222
231223 let pdfOutput = map toLower (takeExtension outputFile) == ".pdf"
232− (writerName, maybePdfProg) <- if pdfOutput
233− then pdfWriterAndProg (optWriter opts) (optPdfEngine opts)
234− else return (nonPdfWriterName $ optWriter opts, Nothing)
224+ (writerName, maybePdfProg) <-
225+ if pdfOutput
226+ then pdfWriterAndProg (optWriter opts) (optPdfEngine opts)
227+ else return (nonPdfWriterName $ optWriter opts, Nothing)
235228
236− let format = takeWhile (`notElem` ['+','-'])
237− $ takeFileName writerName -- in case path to lua script
229+ let format = baseWriterName
230+ $ takeFileName writerName -- in case path to lua script
238231
239232 -- disabling the custom writer for now
240233 (writer, writerExts) <-
@@ -931,13 +924,15 @@ options :: [OptDescr (Opt -> IO Opt)]
931924options =
932925 [ Option "fr" ["from","read"]
933926 (ReqArg
934− (\arg opt -> return opt { optReader = Just arg })
927+ (\arg opt -> return opt { optReader =
928+ Just (map toLower arg) })
935929 "FORMAT")
936930 ""
937931
938932 , Option "tw" ["to","write"]
939933 (ReqArg
940− (\arg opt -> return opt { optWriter = Just arg })
934+ (\arg opt -> return opt { optWriter =
935+ Just (map toLower arg) })
941936 "FORMAT")
942937 ""
943938
@@ -1680,3 +1675,5 @@ splitField s =
16801675 (k,_:v) -> (k,v)
16811676 (k,[]) -> (k,"true")
16821677
1678+baseWriterName :: String -> String
1679+baseWriterName = takeWhile (\c -> c /= '+' && c /= '-')