@@ -28,51 +28,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2828Utility functions for splitting documents into slides for slide
2929show formats (dzslides, s5, slidy, beamer).
3030-}
31−module Text.Pandoc.Slides ( toSlideElements, SlideElement(..) ) where
31+module Text.Pandoc.Slides ( getSlideLevel, prepSlides ) where
3232import Text.Pandoc.Definition
33−import Text.ParserCombinators.Parsec
34−import Text.Parsec.Pos (initialPos)
35−import Control.Monad
36−
37−data SlideElement = SectionSlide Int [Inline]
38− | ContentSlide [Inline] [Block] -- title - contents
39− deriving (Read, Show)
40−
41−toSlideElements :: [Block] -> [SlideElement]
42−toSlideElements bs =
43− case parse (pElements $ getSlideLevel bs) "blocks" bs of
44− Left err -> error $ "toSlideElements: " ++ show err -- should never happen
45− Right res -> res
46−
47−satisfies :: (Block -> Bool) -> GenParser Block () Block
48−satisfies f = token show (const $ initialPos "blocks")
49− (\x -> if f x then Just x else Nothing)
50−
51−pElements :: Int -> GenParser Block () [SlideElement]
52−pElements slideLevel = do
53− res <- many (pSectionSlide slideLevel <|> pContentSlide slideLevel)
54− eof
55− return res
56−
57−pContentSlide :: Int -> GenParser Block () SlideElement
58−pContentSlide slideLevel = try $ do
59− hrs <- many $ satisfies (== HorizontalRule)
60− Header _ tit <- option (Header 1 []) $ satisfies (isHeader (== slideLevel))
61− xs <- many $ try $ notFollowedBy (satisfies (== HorizontalRule)) >>
62− notFollowedBy (satisfies (isHeader (<= slideLevel))) >>
63− anyToken
64− guard $ not (null hrs && null tit && null xs) -- make sure we can't match empty
65− return $ ContentSlide tit xs
66−
67−pSectionSlide :: Int -> GenParser Block () SlideElement
68−pSectionSlide slideLevel = try $ do
69− skipMany $ satisfies (== HorizontalRule)
70− Header lvl txt <- satisfies (isHeader (< slideLevel))
71− return $ SectionSlide lvl txt
72−
73−isHeader :: (Int -> Bool) -> Block -> Bool
74−isHeader f (Header n _) = f n
75−isHeader _ _ = False
7633
7734-- | Find level of header that starts slides (defined as the least header
7835-- level that occurs before a non-header/non-hrule in the blocks).
@@ -86,3 +43,15 @@ getSlideLevel = go 6
8643 nonHOrHR (Header _ _) = False
8744 nonHOrHR (HorizontalRule) = False
8845 nonHOrHR _ = True
46+
47+-- | Prepare a block list to be passed to hierarchicalize.
48+prepSlides :: Int -> [Block] -> [Block]
49+prepSlides slideLevel = ensureStartWithH . splitHrule
50+ where splitHrule (HorizontalRule : Header n xs : ys)
51+ | n == slideLevel = Header slideLevel xs : splitHrule ys
52+ splitHrule (HorizontalRule : xs) = Header slideLevel [] : splitHrule xs
53+ splitHrule (x : xs) = x : splitHrule xs
54+ splitHrule [] = []
55+ ensureStartWithH bs@(Header n _:_)
56+ | n == slideLevel = bs
57+ ensureStartWithH bs = Header slideLevel [] : bs