git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: Use ch001, ch002, etc. for chapter filenames.
880af86John MacFarlane
src/Text/Pandoc/Writers/EPUB.hs+12−5
@@ -32,6 +32,7 @@ import Data.IORef
32import Data.Maybe ( fromMaybe, isNothing )
32import Data.Maybe ( fromMaybe, isNothing )
33import Data.List ( findIndices, isPrefixOf )
33import Data.List ( findIndices, isPrefixOf )
34import System.Environment ( getEnv )
34import System.Environment ( getEnv )
35+import Text.Printf (printf)
35import System.FilePath ( (</>), (<.>), takeBaseName, takeExtension, takeFileName )
36import System.FilePath ( (</>), (<.>), takeBaseName, takeExtension, takeFileName )
36import qualified Data.ByteString.Lazy as B
37import qualified Data.ByteString.Lazy as B
37import Data.ByteString.Lazy.UTF8 ( fromString )
38import Data.ByteString.Lazy.UTF8 ( fromString )
@@ -122,8 +123,9 @@ writeEPUB opts doc@(Pandoc meta _) = do
122 let chapters = map titleize chunks
123 let chapters = map titleize chunks
123 let chapToHtml = writeHtmlString opts'{ writerTemplate = pageTemplate }
124 let chapToHtml = writeHtmlString opts'{ writerTemplate = pageTemplate }
124 let chapterToEntry :: Int -> Pandoc -> Entry
125 let chapterToEntry :: Int -> Pandoc -> Entry
125− chapterToEntry num chap = mkEntry ("ch" ++ show num ++ ".xhtml") $
126+ chapterToEntry num chap = mkEntry
127+ (showChapter num) $
126− fromString $ chapToHtml chap
128+ fromString $ chapToHtml chap
127 let chapterEntries = zipWith chapterToEntry [1..] chapters
129 let chapterEntries = zipWith chapterToEntry [1..] chapters
128
130
129 -- contents.opf
131 -- contents.opf
@@ -334,11 +336,15 @@ data IdentState = IdentState{
334 identTable :: [(String,String)]
336 identTable :: [(String,String)]
335 } deriving (Read, Show)
337 } deriving (Read, Show)
336
338
339+-- Returns filename for chapter number.
340+showChapter :: Int -> String
341+showChapter = printf "ch%03d.xhtml"
342+
337-- Go through a block list and construct a table
343-- Go through a block list and construct a table
338-- correlating the automatically constructed references
344-- correlating the automatically constructed references
339-- that would be used in a normal pandoc document with
345-- that would be used in a normal pandoc document with
340-- new URLs to be used in the EPUB. For example, what
346-- new URLs to be used in the EPUB. For example, what
341−-- was "header-1" might turn into "ch6.xhtml#header".
347+-- was "header-1" might turn into "ch006.xhtml#header".
342correlateRefs :: [Block] -> [(String,String)]
348correlateRefs :: [Block] -> [(String,String)]
343correlateRefs bs = identTable $ execState (mapM_ go bs)
349correlateRefs bs = identTable $ execState (mapM_ go bs)
344 IdentState{ chapterNumber = 0
350 IdentState{ chapterNumber = 0
@@ -358,8 +364,9 @@ correlateRefs bs = identTable $ execState (mapM_ go bs)
358 modify $ \s -> s{ runningIdents = runningid : runningIdents st
364 modify $ \s -> s{ runningIdents = runningid : runningIdents st
359 , chapterIdents = maybe (chapterIdents st)
365 , chapterIdents = maybe (chapterIdents st)
360 (: chapterIdents st) chapid
366 (: chapterIdents st) chapid
361− , identTable = (runningid, "ch" ++ show (chapterNumber st) ++
367+ , identTable = (runningid,
368+ showChapter (chapterNumber st) ++
362− ".xhtml" ++ maybe "" ('#':) chapid) : identTable st
369+ maybe "" ('#':) chapid) : identTable st
363 }
370 }
364 go _ = return ()
371 go _ = return ()
365
372