From c1d058aeb1c6a331a2cc22786ffaab17f7118ccd Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Mon, 7 Jan 2019 21:07:05 -0800
Subject: revealjs writer: fix some section nesting corner cases.

* Ensure that we don't get > 2 levels of section nesting,
  even with slide level > 2.
* If slide level == N but there is no N-level header, make
  sure the next header with level > N gets treated as a slide
  and put in a section, rather than remaining loose.

Closes #5168.
---
 src/Text/Pandoc/Writers/HTML.hs | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

(limited to 'src/Text')

diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs
index 98b86a7c9..7329d8197 100644
--- a/src/Text/Pandoc/Writers/HTML.hs
+++ b/src/Text/Pandoc/Writers/HTML.hs
@@ -266,7 +266,7 @@ pandocToHtml opts (Pandoc meta blocks) = do
             then fmap renderHtml' <$> tableOfContents opts sects
             else return Nothing
   blocks' <- liftM (mconcat . intersperse (nl opts)) $
-                 mapM (elementToHtml slideLevel opts) sects
+                 mapM (elementToHtml Nothing slideLevel opts) sects
   st <- get
   notes <- footnoteSection opts (reverse (stNotes st))
   let thebody = blocks' >> notes
@@ -435,12 +435,17 @@ deLink (Link _ ils _) = Span nullAttr ils
 deLink x              = x
 
 -- | Convert an Element to Html.
-elementToHtml :: PandocMonad m => Int -> WriterOptions -> Element
+elementToHtml :: PandocMonad m => Maybe Int -> Int -> WriterOptions -> Element
               -> StateT WriterState m Html
-elementToHtml _slideLevel opts (Blk block) = blockToHtml opts block
-elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elements) = do
+elementToHtml _ _ opts (Blk block) = blockToHtml opts block
+elementToHtml mbparentlevel slideLevel opts
+      (Sec level num (id',classes,keyvals) title' elements)
+  = do
   slideVariant <- gets stSlideVariant
-  let slide = slideVariant /= NoSlides && level <= slideLevel
+  let slide = slideVariant /= NoSlides &&
+               (level <= slideLevel ||
+                -- we're missing a header at slide level (see #5168)
+                maybe False (< slideLevel) mbparentlevel)
   let num' = zipWith (+) num (writerNumberOffset opts ++ repeat 0)
   modify $ \st -> st{stSecNum = num'}  -- update section number
   html5 <- gets stHtml5
@@ -468,7 +473,7 @@ elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elemen
   let inDiv xs = Blk (RawBlock (Format "html") ("<div class=\""
                        ++ fragmentClass ++ "\">")) :
                    (xs ++ [Blk (RawBlock (Format "html") "</div>")])
-  innerContents <- mapM (elementToHtml slideLevel opts)
+  innerContents <- mapM (elementToHtml (Just level) slideLevel opts)
                    $ if titleSlide
                         -- title slides have no content of their own
                         then filter isSec elements
@@ -491,6 +496,8 @@ elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elemen
                    secttag header'
        return $
          (if slideVariant == RevealJsSlides && not (null innerContents)
+             -- revealjs doesn't like more than one level of section nesting:
+             && isNothing mbparentlevel
                 then H5.section
                 else id) $ mconcat $ t : innerContents
      else if writerSectionDivs opts || slide
-- 
cgit v1.2.3