git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: properly handle internal links to IDs in spans, divs.

Closes #1884.

commit25e12ca7b20bc48fc5ff866a8427b08df7c1c71a authorJohn MacFarlane <jgm@berkeley.edu> dateSat 17 Jan 2015, 11:27 (12 years ago) parentc63020d
1 file changed+18−11
src/Text/Pandoc/Writers/EPUB.hs+18−11
src/Text/Pandoc/Writers/EPUB.hs+18−11
@@ -41,7 +41,7 @@ import qualified Data.ByteString.Lazy.Char8 as B8
4141import qualified Text.Pandoc.UTF8 as UTF8
4242import Text.Pandoc.SelfContained ( makeSelfContained )
4343import Codec.Archive.Zip ( emptyArchive, addEntryToArchive, eRelativePath, fromEntry , Entry, toEntry, fromArchive)
44−import Control.Applicative ((<$>))
44+import Control.Applicative ((<$>), (<$))
4545import Data.Time.Clock.POSIX ( getPOSIXTime )
4646import Data.Time (getCurrentTime,UTCTime, formatTime)
4747import Text.Pandoc.Compat.Locale ( defaultTimeLocale )
@@ -57,7 +57,7 @@ import Text.Pandoc.Options ( WriterOptions(..)
5757import Text.Pandoc.Definition
5858import Text.Pandoc.Walk (walk, walkM)
5959import Control.Monad.State (modify, get, execState, State, put, evalState)
60−import Control.Monad (foldM, when, mplus, liftM)
60+import Control.Monad (foldM, mplus, liftM)
6161import Text.XML.Light ( unode, Element(..), unqual, Attr(..), add_attrs
6262 , strContent, lookupAttr, Node(..), QName(..), parseXML
6363 , onlyElems, node, ppElement)
@@ -894,20 +894,27 @@ addIdentifiers bs = evalState (mapM go bs) []
894894-- was "header-1" might turn into "ch006.xhtml#header".
895895correlateRefs :: Int -> [Block] -> [(String,String)]
896896correlateRefs chapterHeaderLevel bs =
897− identTable $ execState (mapM_ go bs)
897+ identTable $ execState (walkM goBlock bs >>= walkM goInline)
898898 IdentState{ chapterNumber = 0
899899 , identTable = [] }
900− where go :: Block -> State IdentState ()
901− go (Header n (ident,_,_) _) = do
902− when (n <= chapterHeaderLevel) $
903− modify $ \s -> s{ chapterNumber = chapterNumber s + 1 }
900+ where goBlock :: Block -> State IdentState Block
901+ goBlock x@(Header n (ident,_,_) _) = x <$ addIdentifier (Just n) ident
902+ goBlock x@(Div (ident,_,_) _) = x <$ addIdentifier Nothing ident
903+ goBlock x = return x
904+ goInline :: Inline -> State IdentState Inline
905+ goInline x@(Span (ident,_,_) _) = x <$ addIdentifier Nothing ident
906+ goInline x = return x
907+ addIdentifier mbHeaderLevel ident = do
908+ case mbHeaderLevel of
909+ Just n | n <= chapterHeaderLevel ->
910+ modify $ \s -> s{ chapterNumber = chapterNumber s + 1 }
911+ _ -> return ()
904912 st <- get
905913 let chapterid = showChapter (chapterNumber st) ++
906− if n <= chapterHeaderLevel
907− then ""
908− else '#' : ident
914+ case mbHeaderLevel of
915+ Just n | n <= chapterHeaderLevel -> ""
916+ _ -> '#' : ident
909917 modify $ \s -> s{ identTable = (ident, chapterid) : identTable st }
910− go _ = return ()
911918
912919-- Replace internal link references using the table produced
913920-- by correlateRefs.