diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2021-12-29 15:46:11 +0200 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2021-12-29 15:46:11 +0200 |
commit | 004d06cfdca6b15364f39d632daf87675ce18b8d (patch) | |
tree | 3eb6c19418d10da347761a26d444657a3fee059e | |
parent | b4361712899fd0183fea5513180cb383979616de (diff) | |
download | pandoc-004d06cfdca6b15364f39d632daf87675ce18b8d.tar.gz |
LaTeX->HTML: Automatically generate the TOC
-rw-r--r-- | src/Text/Pandoc/Writers/HTML.hs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index 8c5548196..7cc6fef49 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -28,6 +28,7 @@ module Text.Pandoc.Writers.HTML ( writeRevealJs, tagWithAttributes ) where +import Control.Monad.Identity (runIdentity) import Control.Monad.State.Strict import Data.Char (ord) import Data.List (intercalate, intersperse, partition, delete, (\\), foldl') @@ -50,7 +51,7 @@ import Text.Pandoc.ImageSize import Text.Pandoc.Options import Text.Pandoc.Shared import Text.Pandoc.Slides -import Text.Pandoc.Templates (renderTemplate) +import Text.Pandoc.Templates (Template, compileTemplate, renderTemplate) import Text.Pandoc.Walk import Text.Pandoc.Writers.Math import Text.Pandoc.Writers.Shared @@ -207,13 +208,15 @@ writeHtmlString' :: PandocMonad m => WriterState -> WriterOptions -> Pandoc -> m Text writeHtmlString' st opts d = do (body, context) <- evalStateT (pandocToHtml opts d) st + let defaultTemplate = fmap (const tocTemplate) (getField "table-of-contents" context :: Maybe Text) + let template = msum [ writerTemplate opts, defaultTemplate ] let colwidth = case writerWrapText opts of WrapAuto -> Just (writerColumns opts) _ -> Nothing (if writerPreferAscii opts then toEntities else id) <$> - case writerTemplate opts of + case template of Nothing -> return $ case colwidth of Nothing -> renderHtml' body -- optimization, skip layout @@ -249,6 +252,13 @@ writeHtml' st opts d = (body, _) <- evalStateT (pandocToHtml opts d) st return body +wantTOC :: Meta -> Maybe Bool +wantTOC = fmap (== MetaBool True) . lookupMeta "tableOfContents" + +tocTemplate :: Template Text +tocTemplate = either error id . runIdentity . compileTemplate "" $ + "<div class=\"toc\"><h1></h1>$table-of-contents$</div>$body$" + -- result is (title, authors, date, toc, body, new variables) pandocToHtml :: PandocMonad m => WriterOptions @@ -272,7 +282,8 @@ pandocToHtml opts (Pandoc meta blocks) = do if slideVariant == NoSlides then blocks else prepSlides slideLevel blocks - toc <- if writerTableOfContents opts && slideVariant /= S5Slides + let withTOC = fromMaybe (writerTableOfContents opts) (wantTOC meta) + toc <- if withTOC && slideVariant /= S5Slides then fmap layoutMarkup <$> tableOfContents opts sects else return Nothing blocks' <- blockListToHtml opts sects |