git.iko.soy/typeable/pandoc
on master help sign in
Make some writers sensitive to 'unlisted' class on headings.

If this is present on a heading with the 'unnumbered' class, the heading won't appear in the TOC. This class has no effect if 'unnumbered' is not also specified. This affects HTML-based writers (including slide shows and epub), LateX (including beamer), RTF, and PowerPoint. Other writers do not yet support `unlisted`. Closes #1762.

commit68b09a6d81b24b928b1629ecb3061a51a5ce2352 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 10 Oct 2019, 08:59 (7 years ago) parent2b1361e
4 files changed+33−7
MANUAL.txt+5 src/Text/Pandoc/Writers/LaTeX.hs+6−4 src/Text/Pandoc/Writers/Shared.hs+4−3 test/command/1762.md+18
MANUAL.txt+5
@@ -2712,6 +2712,11 @@ is just the same as
27122712
27132713 # My heading {.unnumbered}
27142714
2715+If the `unlisted` class is present in addition to `unnumbered`,
2716+the heading will not be included in a table of contents.
2717+(Currently this feature is only implemented for certain
2718+formats: those based on LaTeX and HTML, PowerPoint, and RTF.)
2719+
27152720#### Extension: `implicit_header_references` ####
27162721
27172722Pandoc behaves as if reference links have been defined for each heading.
src/Text/Pandoc/Writers/LaTeX.hs+6−4
@@ -748,7 +748,7 @@ blockToLaTeX HorizontalRule =
748748 "\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}"
749749blockToLaTeX (Header level (id',classes,_) lst) = do
750750 modify $ \s -> s{stInHeading = True}
751− hdr <- sectionHeader ("unnumbered" `elem` classes) id' level lst
751+ hdr <- sectionHeader classes id' level lst
752752 modify $ \s -> s{stInHeading = False}
753753 return hdr
754754blockToLaTeX (Table caption aligns widths heads rows) = do
@@ -949,12 +949,14 @@ defListItemToLaTeX (term, defs) = do
949949
950950-- | Craft the section header, inserting the secton reference, if supplied.
951951sectionHeader :: PandocMonad m
952− => Bool -- True for unnumbered
952+ => [String] -- classes
953953 -> [Char]
954954 -> Int
955955 -> [Inline]
956956 -> LW m (Doc Text)
957−sectionHeader unnumbered ident level lst = do
957+sectionHeader classes ident level lst = do
958+ let unnumbered = "unnumbered" `elem` classes
959+ let unlisted = "unlisted" `elem` classes
958960 txt <- inlineListToLaTeX lst
959961 plain <- stringToLaTeX TextString $ concatMap stringify lst
960962 let removeInvalidInline (Note _) = []
@@ -1013,7 +1015,7 @@ sectionHeader unnumbered ident level lst = do
10131015 return $ if level' > 5
10141016 then txt
10151017 else prefix $$ stuffing'
1016− $$ if unnumbered
1018+ $$ if unnumbered && not unlisted
10171019 then "\\addcontentsline{toc}" <>
10181020 braces (text sectionType) <>
10191021 braces txtNoNotes
src/Text/Pandoc/Writers/Shared.hs+4−3
@@ -40,7 +40,7 @@ where
4040import Prelude
4141import Safe (lastMay)
4242import qualified Data.ByteString.Lazy as BL
43−import Data.Maybe (fromMaybe)
43+import Data.Maybe (fromMaybe, isNothing)
4444import Control.Monad (zipWithM)
4545import Data.Aeson (ToJSON (..), encode)
4646import Data.Char (chr, ord, isSpace)
@@ -394,8 +394,9 @@ toTableOfContents opts bs =
394394-- | Converts an Element to a list item for a table of contents,
395395sectionToListItem :: WriterOptions -> Block -> [Block]
396396sectionToListItem opts (Div (ident,_,_)
397− (Header lev (_,_,kvs) ils : subsecs)) =
398− Plain headerLink : [BulletList listContents | not (null listContents)
397+ (Header lev (_,classes,kvs) ils : subsecs))
398+ | not (isNothing (lookup "number" kvs) && "unlisted" `elem` classes)
399+ = Plain headerLink : [BulletList listContents | not (null listContents)
399400 , lev < writerTOCDepth opts]
400401 where
401402 num = fromMaybe "" $ lookup "number" kvs
test/command/1762.mdnew file+18
@@ -0,0 +1,18 @@
1+```
2+% pandoc -t latex
3+# One {.unlisted}
4+
5+# Two {.unnumbered}
6+
7+# Three {.unlisted .unnumbered}
8+^D
9+\hypertarget{one}{%
10+\section{One}\label{one}}
11+
12+\hypertarget{two}{%
13+\section*{Two}\label{two}}
14+\addcontentsline{toc}{section}{Two}
15+
16+\hypertarget{three}{%
17+\section*{Three}\label{three}}
18+```