summaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <fiddlosopher@gmail.com>2012-09-13 08:58:29 -0700
committerJohn MacFarlane <fiddlosopher@gmail.com>2012-09-13 08:59:33 -0700
commit880af865561ceba35944bf166b486b99dda08c0d (patch)
treece254fa43e6f076fc2b4254033299c986366997b /src/Text
parent93fa5df86609da43132807a46ef655bb5d7ba4f9 (diff)
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.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Writers/EPUB.hs17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs
index b6527c6c8..6f8931caa 100644
--- a/src/Text/Pandoc/Writers/EPUB.hs
+++ b/src/Text/Pandoc/Writers/EPUB.hs
@@ -32,6 +32,7 @@ import Data.IORef
import Data.Maybe ( fromMaybe, isNothing )
import Data.List ( findIndices, isPrefixOf )
import System.Environment ( getEnv )
+import Text.Printf (printf)
import System.FilePath ( (</>), (<.>), takeBaseName, takeExtension, takeFileName )
import qualified Data.ByteString.Lazy as B
import Data.ByteString.Lazy.UTF8 ( fromString )
@@ -122,8 +123,9 @@ writeEPUB opts doc@(Pandoc meta _) = do
let chapters = map titleize chunks
let chapToHtml = writeHtmlString opts'{ writerTemplate = pageTemplate }
let chapterToEntry :: Int -> Pandoc -> Entry
- chapterToEntry num chap = mkEntry ("ch" ++ show num ++ ".xhtml") $
- fromString $ chapToHtml chap
+ chapterToEntry num chap = mkEntry
+ (showChapter num) $
+ fromString $ chapToHtml chap
let chapterEntries = zipWith chapterToEntry [1..] chapters
-- contents.opf
@@ -334,11 +336,15 @@ data IdentState = IdentState{
identTable :: [(String,String)]
} deriving (Read, Show)
+-- Returns filename for chapter number.
+showChapter :: Int -> String
+showChapter = printf "ch%03d.xhtml"
+
-- Go through a block list and construct a table
-- correlating the automatically constructed references
-- that would be used in a normal pandoc document with
-- new URLs to be used in the EPUB. For example, what
--- was "header-1" might turn into "ch6.xhtml#header".
+-- was "header-1" might turn into "ch006.xhtml#header".
correlateRefs :: [Block] -> [(String,String)]
correlateRefs bs = identTable $ execState (mapM_ go bs)
IdentState{ chapterNumber = 0
@@ -358,8 +364,9 @@ correlateRefs bs = identTable $ execState (mapM_ go bs)
modify $ \s -> s{ runningIdents = runningid : runningIdents st
, chapterIdents = maybe (chapterIdents st)
(: chapterIdents st) chapid
- , identTable = (runningid, "ch" ++ show (chapterNumber st) ++
- ".xhtml" ++ maybe "" ('#':) chapid) : identTable st
+ , identTable = (runningid,
+ showChapter (chapterNumber st) ++
+ maybe "" ('#':) chapid) : identTable st
}
go _ = return ()