From 113479feb55e555a6a114cf6878f9a24c48b3347 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Thu, 22 Apr 2021 20:30:35 +0200 Subject: LaTeX->HTML: Automatically generate the TOC --- src/Text/Pandoc/Readers/LaTeX.hs | 1 + src/Text/Pandoc/Writers/HTML.hs | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index 493b8abac..7b942aed9 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -1549,6 +1549,7 @@ blockCommands = M.fromList <|> (grouped block >>= addMeta "title"))) , ("subtitle", mempty <$ (skipopts *> tok >>= addMeta "subtitle")) , ("author", mempty <$ (skipopts *> authors)) + , ("tableofcontents", mempty <$ (addMeta "tableOfContents" True)) -- -- in letter class, temp. store address & sig as title, author , ("address", mempty <$ (skipopts *> tok >>= addMeta "address")) , ("signature", mempty <$ (skipopts *> authors)) diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index c92131d5a..cba6b7d1c 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, (\\)) @@ -51,7 +52,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 @@ -203,10 +204,13 @@ 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 ] (if writerPreferAscii opts then toEntities else id) <$> - case writerTemplate opts of + case template of Nothing -> return $ renderHtml' body Just tpl -> do -- warn if empty lang @@ -239,6 +243,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 "" $ + "

$table-of-contents$
$body$" + -- result is (title, authors, date, toc, body, new variables) pandocToHtml :: PandocMonad m => WriterOptions @@ -260,7 +271,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 renderHtml' <$> tableOfContents opts sects else return Nothing blocks' <- blockListToHtml opts sects -- cgit v1.2.3