git.iko.soy/typeable/pandoc
on master help sign in
Added `writerEpubSubdirectory` to `WriterOptions`.

[API change] The EPUB writer now takes its EPUB subdirectory from this option. Also added `PandocEpubSubdirectoryError` to `PandocError`. This is raised if the EPUB subdirectory is not all ASCII alphanumerics. See #3720.

commit379b99f63abe534c7666c8e16e0bf2e980b63d1c authorJohn MacFarlane <jgm@berkeley.edu> dateThu 22 Jun 2017, 11:43 (9 years ago) parent242e2a0
3 files changed+13−8
src/Text/Pandoc/Error.hs+3 src/Text/Pandoc/Options.hs+2 src/Text/Pandoc/Writers/EPUB.hs+8−8
src/Text/Pandoc/Error.hs+3
@@ -63,6 +63,7 @@ data PandocError = PandocIOError String IOError
6363 | PandocResourceNotFound String
6464 | PandocTemplateError String
6565 | PandocAppError String
66+ | PandocEpubSubdirectoryError String
6667 deriving (Show, Typeable, Generic)
6768
6869instance Exception PandocError
@@ -104,6 +105,8 @@ handleError (Left e) =
104105 "File " ++ fn ++ " not found in resource path"
105106 PandocTemplateError s -> err 5 s
106107 PandocAppError s -> err 1 s
108+ PandocEpubSubdirectoryError s -> err 31 $
109+ "EPUB subdirectory name '" ++ s ++ "' contains illegal characters"
107110
108111err :: Int -> String -> IO a
109112err exitCode msg = do
src/Text/Pandoc/Options.hs+2
@@ -213,6 +213,7 @@ data WriterOptions = WriterOptions
213213 , writerHighlightStyle :: Maybe Style -- ^ Style to use for highlighting
214214 -- (Nothing = no highlighting)
215215 , writerSetextHeaders :: Bool -- ^ Use setext headers for levels 1-2 in markdown
216+ , writerEpubSubdirectory :: String -- ^ Subdir for epub in OCF
216217 , writerEpubMetadata :: Maybe String -- ^ Metadata to include in EPUB
217218 , writerEpubFonts :: [FilePath] -- ^ Paths to fonts to embed
218219 , writerEpubChapterLevel :: Int -- ^ Header level for chapters (separate files)
@@ -249,6 +250,7 @@ instance Default WriterOptions where
249250 , writerListings = False
250251 , writerHighlightStyle = Just pygments
251252 , writerSetextHeaders = True
253+ , writerEpubSubdirectory = "EPUB"
252254 , writerEpubMetadata = Nothing
253255 , writerEpubFonts = []
254256 , writerEpubChapterLevel = 1
src/Text/Pandoc/Writers/EPUB.hs+8−8
@@ -34,14 +34,14 @@ Conversion of 'Pandoc' documents to EPUB.
3434module Text.Pandoc.Writers.EPUB ( writeEPUB2, writeEPUB3 ) where
3535import Codec.Archive.Zip (Entry, addEntryToArchive, eRelativePath, emptyArchive,
3636 fromArchive, fromEntry, toEntry)
37−import Control.Monad (mplus, when, zipWithM)
37+import Control.Monad (mplus, when, unless, zipWithM)
3838import Control.Monad.Except (catchError, throwError)
3939import Control.Monad.State.Strict (State, StateT, evalState, evalStateT, get, gets,
4040 lift, modify, put)
4141import qualified Data.ByteString.Lazy as B
4242import qualified Data.ByteString.Lazy.Char8 as B8
4343import qualified Data.Text.Lazy as TL
44−import Data.Char (isAlphaNum, isDigit, toLower)
44+import Data.Char (isAlphaNum, isDigit, toLower, isAscii)
4545import Data.List (intercalate, isInfixOf, isPrefixOf)
4646import qualified Data.Map as M
4747import Data.Maybe (catMaybes, fromMaybe)
@@ -80,7 +80,6 @@ data Chapter = Chapter (Maybe [Int]) [Block]
8080
8181data EPUBState = EPUBState {
8282 stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
83− , stEPUBSubdir :: String
8483 }
8584
8685type E m = StateT EPUBState m
@@ -362,9 +361,7 @@ writeEPUB :: PandocMonad m
362361 -> Pandoc -- ^ Document to convert
363362 -> m B.ByteString
364363writeEPUB epubVersion opts doc =
365− let initState = EPUBState { stMediaPaths = []
366− , stEPUBSubdir = "EPUB"
367− }
364+ let initState = EPUBState { stMediaPaths = [] }
368365 in
369366 evalStateT (pandocToEPUB epubVersion opts doc)
370367 initState
@@ -375,7 +372,10 @@ pandocToEPUB :: PandocMonad m
375372 -> Pandoc
376373 -> E m B.ByteString
377374pandocToEPUB 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
379379 let epub3 = version == EPUB3
380380 let writeHtml o = fmap (UTF8.fromTextLazy . TL.fromStrict) .
381381 writeHtmlStringForEPUB version o
@@ -888,7 +888,7 @@ modifyMediaRef :: PandocMonad m
888888modifyMediaRef _ "" = return ""
889889modifyMediaRef opts oldsrc = do
890890 media <- gets stMediaPaths
891− epubSubdir <- gets stEPUBSubdir
891+ let epubSubdir = writerEpubSubdirectory opts
892892 case lookup oldsrc media of
893893 Just (n,_) -> return n
894894 Nothing -> catchError