summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2022-12-19 21:24:33 +0100
committerJohn MacFarlane <jgm@berkeley.edu>2023-01-15 10:47:54 -0800
commit89800c3f9ea056bb1747790db685e2d6c688ed7a (patch)
treedef430a6bef6f8aa68599527d03fa3c1c0b1b023 /src
parentca6d4bfa490349e7a4dfeee05119427a1c5ace6c (diff)
Chunks: export `tocToList`
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Chunks.hs29
-rw-r--r--src/Text/Pandoc/Writers/Shared.hs32
2 files changed, 30 insertions, 31 deletions
diff --git a/src/Text/Pandoc/Chunks.hs b/src/Text/Pandoc/Chunks.hs
index a21fbb26b..3d144f325 100644
--- a/src/Text/Pandoc/Chunks.hs
+++ b/src/Text/Pandoc/Chunks.hs
@@ -23,6 +23,7 @@ module Text.Pandoc.Chunks
, PathTemplate(..)
, splitIntoChunks
, toTOCTree
+ , tocToList
, SecInfo(..)
) where
import Text.Pandoc.Definition
@@ -372,3 +373,31 @@ toTOCTree' =
secLevel = chunkLevel c }
in Node secinfo (getNodes as) : getNodes bs
getNodes [] = []
+
+-- | Creates a TOC link to the respective document section.
+tocEntryToLink :: SecInfo -> [Inline]
+tocEntryToLink secinfo = headerLink
+ where
+ addNumber = case secNumber secinfo of
+ Just num -> (Span ("",["toc-section-number"],[])
+ [Str num] :) . (Space :)
+ Nothing -> id
+ clean (Link _ xs _) = xs
+ clean (Note _) = []
+ clean x = [x]
+ ident = secId secinfo
+ headerText = addNumber $ walk (concatMap clean) (secTitle secinfo)
+ headerLink = if T.null ident
+ then headerText
+ else [Link ("toc-" <> ident, [], [])
+ headerText (secPath secinfo <> "#" <> ident, "")]
+
+-- | Generate a table of contents of the given depth.
+tocToList :: Int -> Tree SecInfo -> Block
+tocToList tocDepth (Node _ subtrees)
+ = BulletList (toItems subtrees)
+ where
+ toItems = map go . filter isBelowTocDepth
+ isBelowTocDepth (Node sec _) = secLevel sec <= tocDepth
+ go (Node secinfo xs) =
+ Plain (tocEntryToLink secinfo) : [BulletList (toItems xs) | not (null xs)]
diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs
index c309df99c..723f55b8a 100644
--- a/src/Text/Pandoc/Writers/Shared.hs
+++ b/src/Text/Pandoc/Writers/Shared.hs
@@ -67,8 +67,7 @@ import qualified Text.Pandoc.UTF8 as UTF8
import Text.Pandoc.XML (escapeStringForXML)
import Text.DocTemplates (Context(..), Val(..), TemplateTarget,
ToContext(..), FromContext(..))
-import Text.Pandoc.Chunks (toTOCTree, SecInfo(..))
-import Data.Tree
+import Text.Pandoc.Chunks (tocToList, toTOCTree)
-- | Create template Context from a 'Meta' and an association list
-- of variables, specified at the command line or in the writer.
@@ -470,35 +469,6 @@ toTableOfContents opts =
. toTOCTree
. makeSections (writerNumberSections opts) Nothing
-tocEntryToLink :: SecInfo -> [Inline]
-tocEntryToLink secinfo = headerLink
- where
- addNumber = case secNumber secinfo of
- Just num -> (Span ("",["toc-section-number"],[])
- [Str num] :) . (Space :)
- Nothing -> id
- clean (Link _ xs _) = xs
- clean (Note _) = []
- clean x = [x]
- ident = secId secinfo
- headerText = addNumber $ walk (concatMap clean) (secTitle secinfo)
- headerLink = if T.null ident
- then headerText
- else [Link ("toc-" <> ident, [], [])
- headerText (secPath secinfo <> "#" <> ident, "")]
-
-tocToList :: Int -> Tree SecInfo -> Block
-tocToList tocDepth (Node _ subtrees)
- = BulletList (toItems subtrees)
- where
- toItems = map go . filter isBelowTocDepth
- isBelowTocDepth (Node sec _) = secLevel sec <= tocDepth
- go (Node secinfo xs) =
- Plain (tocEntryToLink secinfo) :
- if null xs
- then []
- else [BulletList (toItems xs)]
-
-- | Returns 'True' iff the list of blocks has a @'Plain'@ as its last
-- element.
endsWithPlain :: [Block] -> Bool