@@ -41,6 +41,7 @@ import Control.Monad.State.Strict (State, StateT, evalState, evalStateT, get, ge
4141import qualified Data.ByteString.Lazy as B
4242import qualified Data.ByteString.Lazy.Char8 as B8
4343import qualified Data.Text.Lazy as TL
44+import qualified Data.Text as TS
4445import Data.Char (isAlphaNum, isDigit, toLower, isAscii)
4546import Data.List (intercalate, isInfixOf, isPrefixOf)
4647import qualified Data.Map as M
@@ -70,7 +71,7 @@ import Text.Pandoc.Writers.HTML (writeHtmlStringForEPUB)
7071import Text.Printf (printf)
7172import Text.XML.Light (Attr (..), Element (..), Node (..), QName (..),
7273 add_attrs, lookupAttr, node, onlyElems, parseXML,
73− ppElement, strContent, unode, unqual)
74+ ppElement, strContent, unode, unqual, showElement)
7475
7576-- A Chapter includes a list of blocks and maybe a section
7677-- number offset. Note, some chapters are unnumbered. The section
@@ -635,17 +636,17 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
635636 let tocLevel = writerTOCDepth opts
636637
637638 let navPointNode :: PandocMonad m
638− => (Int -> String -> String -> [Element] -> Element)
639+ => (Int -> [Inline] -> String -> [Element] -> Element)
639640 -> S.Element -> StateT Int m Element
640641 navPointNode formatter (S.Sec _ nums (ident,_,_) ils children) = do
641642 n <- get
642643 modify (+1)
643644 let showNums :: [Int] -> String
644645 showNums = intercalate "." . map show
645− let tit' = stringify ils
646646 let tit = if writerNumberSections opts && not (null nums)
647− then showNums nums ++ " " ++ tit'
648− else tit'
647+ then Span ("", ["section-header-number"], [])
648+ [Str (showNums nums)] : Space : ils
649+ else ils
649650 src <- case lookup ident reftable of
650651 Just x -> return x
651652 Nothing -> throwError $ PandocSomeError $ ident ++ " not found in reftable"
@@ -656,10 +657,10 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
656657 return $ formatter n tit src subs
657658 navPointNode _ (S.Blk _) = throwError $ PandocSomeError "navPointNode encountered Blk"
658659
659− let navMapFormatter :: Int -> String -> String -> [Element] -> Element
660+ let navMapFormatter :: Int -> [Inline] -> String -> [Element] -> Element
660661 navMapFormatter n tit src subs = unode "navPoint" !
661662 [("id", "navPoint-" ++ show n)] $
662− [ unode "navLabel" $ unode "text" tit
663+ [ unode "navLabel" $ unode "text" $ stringify tit
663664 , unode "content" ! [("src", "text/" ++ src)] $ ()
664665 ] ++ subs
665666
@@ -690,19 +691,31 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
690691 ]
691692 let tocEntry = mkEntry "toc.ncx" tocData
692693
693− let navXhtmlFormatter :: Int -> String -> String -> [Element] -> Element
694+ let navXhtmlFormatter :: Int -> [Inline] -> String -> [Element] -> Element
694695 navXhtmlFormatter n tit src subs = unode "li" !
695696 [("id", "toc-li-" ++ show n)] $
696697 (unode "a" ! [("href", "text/" ++
697698 src)]
698− $ tit)
699+ $ titElements)
699700 : case subs of
700701 [] -> []
701702 (_:_) -> [unode "ol" ! [("class","toc")] $ subs]
703+ where titElements = parseXML titRendered
704+ titRendered = case P.runPure
705+ (writeHtmlStringForEPUB version
706+ opts{ writerTemplate = Nothing }
707+ (Pandoc nullMeta
708+ [Plain $ walk delink tit])) of
709+ Left _ -> TS.pack $ stringify tit
710+ Right x -> x
711+ -- can't have a element inside a...
712+ delink (Link _ ils _) = Span ("", [], []) ils
713+ delink x = x
702714
703715 let navtag = if epub3 then "nav" else "div"
704716 tocBlocks <- lift $ evalStateT (mapM (navPointNode navXhtmlFormatter) secs) 1
705− let navBlocks = [RawBlock (Format "html") $ ppElement $
717+ let navBlocks = [RawBlock (Format "html")
718+ $ showElement $ -- prettyprinting introduces bad spaces
706719 unode navtag ! ([("epub:type","toc") | epub3] ++
707720 [("id","toc")]) $
708721 [ unode "h1" ! [("id","toc-title")] $ tocTitle