git.iko.soy/typeable/pandoc
on master help sign in
Templates: change signature of getDefaultTemplate.

Now it runs in any instance of PandocMonad, and returns a String rather than an Either value.

commit992943d98e14cc2dd249d6279c8c930dddc5547d authorJohn MacFarlane <jgm@berkeley.edu> dateWed 19 Jul 2017, 21:46 (9 years ago) parent2ce6b49
2 files changed+14−13
src/Text/Pandoc/App.hs+4−3 src/Text/Pandoc/Templates.hs+10−10
src/Text/Pandoc/App.hs+4−3
@@ -218,7 +218,8 @@ convertWithOpts opts = do
218218 templ <- case optTemplate opts of
219219 _ | not standalone -> return Nothing
220220 Nothing -> do
221− deftemp <- getDefaultTemplate datadir format
221+ deftemp <- runIO $
222+ getDefaultTemplate datadir format
222223 case deftemp of
223224 Left e -> E.throwIO e
224225 Right t -> return (Just t)
@@ -991,10 +992,10 @@ options =
991992 , Option "D" ["print-default-template"]
992993 (ReqArg
993994 (\arg _ -> do
994− templ <- getDefaultTemplate Nothing arg
995+ templ <- runIO $ getDefaultTemplate Nothing arg
995996 case templ of
996997 Right t -> UTF8.hPutStr stdout t
997− Left e -> E.throwIO $ PandocAppError (show e)
998+ Left e -> E.throwIO e
998999 exitSuccess)
9991000 "FORMAT")
10001001 "" -- "Print default template for FORMAT"
src/Text/Pandoc/Templates.hs+10−10
@@ -38,28 +38,28 @@ module Text.Pandoc.Templates ( module Text.DocTemplates
3838 , getDefaultTemplate
3939 ) where
4040
41−import qualified Control.Exception as E (IOException, try)
4241import Control.Monad.Except (throwError)
4342import Data.Aeson (ToJSON (..))
4443import qualified Data.Text as T
4544import System.FilePath ((<.>), (</>))
4645import Text.DocTemplates (Template, TemplateTarget, applyTemplate,
4746 compileTemplate, renderTemplate, varListToJSON)
48−import Text.Pandoc.Class (PandocMonad)
47+import Text.Pandoc.Class (PandocMonad(readDataFile))
4948import Text.Pandoc.Error
50−import Text.Pandoc.Shared (readDataFileUTF8)
49+import qualified Text.Pandoc.UTF8 as UTF8
5150
5251-- | Get default template for the specified writer.
53−getDefaultTemplate :: (Maybe FilePath) -- ^ User data directory to search first
52+getDefaultTemplate :: PandocMonad m
53+ => (Maybe FilePath) -- ^ User data directory to search 1st
5454 -> String -- ^ Name of writer
55− -> IO (Either E.IOException String)
55+ -> m String
5656getDefaultTemplate user writer = do
5757 let format = takeWhile (`notElem` ("+-" :: String)) writer -- strip off extensions
5858 case format of
59− "native" -> return $ Right ""
60− "json" -> return $ Right ""
61− "docx" -> return $ Right ""
62− "fb2" -> return $ Right ""
59+ "native" -> return ""
60+ "json" -> return ""
61+ "docx" -> return ""
62+ "fb2" -> return ""
6363 "odt" -> getDefaultTemplate user "opendocument"
6464 "html" -> getDefaultTemplate user "html5"
6565 "docbook" -> getDefaultTemplate user "docbook5"
@@ -70,7 +70,7 @@ getDefaultTemplate user writer = do
7070 "markdown_mmd" -> getDefaultTemplate user "markdown"
7171 "markdown_phpextra" -> getDefaultTemplate user "markdown"
7272 _ -> let fname = "templates" </> "default" <.> format
73− in E.try $ readDataFileUTF8 user fname
73+ in UTF8.toString <$> readDataFile user fname
7474
7575-- | Like 'applyTemplate', but runs in PandocMonad and
7676-- raises an error if compilation fails.