git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: Use ch001, ch002, etc. for chapter filenames.

This improves sorting of chapters in some readers, which apparently sort ch2 after ch10. Closes #610.

commit880af865561ceba35944bf166b486b99dda08c0d authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 13 Sep 2012, 08:58 (14 years ago) parent93fa5df
1 file changed+12−5
src/Text/Pandoc/Writers/EPUB.hs+12−5
src/Text/Pandoc/Writers/EPUB.hs+12−5
@@ -32,6 +32,7 @@ import Data.IORef
3232import Data.Maybe ( fromMaybe, isNothing )
3333import Data.List ( findIndices, isPrefixOf )
3434import System.Environment ( getEnv )
35+import Text.Printf (printf)
3536import System.FilePath ( (</>), (<.>), takeBaseName, takeExtension, takeFileName )
3637import qualified Data.ByteString.Lazy as B
3738import Data.ByteString.Lazy.UTF8 ( fromString )
@@ -122,8 +123,9 @@ writeEPUB opts doc@(Pandoc meta _) = do
122123 let chapters = map titleize chunks
123124 let chapToHtml = writeHtmlString opts'{ writerTemplate = pageTemplate }
124125 let chapterToEntry :: Int -> Pandoc -> Entry
125− chapterToEntry num chap = mkEntry ("ch" ++ show num ++ ".xhtml") $
126− fromString $ chapToHtml chap
126+ chapterToEntry num chap = mkEntry
127+ (showChapter num) $
128+ fromString $ chapToHtml chap
127129 let chapterEntries = zipWith chapterToEntry [1..] chapters
128130
129131 -- contents.opf
@@ -334,11 +336,15 @@ data IdentState = IdentState{
334336 identTable :: [(String,String)]
335337 } deriving (Read, Show)
336338
339+-- Returns filename for chapter number.
340+showChapter :: Int -> String
341+showChapter = printf "ch%03d.xhtml"
342+
337343-- Go through a block list and construct a table
338344-- correlating the automatically constructed references
339345-- that would be used in a normal pandoc document with
340346-- 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".
342348correlateRefs :: [Block] -> [(String,String)]
343349correlateRefs bs = identTable $ execState (mapM_ go bs)
344350 IdentState{ chapterNumber = 0
@@ -358,8 +364,9 @@ correlateRefs bs = identTable $ execState (mapM_ go bs)
358364 modify $ \s -> s{ runningIdents = runningid : runningIdents st
359365 , chapterIdents = maybe (chapterIdents st)
360366 (: chapterIdents st) chapid
361− , identTable = (runningid, "ch" ++ show (chapterNumber st) ++
362− ".xhtml" ++ maybe "" ('#':) chapid) : identTable st
367+ , identTable = (runningid,
368+ showChapter (chapterNumber st) ++
369+ maybe "" ('#':) chapid) : identTable st
363370 }
364371 go _ = return ()
365372