git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: Download webtex images and include as data URLs.

This allows you to use `--webtex` in creating EPUBs.

commit210855e03e9389f8818870e20ca9a4f63d38ca8b authorJohn MacFarlane <fiddlosopher@gmail.com> dateWed 22 May 2013, 10:21 (13 years ago) parentca09c1a
1 file changed+32−25
src/Text/Pandoc/Writers/EPUB.hs+32−25
src/Text/Pandoc/Writers/EPUB.hs+32−25
@@ -30,13 +30,14 @@ Conversion of 'Pandoc' documents to EPUB.
3030module Text.Pandoc.Writers.EPUB ( writeEPUB ) where
3131import Data.IORef
3232import Data.Maybe ( fromMaybe, isNothing )
33−import Data.List ( isInfixOf, intercalate )
33+import Data.List ( isInfixOf, intercalate, isPrefixOf )
3434import System.Environment ( getEnv )
3535import Text.Printf (printf)
3636import System.FilePath ( (</>), takeBaseName, takeExtension, takeFileName )
3737import qualified Data.ByteString.Lazy as B
3838import qualified Data.ByteString.Lazy.Char8 as B8
3939import Text.Pandoc.UTF8 ( fromStringLazy, toString )
40+import Text.Pandoc.SelfContained ( makeSelfContained )
4041import Codec.Archive.Zip
4142import Data.Time.Clock.POSIX
4243import Data.Time
@@ -115,7 +116,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
115116 -- handle pictures
116117 picsRef <- newIORef []
117118 Pandoc _ blocks <- bottomUpM
118− (transformInlines (writerHTMLMathMethod opts') sourceDir picsRef) doc
119+ (transformInline (writerHTMLMathMethod opts') sourceDir picsRef) doc
119120 pics <- readIORef picsRef
120121 let readPicEntry (oldsrc, newsrc) = do
121122 (img,_) <- fetchItem sourceDir oldsrc
@@ -404,34 +405,40 @@ metadataElement version metadataXML uuid lang title authors date currentTime mbC
404405showDateTimeISO8601 :: UTCTime -> String
405406showDateTimeISO8601 = formatTime defaultTimeLocale "%FT%TZ"
406407
407−transformInlines :: HTMLMathMethod
408+transformInline :: HTMLMathMethod
408409 -> FilePath
409410 -> IORef [(FilePath, FilePath)] -- ^ (oldpath, newpath) images
410− -> [Inline]
411− -> IO [Inline]
412−transformInlines _ _ _ (Image lab (src,_) : xs)
413− | isNothing (imageTypeOf src) = return $ Emph lab : xs
414−transformInlines _ sourceDir picsRef (Image lab (src,tit) : xs) = do
415− let src' = unEscapeString src
416− pics <- readIORef picsRef
417− let oldsrc = sourceDir </> src'
418− let ext = takeExtension src'
419− newsrc <- case lookup oldsrc pics of
420− Just n -> return n
421− Nothing -> do
422− let new = "images/img" ++ show (length pics) ++ ext
423− modifyIORef picsRef ( (oldsrc, new): )
424− return new
425− return $ Image lab (newsrc, tit) : xs
426−transformInlines (MathML _) _ _ (x@(Math _ _) : xs) = do
411+ -> Inline
412+ -> IO Inline
413+transformInline _ sourceDir picsRef (Image lab (src,tit))
414+ | "http://chart.apis.google.com" `isPrefixOf` src = do
415+ raw <- makeSelfContained Nothing $
416+ writeHtmlInline def (Image lab (src,tit))
417+ return (RawInline "html" raw)
418+ | isNothing (imageTypeOf src) = return $ Emph lab
419+ | otherwise = do
420+ let src' = unEscapeString src
421+ pics <- readIORef picsRef
422+ let oldsrc = sourceDir </> src'
423+ let ext = takeExtension src'
424+ newsrc <- case lookup oldsrc pics of
425+ Just n -> return n
426+ Nothing -> do
427+ let new = "images/img" ++ show (length pics) ++ ext
428+ modifyIORef picsRef ( (oldsrc, new): )
429+ return new
430+ return $ Image lab (newsrc, tit)
431+transformInline (MathML _) _ _ (x@(Math _ _)) = do
427432 -- note: ideally we'd use a switch statement to provide a fallback
428433 -- but switch does not seem to be widely implemented yet, so we just
429434 -- provide the mathml
430− let writeHtmlInline opts z = trimr $
431− writeHtmlString opts $ Pandoc (Meta [] [] []) [Plain [z]]
432− result = writeHtmlInline def{writerHTMLMathMethod = MathML Nothing } x
433− return $ RawInline "html" result : xs
434−transformInlines _ _ _ xs = return xs
435+ let result = writeHtmlInline def{writerHTMLMathMethod = MathML Nothing } x
436+ return $ RawInline "html" result
437+transformInline _ _ _ x = return x
438+
439+writeHtmlInline :: WriterOptions -> Inline -> String
440+writeHtmlInline opts z = trimr $
441+ writeHtmlString opts $ Pandoc (Meta [] [] []) [Plain [z]]
435442
436443(!) :: Node t => (t -> Element) -> [(String, String)] -> t -> Element
437444(!) f attrs n = add_attrs (map (\(k,v) -> Attr (unqual k) v) attrs) (f n)