git.iko.soy/typeable/pandoc
on master help sign in
Use Slides in LaTeX writer for beamer.
commit97af5767295f9bc48c8dc99303bb5b53829beb43 authorJohn MacFarlane <fiddlosopher@gmail.com> dateMon 23 Jan 2012, 13:25 (15 years ago) parenta8046ea
2 files changed+23−39
src/Text/Pandoc/Slides.hs+6−10 src/Text/Pandoc/Writers/LaTeX.hs+17−29
src/Text/Pandoc/Slides.hs+6−10
@@ -28,14 +28,14 @@ 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 ( ) where
31+module Text.Pandoc.Slides ( toSlideElements, SlideElement(..) ) where
3232import Text.Pandoc.Definition
3333import Text.ParserCombinators.Parsec
3434import Text.Parsec.Pos (initialPos)
3535import Control.Monad
3636
3737data SlideElement = SectionSlide Int [Inline]
38− | ContentSlide [Inline] [Inline] [Block] -- title - subtitle - contents
38+ | ContentSlide [Inline] [Block] -- title - contents
3939 deriving (Read, Show)
4040
4141toSlideElements :: [Block] -> [SlideElement]
@@ -44,9 +44,6 @@ toSlideElements bs =
4444 Left err -> error $ show err
4545 Right res -> res
4646
47−anyTok :: GenParser Block () Block
48−anyTok = token show (const $ initialPos "blocks") Just
49−
5047satisfies :: (Block -> Bool) -> GenParser Block () Block
5148satisfies f = token show (const $ initialPos "blocks")
5249 (\x -> if f x then Just x else Nothing)
@@ -61,12 +58,11 @@ pContentSlide :: Int -> GenParser Block () SlideElement
6158pContentSlide slideLevel = try $ do
6259 hrs <- many $ satisfies (== HorizontalRule)
6360 Header _ tit <- option (Header 1 []) $ satisfies (isHeader (== slideLevel))
64− Header _ subtit <- option (Header 1 []) $ satisfies (isHeader (== slideLevel + 1))
65− guard $ not (null hrs && null tit && null subtit)
6661 xs <- many $ try $ notFollowedBy (satisfies (== HorizontalRule)) >>
6762 notFollowedBy (satisfies (isHeader (<= slideLevel))) >>
68− anyTok
69− return $ ContentSlide tit subtit xs
63+ anyToken
64+ guard $ not (null hrs && null tit && null xs) -- make sure we can't match empty
65+ return $ ContentSlide tit xs
7066
7167pSectionSlide :: Int -> GenParser Block () SlideElement
7268pSectionSlide slideLevel = try $ do
@@ -85,7 +81,7 @@ getSlideLevel = go 6
8581 where go least (Header n _ : x : xs)
8682 | n < least && nonHOrHR x = go n xs
8783 | otherwise = go least (x:xs)
88− go least (x : xs) = go least xs
84+ go least (_ : xs) = go least xs
8985 go least [] = least
9086 nonHOrHR (Header _ _) = False
9187 nonHOrHR (HorizontalRule) = False
src/Text/Pandoc/Writers/LaTeX.hs+17−29
@@ -41,6 +41,7 @@ import Data.Char ( toLower, isPunctuation )
4141import Control.Monad.State
4242import Text.Pandoc.Pretty
4343import System.FilePath (dropExtension)
44+import Text.Pandoc.Slides
4445import Text.Pandoc.Highlighting (highlight, styleToLaTeX,
4546 formatLaTeXInline, formatLaTeXBlock)
4647
@@ -62,7 +63,6 @@ data WriterState =
6263 , stBook :: Bool -- true if document uses book or memoir class
6364 , stCsquotes :: Bool -- true if document uses csquotes
6465 , stHighlighting :: Bool -- true if document has highlighted code
65− , stFirstFrame :: Bool -- true til we've written first beamer frame
6666 , stIncremental :: Bool -- true if beamer lists should be displayed bit by bit
6767 , stInternalLinks :: [String] -- list of internal link targets
6868 }
@@ -78,7 +78,7 @@ writeLaTeX options document =
7878 stUrl = False, stGraphics = False,
7979 stLHS = False, stBook = writerChapters options,
8080 stCsquotes = False, stHighlighting = False,
81− stFirstFrame = True, stIncremental = writerIncremental options,
81+ stIncremental = writerIncremental options,
8282 stInternalLinks = [] }
8383
8484pandocToLaTeX :: WriterOptions -> Pandoc -> State WriterState String
@@ -200,34 +200,22 @@ inCmd :: String -> Doc -> Doc
200200inCmd cmd contents = char '\\' <> text cmd <> braces contents
201201
202202toSlides :: [Block] -> State WriterState [Block]
203−toSlides (Header n ils : bs) = do
204− tit <- inlineListToLaTeX ils
205− firstFrame <- gets stFirstFrame
206− modify $ \s -> s{ stFirstFrame = False }
203+toSlides bs = concat `fmap` (mapM slideToBeamer $ toSlideElements bs)
204+
205+slideToBeamer :: SlideElement -> State WriterState [Block]
206+slideToBeamer (SectionSlide lvl tit) = return [Header lvl tit]
207+slideToBeamer (ContentSlide tit bs) = do
208+ tit' <- inlineListToLaTeX tit
207209 -- note: [fragile] is required or verbatim breaks
208− result <- ((Header n ils :) .
209− (RawBlock "latex" ("\\begin{frame}[fragile]\n" ++
210− "\\frametitle{" ++ render Nothing tit ++ "}") :))
211− `fmap` toSlides bs
212− if firstFrame
213− then return result
214− else return $ RawBlock "latex" "\\end{frame}" : result
215−toSlides (HorizontalRule : Header n ils : bs) =
216− toSlides (Header n ils : bs)
217−toSlides (HorizontalRule : bs) = do
218− firstFrame <- gets stFirstFrame
219− modify $ \s -> s{ stFirstFrame = False }
220− result <- (RawBlock "latex" "\\begin{frame}[fragile]" :)
221− `fmap` toSlides bs
222− if firstFrame
223− then return result
224− else return $ RawBlock "latex" "\\end{frame}" : result
225−toSlides (b:bs) = (b:) `fmap` toSlides bs
226−toSlides [] = do
227− firstFrame <- gets stFirstFrame
228− if firstFrame
229− then return []
230− else return [RawBlock "latex" "\\end{frame}"]
210+ let slideStart = RawBlock "latex" ("\\begin{frame}[fragile]\n" ++
211+ "\\frametitle{" ++ render Nothing tit' ++ "}")
212+ let slideEnd = RawBlock "latex" "\\end{frame}"
213+ -- now carve up slide into blocks if there are sections inside
214+ let eltToBlocks (Blk b) = [b]
215+ eltToBlocks (Sec _ _ _ lab xs) =
216+ Para (RawInline "latex" "\\begin{block}{" : lab ++ [RawInline "latex" "}"])
217+ : concatMap eltToBlocks xs ++ [RawBlock "latex" "\\end{block}"]
218+ return $ slideStart : concatMap eltToBlocks (hierarchicalize bs) ++ [slideEnd]
231219
232220isListBlock :: Block -> Bool
233221isListBlock (BulletList _) = True