@@ -38,7 +38,7 @@ import Text.Printf (printf)
3838import System.FilePath ( (</>), takeBaseName, takeExtension, takeFileName )
3939import qualified Data.ByteString.Lazy as B
4040import qualified Data.ByteString.Lazy.Char8 as B8
41−import Text.Pandoc.UTF8 ( fromStringLazy, toString )
41+import qualified Text.Pandoc.UTF8 as UTF8
4242import Text.Pandoc.SelfContained ( makeSelfContained )
4343import Codec.Archive.Zip
4444import Control.Applicative ((<$>))
@@ -89,8 +89,13 @@ data EPUBMetadata = EPUBMetadata{
8989 , epubCoverage :: Maybe String
9090 , epubRights :: Maybe String
9191 , epubCoverImage :: Maybe String
92+ , epubStylesheet :: Maybe Stylesheet
9293 } deriving Show
9394
95+data Stylesheet = StylesheetPath FilePath
96+ | StylesheetContents String
97+ deriving Show
98+
9499data Creator = Creator{
95100 creatorText :: String
96101 , creatorRole :: Maybe String
@@ -262,6 +267,7 @@ metadataFromMeta opts meta = EPUBMetadata{
262267 , epubCoverage = coverage
263268 , epubRights = rights
264269 , epubCoverImage = coverImage
270+ , epubStylesheet = stylesheet
265271 }
266272 where identifiers = getIdentifier meta
267273 titles = getTitle meta
@@ -282,6 +288,10 @@ metadataFromMeta opts meta = EPUBMetadata{
282288 rights = metaValueToString <$> lookupMeta "rights" meta
283289 coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus`
284290 (metaValueToString <$> lookupMeta "cover-image" meta)
291+ stylesheet = (StylesheetContents <$>
292+ lookup "epub-stylesheet" (writerVariables opts)) `mplus`
293+ ((StylesheetPath . metaValueToString) <$>
294+ lookupMeta "stylesheet" meta)
285295
286296-- | Produce an EPUB file from a Pandoc document.
287297writeEPUB :: WriterOptions -- ^ Writer options
@@ -437,7 +447,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
437447 (x:_) -> identifierText x -- use first identifier as UUID
438448 [] -> error "epubIdentifier is null" -- shouldn't happen
439449 currentTime <- getCurrentTime
440− let contentsData = fromStringLazy $ ppTopElement $
450+ let contentsData = UTF8.fromStringLazy $ ppTopElement $
441451 unode "package" ! [("version", case version of
442452 EPUB2 -> "2.0"
443453 EPUB3 -> "3.0")
@@ -525,7 +535,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
525535 [ unode "navLabel" $ unode "text" (plainify $ docTitle meta)
526536 , unode "content" ! [("src","title_page.xhtml")] $ () ]
527537
528− let tocData = fromStringLazy $ ppTopElement $
538+ let tocData = UTF8.fromStringLazy $ ppTopElement $
529539 unode "ncx" ! [("version","2005-1")
530540 ,("xmlns","http://www.daisy.org/z3986/2005/ncx/")] $
531541 [ unode "head" $
@@ -557,7 +567,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
557567 (_:_) -> [unode "ol" ! [("class","toc")] $ subs]
558568
559569 let navtag = if epub3 then "nav" else "div"
560− let navData = fromStringLazy $ ppTopElement $
570+ let navData = UTF8.fromStringLazy $ ppTopElement $
561571 unode "html" ! [("xmlns","http://www.w3.org/1999/xhtml")
562572 ,("xmlns:epub","http://www.idpf.org/2007/ops")] $
563573 [ unode "head" $
@@ -571,10 +581,10 @@ writeEPUB opts doc@(Pandoc meta _) = do
571581 let navEntry = mkEntry "nav.xhtml" navData
572582
573583 -- mimetype
574− let mimetypeEntry = mkEntry "mimetype" $ fromStringLazy "application/epub+zip"
584+ let mimetypeEntry = mkEntry "mimetype" $ UTF8.fromStringLazy "application/epub+zip"
575585
576586 -- container.xml
577− let containerData = fromStringLazy $ ppTopElement $
587+ let containerData = UTF8.fromStringLazy $ ppTopElement $
578588 unode "container" ! [("version","1.0")
579589 ,("xmlns","urn:oasis:names:tc:opendocument:xmlns:container")] $
580590 unode "rootfiles" $
@@ -583,18 +593,19 @@ writeEPUB opts doc@(Pandoc meta _) = do
583593 let containerEntry = mkEntry "META-INF/container.xml" containerData
584594
585595 -- com.apple.ibooks.display-options.xml
586− let apple = fromStringLazy $ ppTopElement $
596+ let apple = UTF8.fromStringLazy $ ppTopElement $
587597 unode "display_options" $
588598 unode "platform" ! [("name","*")] $
589599 unode "option" ! [("name","specified-fonts")] $ "true"
590600 let appleEntry = mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
591601
592602 -- stylesheet
593− stylesheet <- case writerEpubStylesheet opts of
594− Just s -> return s
595− Nothing -> toString `fmap`
603+ stylesheet <- case epubStylesheet metadata of
604+ Just (StylesheetPath fp) -> UTF8.readFile fp
605+ Just (StylesheetContents s) -> return s
606+ Nothing -> UTF8.toString `fmap`
596607 readDataFile (writerUserDataDir opts) "epub.css"
597− let stylesheetEntry = mkEntry "stylesheet.css" $ fromStringLazy stylesheet
608+ let stylesheetEntry = mkEntry "stylesheet.css" $ UTF8.fromStringLazy stylesheet
598609
599610 -- construct archive
600611 let archive = foldr addEntryToArchive emptyArchive