@@ -44,6 +44,8 @@ import Data.Char ( toLower )
44import Data.List ( intercalate, isSuffixOf, isPrefixOf )
44import Data.List ( intercalate, isSuffixOf, isPrefixOf )
45import System.Directory ( getAppUserDataDirectory, doesFileExist )
45import System.Directory ( getAppUserDataDirectory, doesFileExist )
46import System.IO ( stdout, stderr )
46import System.IO ( stdout, stderr )
47+import System.IO.Error ( isDoesNotExistError )
48+import Control.Exception.Extensible ( throwIO )
47import qualified Text.Pandoc.UTF8 as UTF8
49import qualified Text.Pandoc.UTF8 as UTF8
48import Text.CSL
50import Text.CSL
49import Text.Pandoc.Biblio
51import Text.Pandoc.Biblio
@@ -93,7 +95,7 @@ data Opt = Opt
93 , optParseRaw :: Bool -- ^ Parse unconvertable HTML and TeX
95 , optParseRaw :: Bool -- ^ Parse unconvertable HTML and TeX
94 , optTableOfContents :: Bool -- ^ Include table of contents
96 , optTableOfContents :: Bool -- ^ Include table of contents
95 , optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply
97 , optTransforms :: [Pandoc -> Pandoc] -- ^ Doc transforms to apply
96− , optTemplate :: String -- ^ Custom template
98+ , optTemplate :: Maybe FilePath -- ^ Custom template
97 , optVariables :: [(String,String)] -- ^ Template variables to set
99 , optVariables :: [(String,String)] -- ^ Template variables to set
98 , optOutputFile :: String -- ^ Name of output file
100 , optOutputFile :: String -- ^ Name of output file
99 , optNumberSections :: Bool -- ^ Number sections in LaTeX
101 , optNumberSections :: Bool -- ^ Number sections in LaTeX
@@ -137,7 +139,7 @@ defaultOpts = Opt
137 , optParseRaw = False
139 , optParseRaw = False
138 , optTableOfContents = False
140 , optTableOfContents = False
139 , optTransforms = []
141 , optTransforms = []
140− , optTemplate = ""
142+ , optTemplate = Nothing
141 , optVariables = []
143 , optVariables = []
142 , optOutputFile = "-" -- "-" means stdout
144 , optOutputFile = "-" -- "-" means stdout
143 , optNumberSections = False
145 , optNumberSections = False
@@ -407,8 +409,7 @@ options =
407 , Option "" ["template"]
409 , Option "" ["template"]
409 (\arg opt -> do
411 (\arg opt -> do
410− text <- UTF8.readFile arg
411− return opt{ optTemplate = text,
412+ return opt{ optTemplate = Just arg,
412 optStandalone = True })
413 optStandalone = True })
413 "FILENAME")
414 "FILENAME")
414 "" -- "Use custom template"
415 "" -- "Use custom template"
@@ -672,7 +673,7 @@ main = do
672 , optVariables = variables
673 , optVariables = variables
673 , optTableOfContents = toc
674 , optTableOfContents = toc
674 , optTransforms = transforms
675 , optTransforms = transforms
675− , optTemplate = template
676+ , optTemplate = templatePath
676 , optOutputFile = outputFile
677 , optOutputFile = outputFile
677 , optNumberSections = numberSections
678 , optNumberSections = numberSections
678 , optSectionDivs = sectionDivs
679 , optSectionDivs = sectionDivs
@@ -731,10 +732,18 @@ main = do
731 Just r -> return r
732 Just r -> return r
732 Nothing -> error ("Unknown reader: " ++ readerName')
733 Nothing -> error ("Unknown reader: " ++ readerName')
735+ templ <- case templatePath of
734− templ <- getDefaultTemplate datadir writerName'
737+ deftemp <- getDefaultTemplate datadir writerName'
735− let defaultTemplate = case templ of
738+ case deftemp of
736− Right t -> t
740+ Right t -> return t
737− Left e -> error (show e)
741+ Just tp -> catch (UTF8.readFile tp)
742+ (\e -> if isDoesNotExistError e
744+ (readDataFile datadir tp)
739 let standalone' = standalone || isNonTextOutput writerName'
748 let standalone' = standalone || isNonTextOutput writerName'
@@ -790,9 +799,7 @@ main = do
791 let writerOptions = defaultWriterOptions
800 let writerOptions = defaultWriterOptions
792 { writerStandalone = standalone',
801 { writerStandalone = standalone',
793− writerTemplate = if null template
802+ writerTemplate = templ,
794− then defaultTemplate
796 writerVariables = variables'',
803 writerVariables = variables'',
797 writerEPUBMetadata = epubMetadata,
804 writerEPUBMetadata = epubMetadata,
798 writerTabStop = tabStop,
805 writerTabStop = tabStop,