git.iko.soy/typeable/pandoc
on master help sign in
Added `writerEpubSubdirectory` to `WriterOptions`.
379b99fJohn MacFarlane
src/Text/Pandoc/Error.hs+3
@@ -63,6 +63,7 @@ data PandocError = PandocIOError String IOError
63 | PandocResourceNotFound String
63 | PandocResourceNotFound String
64 | PandocTemplateError String
64 | PandocTemplateError String
65 | PandocAppError String
65 | PandocAppError String
66+ | PandocEpubSubdirectoryError String
66 deriving (Show, Typeable, Generic)
67 deriving (Show, Typeable, Generic)
67
68
68instance Exception PandocError
69instance Exception PandocError
@@ -104,6 +105,8 @@ handleError (Left e) =
104 "File " ++ fn ++ " not found in resource path"
105 "File " ++ fn ++ " not found in resource path"
105 PandocTemplateError s -> err 5 s
106 PandocTemplateError s -> err 5 s
106 PandocAppError s -> err 1 s
107 PandocAppError s -> err 1 s
108+ PandocEpubSubdirectoryError s -> err 31 $
109+ "EPUB subdirectory name '" ++ s ++ "' contains illegal characters"
107
110
108err :: Int -> String -> IO a
111err :: Int -> String -> IO a
109err exitCode msg = do
112err exitCode msg = do
src/Text/Pandoc/Options.hs+2
@@ -213,6 +213,7 @@ data WriterOptions = WriterOptions
213 , writerHighlightStyle :: Maybe Style -- ^ Style to use for highlighting
213 , writerHighlightStyle :: Maybe Style -- ^ Style to use for highlighting
214 -- (Nothing = no highlighting)
214 -- (Nothing = no highlighting)
215 , writerSetextHeaders :: Bool -- ^ Use setext headers for levels 1-2 in markdown
215 , writerSetextHeaders :: Bool -- ^ Use setext headers for levels 1-2 in markdown
216+ , writerEpubSubdirectory :: String -- ^ Subdir for epub in OCF
216 , writerEpubMetadata :: Maybe String -- ^ Metadata to include in EPUB
217 , writerEpubMetadata :: Maybe String -- ^ Metadata to include in EPUB
217 , writerEpubFonts :: [FilePath] -- ^ Paths to fonts to embed
218 , writerEpubFonts :: [FilePath] -- ^ Paths to fonts to embed
218 , writerEpubChapterLevel :: Int -- ^ Header level for chapters (separate files)
219 , writerEpubChapterLevel :: Int -- ^ Header level for chapters (separate files)
@@ -249,6 +250,7 @@ instance Default WriterOptions where
249 , writerListings = False
250 , writerListings = False
250 , writerHighlightStyle = Just pygments
251 , writerHighlightStyle = Just pygments
251 , writerSetextHeaders = True
252 , writerSetextHeaders = True
253+ , writerEpubSubdirectory = "EPUB"
252 , writerEpubMetadata = Nothing
254 , writerEpubMetadata = Nothing
253 , writerEpubFonts = []
255 , writerEpubFonts = []
254 , writerEpubChapterLevel = 1
256 , writerEpubChapterLevel = 1
src/Text/Pandoc/Writers/EPUB.hs+8−8
@@ -34,14 +34,14 @@ Conversion of 'Pandoc' documents to EPUB.
34module Text.Pandoc.Writers.EPUB ( writeEPUB2, writeEPUB3 ) where
34module Text.Pandoc.Writers.EPUB ( writeEPUB2, writeEPUB3 ) where
35import Codec.Archive.Zip (Entry, addEntryToArchive, eRelativePath, emptyArchive,
35import Codec.Archive.Zip (Entry, addEntryToArchive, eRelativePath, emptyArchive,
36 fromArchive, fromEntry, toEntry)
36 fromArchive, fromEntry, toEntry)
37−import Control.Monad (mplus, when, zipWithM)
37+import Control.Monad (mplus, when, unless, zipWithM)
38import Control.Monad.Except (catchError, throwError)
38import Control.Monad.Except (catchError, throwError)
39import Control.Monad.State.Strict (State, StateT, evalState, evalStateT, get, gets,
39import Control.Monad.State.Strict (State, StateT, evalState, evalStateT, get, gets,
40 lift, modify, put)
40 lift, modify, put)
41import qualified Data.ByteString.Lazy as B
41import qualified Data.ByteString.Lazy as B
42import qualified Data.ByteString.Lazy.Char8 as B8
42import qualified Data.ByteString.Lazy.Char8 as B8
43import qualified Data.Text.Lazy as TL
43import qualified Data.Text.Lazy as TL
44−import Data.Char (isAlphaNum, isDigit, toLower)
44+import Data.Char (isAlphaNum, isDigit, toLower, isAscii)
45import Data.List (intercalate, isInfixOf, isPrefixOf)
45import Data.List (intercalate, isInfixOf, isPrefixOf)
46import qualified Data.Map as M
46import qualified Data.Map as M
47import Data.Maybe (catMaybes, fromMaybe)
47import Data.Maybe (catMaybes, fromMaybe)
@@ -80,7 +80,6 @@ data Chapter = Chapter (Maybe [Int]) [Block]
80
80
81data EPUBState = EPUBState {
81data EPUBState = EPUBState {
82 stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
82 stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
83− , stEPUBSubdir :: String
84 }
83 }
85
84
86type E m = StateT EPUBState m
85type E m = StateT EPUBState m
@@ -362,9 +361,7 @@ writeEPUB :: PandocMonad m
362 -> Pandoc -- ^ Document to convert
361 -> Pandoc -- ^ Document to convert
363 -> m B.ByteString
362 -> m B.ByteString
364writeEPUB epubVersion opts doc =
363writeEPUB epubVersion opts doc =
365− let initState = EPUBState { stMediaPaths = []
364+ let initState = EPUBState { stMediaPaths = [] }
366− , stEPUBSubdir = "EPUB"
367− }
368 in
365 in
369 evalStateT (pandocToEPUB epubVersion opts doc)
366 evalStateT (pandocToEPUB epubVersion opts doc)
370 initState
367 initState
@@ -375,7 +372,10 @@ pandocToEPUB :: PandocMonad m
375 -> Pandoc
372 -> Pandoc
376 -> E m B.ByteString
373 -> E m B.ByteString
377pandocToEPUB version opts doc@(Pandoc meta _) = do
374pandocToEPUB version opts doc@(Pandoc meta _) = do
378− epubSubdir <- gets stEPUBSubdir
375+ let epubSubdir = writerEpubSubdirectory opts
376+ -- sanity check on epubSubdir
377+ unless (all (\c -> isAscii c && isAlphaNum c) epubSubdir) $
378+ throwError $ PandocEpubSubdirectoryError epubSubdir
379 let epub3 = version == EPUB3
379 let epub3 = version == EPUB3
380 let writeHtml o = fmap (UTF8.fromTextLazy . TL.fromStrict) .
380 let writeHtml o = fmap (UTF8.fromTextLazy . TL.fromStrict) .
381 writeHtmlStringForEPUB version o
381 writeHtmlStringForEPUB version o
@@ -888,7 +888,7 @@ modifyMediaRef :: PandocMonad m
888modifyMediaRef _ "" = return ""
888modifyMediaRef _ "" = return ""
889modifyMediaRef opts oldsrc = do
889modifyMediaRef opts oldsrc = do
890 media <- gets stMediaPaths
890 media <- gets stMediaPaths
891− epubSubdir <- gets stEPUBSubdir
891+ let epubSubdir = writerEpubSubdirectory opts
892 case lookup oldsrc media of
892 case lookup oldsrc media of
893 Just (n,_) -> return n
893 Just (n,_) -> return n
894 Nothing -> catchError
894 Nothing -> catchError