git.iko.soy/typeable/pandoc
on master help sign in
Change default EPUB directory structure in OCF container.

See #3720. We now put all EPUB related content in an EPUB/ subdirectory by default (later this will be configurable). mimetype META-INF/ com.apple.ibooks.display-options.xml container.xml EPUB/ <<--configurable-->> fonts/ <<--static-->> font.otf media/ <<--static-->> cover.jpg fig1.jpg styles/ <<--static-->> stylesheet.css content.opf toc.ncx text/ <<--static-->> ch001.xhtml

commit242e2a064f6a32b22e1599bbfe72e64d7b6203b8 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 21 Jun 2017, 23:54 (9 years ago) parent6e6324b
1 file changed+22−11
src/Text/Pandoc/Writers/EPUB.hs+22−11
src/Text/Pandoc/Writers/EPUB.hs+22−11
@@ -80,6 +80,7 @@ data Chapter = Chapter (Maybe [Int]) [Block]
8080
8181data EPUBState = EPUBState {
8282 stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
83+ , stEPUBSubdir :: String
8384 }
8485
8586type E m = StateT EPUBState m
@@ -362,6 +363,7 @@ writeEPUB :: PandocMonad m
362363 -> m B.ByteString
363364writeEPUB epubVersion opts doc =
364365 let initState = EPUBState { stMediaPaths = []
366+ , stEPUBSubdir = "EPUB"
365367 }
366368 in
367369 evalStateT (pandocToEPUB epubVersion opts doc)
@@ -373,6 +375,7 @@ pandocToEPUB :: PandocMonad m
373375 -> Pandoc
374376 -> E m B.ByteString
375377pandocToEPUB version opts doc@(Pandoc meta _) = do
378+ epubSubdir <- gets stEPUBSubdir
376379 let epub3 = version == EPUB3
377380 let writeHtml o = fmap (UTF8.fromTextLazy . TL.fromStrict) .
378381 writeHtmlStringForEPUB version o
@@ -383,10 +386,11 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
383386 -- stylesheet
384387 stylesheets <- case epubStylesheets metadata of
385388 [] -> (\x -> [B.fromChunks [x]]) <$>
386− P.readDataFile (writerUserDataDir opts) "epub.css"
389+ P.readDataFile (writerUserDataDir opts)
390+ "epub.css"
387391 fs -> mapM P.readFileLazy fs
388392 let stylesheetEntries = zipWith
389− (\bs n -> mkEntry ("stylesheet" ++ show n ++ ".css") bs)
393+ (\bs n -> mkEntry ("styles/stylesheet" ++ show n ++ ".css") bs)
390394 stylesheets [(1 :: Int)..]
391395
392396 let vars = ("epub3", if epub3 then "true" else "false")
@@ -431,7 +435,8 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
431435 when (null xs) $
432436 report $ CouldNotFetchResource f "glob did not match any font files"
433437 return xs
434− let mkFontEntry f = mkEntry (takeFileName f) `fmap` (lift $ P.readFileLazy f)
438+ let mkFontEntry f = mkEntry ("fonts/" ++ takeFileName f) <$>
439+ lift (P.readFileLazy f)
435440 fontFiles <- concat <$> mapM matchingGlob (writerEpubFonts opts')
436441 fontEntries <- mapM mkFontEntry fontFiles
437442
@@ -728,7 +733,8 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
728733 unode "container" ! [("version","1.0")
729734 ,("xmlns","urn:oasis:names:tc:opendocument:xmlns:container")] $
730735 unode "rootfiles" $
731− unode "rootfile" ! [("full-path","content.opf")
736+ unode "rootfile" ! [("full-path",
737+ epubSubdir ++ ['/' | not (null epubSubdir)] ++ "content.opf")
732738 ,("media-type","application/oebps-package+xml")] $ ()
733739 let containerEntry = mkEntry "META-INF/container.xml" containerData
734740
@@ -739,10 +745,14 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
739745 unode "option" ! [("name","specified-fonts")] $ "true"
740746 let appleEntry = mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
741747
748+ let addEpubSubdir :: Entry -> Entry
749+ addEpubSubdir e = e{ eRelativePath =
750+ epubSubdir ++ ['/' | not (null epubSubdir)] ++ eRelativePath e }
742751 -- construct archive
743− let archive = foldr addEntryToArchive emptyArchive
744− (mimetypeEntry : containerEntry : appleEntry : tpEntry :
745− contentsEntry : tocEntry : navEntry :
752+ let archive = foldr addEntryToArchive emptyArchive $
753+ [mimetypeEntry, containerEntry, appleEntry] ++
754+ map addEpubSubdir
755+ (tpEntry : contentsEntry : tocEntry : navEntry :
746756 (stylesheetEntries ++ picEntries ++ cpicEntry ++
747757 cpgEntry ++ chapterEntries ++ fontEntries))
748758 return $ fromArchive archive
@@ -878,15 +888,16 @@ modifyMediaRef :: PandocMonad m
878888modifyMediaRef _ "" = return ""
879889modifyMediaRef opts oldsrc = do
880890 media <- gets stMediaPaths
891+ epubSubdir <- gets stEPUBSubdir
881892 case lookup oldsrc media of
882893 Just (n,_) -> return n
883894 Nothing -> catchError
884895 (do (img, mbMime) <- P.fetchItem (writerSourceURL opts) oldsrc
885896 let new = "media/file" ++ show (length media) ++
886− fromMaybe (takeExtension (takeWhile (/='?') oldsrc))
887− (('.':) <$> (mbMime >>= extensionFromMimeType))
897+ fromMaybe (takeExtension (takeWhile (/='?') oldsrc))
898+ (('.':) <$> (mbMime >>= extensionFromMimeType))
888899 epochtime <- floor `fmap` lift P.getPOSIXTime
889− let entry = toEntry new epochtime $ B.fromChunks . (:[]) $ img
900+ let entry = toEntry new epochtime (B.fromChunks . (:[]) $ img)
890901 modify $ \st -> st{ stMediaPaths =
891902 (oldsrc, (new, Just entry)):media}
892903 return new)
@@ -952,7 +963,7 @@ mediaTypeOf x =
952963
953964-- Returns filename for chapter number.
954965showChapter :: Int -> String
955−showChapter = printf "ch%03d.xhtml"
966+showChapter = printf "text/ch%03d.xhtml"
956967
957968-- Add identifiers to any headers without them.
958969addIdentifiers :: [Block] -> [Block]