git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: Allow 'stylesheet' in metadata.
bd96f2bJohn MacFarlane
@@ -2822,6 +2822,8 @@ The following fields are recognized:
2822 ~ A string value.
2822 ~ A string value.
2823`cover-image`
2823`cover-image`
2824 ~ A string value (path to cover image).
2824 ~ A string value (path to cover image).
2825+`stylesheet`
2826+ ~ A string value (path to CSS stylesheet).
2825
2827
2826Literate Haskell support
2828Literate Haskell support
2827========================
2829========================
src/Text/Pandoc/Writers/EPUB.hs+22−11
@@ -38,7 +38,7 @@ import Text.Printf (printf)
38import System.FilePath ( (</>), takeBaseName, takeExtension, takeFileName )
38import System.FilePath ( (</>), takeBaseName, takeExtension, takeFileName )
39import qualified Data.ByteString.Lazy as B
39import qualified Data.ByteString.Lazy as B
40import qualified Data.ByteString.Lazy.Char8 as B8
40import qualified Data.ByteString.Lazy.Char8 as B8
41−import Text.Pandoc.UTF8 ( fromStringLazy, toString )
41+import qualified Text.Pandoc.UTF8 as UTF8
42import Text.Pandoc.SelfContained ( makeSelfContained )
42import Text.Pandoc.SelfContained ( makeSelfContained )
43import Codec.Archive.Zip
43import Codec.Archive.Zip
44import Control.Applicative ((<$>))
44import Control.Applicative ((<$>))
@@ -89,8 +89,13 @@ data EPUBMetadata = EPUBMetadata{
89 , epubCoverage :: Maybe String
89 , epubCoverage :: Maybe String
90 , epubRights :: Maybe String
90 , epubRights :: Maybe String
91 , epubCoverImage :: Maybe String
91 , epubCoverImage :: Maybe String
92+ , epubStylesheet :: Maybe Stylesheet
92 } deriving Show
93 } deriving Show
93
94
95+data Stylesheet = StylesheetPath FilePath
96+ | StylesheetContents String
97+ deriving Show
98+
94data Creator = Creator{
99data Creator = Creator{
95 creatorText :: String
100 creatorText :: String
96 , creatorRole :: Maybe String
101 , creatorRole :: Maybe String
@@ -262,6 +267,7 @@ metadataFromMeta opts meta = EPUBMetadata{
262 , epubCoverage = coverage
267 , epubCoverage = coverage
263 , epubRights = rights
268 , epubRights = rights
264 , epubCoverImage = coverImage
269 , epubCoverImage = coverImage
270+ , epubStylesheet = stylesheet
265 }
271 }
266 where identifiers = getIdentifier meta
272 where identifiers = getIdentifier meta
267 titles = getTitle meta
273 titles = getTitle meta
@@ -282,6 +288,10 @@ metadataFromMeta opts meta = EPUBMetadata{
282 rights = metaValueToString <$> lookupMeta "rights" meta
288 rights = metaValueToString <$> lookupMeta "rights" meta
283 coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus`
289 coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus`
284 (metaValueToString <$> lookupMeta "cover-image" meta)
290 (metaValueToString <$> lookupMeta "cover-image" meta)
291+ stylesheet = (StylesheetContents <$>
292+ lookup "epub-stylesheet" (writerVariables opts)) `mplus`
293+ ((StylesheetPath . metaValueToString) <$>
294+ lookupMeta "stylesheet" meta)
285
295
286-- | Produce an EPUB file from a Pandoc document.
296-- | Produce an EPUB file from a Pandoc document.
287writeEPUB :: WriterOptions -- ^ Writer options
297writeEPUB :: WriterOptions -- ^ Writer options
@@ -437,7 +447,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
437 (x:_) -> identifierText x -- use first identifier as UUID
447 (x:_) -> identifierText x -- use first identifier as UUID
438 [] -> error "epubIdentifier is null" -- shouldn't happen
448 [] -> error "epubIdentifier is null" -- shouldn't happen
439 currentTime <- getCurrentTime
449 currentTime <- getCurrentTime
440− let contentsData = fromStringLazy $ ppTopElement $
450+ let contentsData = UTF8.fromStringLazy $ ppTopElement $
441 unode "package" ! [("version", case version of
451 unode "package" ! [("version", case version of
442 EPUB2 -> "2.0"
452 EPUB2 -> "2.0"
443 EPUB3 -> "3.0")
453 EPUB3 -> "3.0")
@@ -525,7 +535,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
525 [ unode "navLabel" $ unode "text" (plainify $ docTitle meta)
535 [ unode "navLabel" $ unode "text" (plainify $ docTitle meta)
526 , unode "content" ! [("src","title_page.xhtml")] $ () ]
536 , unode "content" ! [("src","title_page.xhtml")] $ () ]
527
537
528− let tocData = fromStringLazy $ ppTopElement $
538+ let tocData = UTF8.fromStringLazy $ ppTopElement $
529 unode "ncx" ! [("version","2005-1")
539 unode "ncx" ! [("version","2005-1")
530 ,("xmlns","http://www.daisy.org/z3986/2005/ncx/")] $
540 ,("xmlns","http://www.daisy.org/z3986/2005/ncx/")] $
531 [ unode "head" $
541 [ unode "head" $
@@ -557,7 +567,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
557 (_:_) -> [unode "ol" ! [("class","toc")] $ subs]
567 (_:_) -> [unode "ol" ! [("class","toc")] $ subs]
558
568
559 let navtag = if epub3 then "nav" else "div"
569 let navtag = if epub3 then "nav" else "div"
560− let navData = fromStringLazy $ ppTopElement $
570+ let navData = UTF8.fromStringLazy $ ppTopElement $
561 unode "html" ! [("xmlns","http://www.w3.org/1999/xhtml")
571 unode "html" ! [("xmlns","http://www.w3.org/1999/xhtml")
562 ,("xmlns:epub","http://www.idpf.org/2007/ops")] $
572 ,("xmlns:epub","http://www.idpf.org/2007/ops")] $
563 [ unode "head" $
573 [ unode "head" $
@@ -571,10 +581,10 @@ writeEPUB opts doc@(Pandoc meta _) = do
571 let navEntry = mkEntry "nav.xhtml" navData
581 let navEntry = mkEntry "nav.xhtml" navData
572
582
573 -- mimetype
583 -- mimetype
574− let mimetypeEntry = mkEntry "mimetype" $ fromStringLazy "application/epub+zip"
584+ let mimetypeEntry = mkEntry "mimetype" $ UTF8.fromStringLazy "application/epub+zip"
575
585
576 -- container.xml
586 -- container.xml
577− let containerData = fromStringLazy $ ppTopElement $
587+ let containerData = UTF8.fromStringLazy $ ppTopElement $
578 unode "container" ! [("version","1.0")
588 unode "container" ! [("version","1.0")
579 ,("xmlns","urn:oasis:names:tc:opendocument:xmlns:container")] $
589 ,("xmlns","urn:oasis:names:tc:opendocument:xmlns:container")] $
580 unode "rootfiles" $
590 unode "rootfiles" $
@@ -583,18 +593,19 @@ writeEPUB opts doc@(Pandoc meta _) = do
583 let containerEntry = mkEntry "META-INF/container.xml" containerData
593 let containerEntry = mkEntry "META-INF/container.xml" containerData
584
594
585 -- com.apple.ibooks.display-options.xml
595 -- com.apple.ibooks.display-options.xml
586− let apple = fromStringLazy $ ppTopElement $
596+ let apple = UTF8.fromStringLazy $ ppTopElement $
587 unode "display_options" $
597 unode "display_options" $
588 unode "platform" ! [("name","*")] $
598 unode "platform" ! [("name","*")] $
589 unode "option" ! [("name","specified-fonts")] $ "true"
599 unode "option" ! [("name","specified-fonts")] $ "true"
590 let appleEntry = mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
600 let appleEntry = mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
591
601
592 -- stylesheet
602 -- stylesheet
593− stylesheet <- case writerEpubStylesheet opts of
603+ stylesheet <- case epubStylesheet metadata of
594− Just s -> return s
604+ Just (StylesheetPath fp) -> UTF8.readFile fp
605+ Just (StylesheetContents s) -> return s
595− Nothing -> toString `fmap`
606+ Nothing -> UTF8.toString `fmap`
596 readDataFile (writerUserDataDir opts) "epub.css"
607 readDataFile (writerUserDataDir opts) "epub.css"
597− let stylesheetEntry = mkEntry "stylesheet.css" $ fromStringLazy stylesheet
608+ let stylesheetEntry = mkEntry "stylesheet.css" $ UTF8.fromStringLazy stylesheet
598
609
599 -- construct archive
610 -- construct archive
600 let archive = foldr addEntryToArchive emptyArchive
611 let archive = foldr addEntryToArchive emptyArchive