jaspervdj / hakyll

A static website compiler library in Haskell
jaspervdj.be/hakyll
Other
2.7k stars 411 forks source link

Generate slide show with Pandoc 2 #609

Open simgt opened 6 years ago

simgt commented 6 years ago

Hi,

I am trying to generate some DZSlides pages with Hakyll. Before the bump to Pandoc 2, I was using

    match "talks/*" $ do
        route $ setExtension "html"

        let writerOptions = defaultHakyllWriterOptions {
                                writerSlideVariant = DZSlides
                              , writerHtml5 = True
                              , writerHTMLMathMethod = KaTeX "" ""
                            }

        compile $ pandocCompilerWith defaultHakyllReaderOptions writerOptions
            >>= loadAndApplyTemplate "templates/talk.html" postContext
            >>= relativizeUrls

both writerHtml5 and writerSlideVariant have been removed from WriterOptions, see jgm/pandoc@91cdcc796df3db290d1930b159eb3ee2f74d4c03.

What is the new proper way for doing this ?

nigelsim commented 5 years ago

I'm not sure how proper what I'm about to propose is, but it is where I got to from first principals.

Instead of being an option of the writeHtml5String function, slide generation are now their own functions - one for each slide framework. The Hakyll pandocCompilerWith calls PanDoc's writeHtml5String function through a few intermediate functions. So, what I did was create a new function to call writeRevealJs instead (the same should apply to the other slide writers).

...
import qualified Data.Text as T
import Text.Pandoc (Pandoc, runPure)
import Text.Pandoc.Writers.HTML (writeRevealJs)
...
writePandocSlideWith :: WriterOptions -> Item Pandoc -> Item String
writePandocSlideWith wopt (Item itemi doc) =
    case runPure $ writeRevealJs wopt doc of
        Left err    -> error $ "writePandocSlidesWith: " ++ show err
        Right item' -> Item itemi $ T.unpack item'

pandocCompileSlides :: ReaderOptions -> WriterOptions -> Compiler (Item String)
pandocCompileSlides ropt wopt =
    cached "pandocCompileSlides" $
        writePandocSlideWith wopt <$>
        (traverse (return.id) =<< readPandocWith ropt =<< getResourceBody)
...
   hakyll $ do
    match "presentations/*" $ do
        route $ setExtension "html"
        compile $ pandocCompileSlides defaultHakyllReaderOptions defaultHakyllWriterOptions
                >>= loadAndApplyTemplate "templates/presentation.html" defaultContext
                >>= relativizeUrls

Note, you'll need a template setup that correctly sets up the presentation. The $body$ that is generated is just the slide content.