git.iko.soy/typeable/pandoc
on master help sign in
Improved default template lookup for custom lua scripts.

Previously, if you tried to do `pandoc -s -t /path/to/lua/script.lua`, pandoc would look for the template in `~/.pandoc/templates/default./path/to/lua/script.lua`. With this change it will look in the more reasonable `~/.pandoc/templates/default.script.lua`. This makes it possible to store default templates for custom writers. Closes #2625.

commita5efd2af11849b6fa55b40d188cc9925bfa4ab9f authorJohn MacFarlane <jgm@berkeley.edu> dateWed 6 Jan 2016, 15:45 (11 years ago) parentf6fe430
1 file changed+17−19
pandoc.hs+17−19
pandoc.hs+17−19
@@ -110,8 +110,7 @@ wrapWords indent c = wrap' (c - indent) (c - indent)
110110 wrap' cols (remaining - length x - 2) xs
111111
112112isTextFormat :: String -> Bool
113−isTextFormat s = takeWhile (`notElem` "+-") s `notElem` binaries
114− where binaries = ["odt","docx","epub","epub3"]
113+isTextFormat s = s `notElem` ["odt","docx","epub","epub3"]
115114
116115externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc
117116externalFilter f args' d = do
@@ -1162,23 +1161,24 @@ convertWithOpts opts args = do
11621161 "epub2" -> "epub"
11631162 "html4" -> "html"
11641163 x -> x
1164+ let format = takeWhile (`notElem` ['+','-'])
1165+ $ takeFileName writerName' -- in case path to lua script
11651166
11661167 let pdfOutput = map toLower (takeExtension outputFile) == ".pdf"
11671168
1168− let laTeXOutput = "latex" `isPrefixOf` writerName' ||
1169− "beamer" `isPrefixOf` writerName'
1170− let conTeXtOutput = "context" `isPrefixOf` writerName'
1171− let html5Output = "html5" `isPrefixOf` writerName'
1169+ let laTeXOutput = format `elem` ["latex", "beamer"]
1170+ let conTeXtOutput = format == "context"
1171+ let html5Output = format == "html5"
11721172
11731173 let laTeXInput = "latex" `isPrefixOf` readerName' ||
11741174 "beamer" `isPrefixOf` readerName'
11751175
1176− writer <- if ".lua" `isSuffixOf` writerName'
1176+ writer <- if ".lua" `isSuffixOf` format
11771177 -- note: use non-lowercased version writerName
11781178 then return $ IOStringWriter $ writeCustom writerName
11791179 else case getWriter writerName' of
11801180 Left e -> err 9 $
1181− if writerName' == "pdf"
1181+ if format == "pdf"
11821182 then e ++
11831183 "\nTo create a pdf with pandoc, use " ++
11841184 "the latex or beamer writer and specify\n" ++
@@ -1201,18 +1201,17 @@ convertWithOpts opts args = do
12011201 "\nPandoc can convert from DOCX, but not from DOC.\nTry using Word to save your DOC file as DOCX, and convert that with pandoc."
12021202 _ -> e
12031203
1204− let standalone' = standalone || not (isTextFormat writerName') || pdfOutput
1204+ let standalone' = standalone || not (isTextFormat format) || pdfOutput
12051205
12061206 templ <- case templatePath of
12071207 _ | not standalone' -> return ""
12081208 Nothing -> do
1209− deftemp <- getDefaultTemplate datadir writerName'
1209+ deftemp <- getDefaultTemplate datadir format
12101210 case deftemp of
12111211 Left e -> throwIO e
12121212 Right t -> return t
12131213 Just tp -> do
12141214 -- strip off extensions
1215− let format = takeWhile (`notElem` "+-") writerName'
12161215 let tp' = case takeExtension tp of
12171216 "" -> tp <.> format
12181217 _ -> tp
@@ -1234,7 +1233,7 @@ convertWithOpts opts args = do
12341233 return $ ("mathml-script", s) : variables
12351234 _ -> return variables
12361235
1237− variables'' <- if "dzslides" `isPrefixOf` writerName'
1236+ variables'' <- if format == "dzslides"
12381237 then do
12391238 dztempl <- readDataFileUTF8 datadir
12401239 ("dzslides" </> "template.html")
@@ -1270,8 +1269,8 @@ convertWithOpts opts args = do
12701269 , readerTrackChanges = trackChanges
12711270 }
12721271
1273− when (not (isTextFormat writerName') && outputFile == "-") $
1274− err 5 $ "Cannot write " ++ writerName' ++ " output to stdout.\n" ++
1272+ when (not (isTextFormat format) && outputFile == "-") $
1273+ err 5 $ "Cannot write " ++ format ++ " output to stdout.\n" ++
12751274 "Specify an output file using the -o option."
12761275
12771276 let readSources [] = mapM readSource ["-"]
@@ -1354,7 +1353,7 @@ convertWithOpts opts args = do
13541353 doc' <- (maybe return (extractMedia media) mbExtractMedia >=>
13551354 adjustMetadata metadata >=>
13561355 applyTransforms transforms >=>
1357− applyFilters filters' [writerName']) doc
1356+ applyFilters filters' [format]) doc
13581357
13591358 let writeBinary :: B.ByteString -> IO ()
13601359 writeBinary = B.writeFile (UTF8.encodePath outputFile)
@@ -1370,7 +1369,7 @@ convertWithOpts opts args = do
13701369 | pdfOutput -> do
13711370 -- make sure writer is latex or beamer or context or html5
13721371 unless (laTeXOutput || conTeXtOutput || html5Output) $
1373− err 47 $ "cannot produce pdf output with " ++ writerName' ++
1372+ err 47 $ "cannot produce pdf output with " ++ format ++
13741373 " writer"
13751374
13761375 let pdfprog = case () of
@@ -1393,9 +1392,8 @@ convertWithOpts opts args = do
13931392 | otherwise -> selfcontain (f writerOptions doc' ++
13941393 ['\n' | not standalone'])
13951394 >>= writerFn outputFile . handleEntities
1396− where htmlFormat = writerName' `elem`
1397− ["html","html+lhs","html5","html5+lhs",
1398− "s5","slidy","slideous","dzslides","revealjs"]
1395+ where htmlFormat = format `elem`
1396+ ["html","html5","s5","slidy","slideous","dzslides","revealjs"]
13991397 selfcontain = if selfContained && htmlFormat
14001398 then makeSelfContained writerOptions
14011399 else return