git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: Download webtex images and include as data URLs.
210855eJohn MacFarlane
src/Text/Pandoc/Writers/EPUB.hs+32−25
@@ -30,13 +30,14 @@ Conversion of 'Pandoc' documents to EPUB.
30module Text.Pandoc.Writers.EPUB ( writeEPUB ) where
30module Text.Pandoc.Writers.EPUB ( writeEPUB ) where
31import Data.IORef
31import Data.IORef
32import Data.Maybe ( fromMaybe, isNothing )
32import Data.Maybe ( fromMaybe, isNothing )
33−import Data.List ( isInfixOf, intercalate )
33+import Data.List ( isInfixOf, intercalate, isPrefixOf )
34import System.Environment ( getEnv )
34import System.Environment ( getEnv )
35import Text.Printf (printf)
35import Text.Printf (printf)
36import System.FilePath ( (</>), takeBaseName, takeExtension, takeFileName )
36import System.FilePath ( (</>), takeBaseName, takeExtension, takeFileName )
37import qualified Data.ByteString.Lazy as B
37import qualified Data.ByteString.Lazy as B
38import qualified Data.ByteString.Lazy.Char8 as B8
38import qualified Data.ByteString.Lazy.Char8 as B8
39import Text.Pandoc.UTF8 ( fromStringLazy, toString )
39import Text.Pandoc.UTF8 ( fromStringLazy, toString )
40+import Text.Pandoc.SelfContained ( makeSelfContained )
40import Codec.Archive.Zip
41import Codec.Archive.Zip
41import Data.Time.Clock.POSIX
42import Data.Time.Clock.POSIX
42import Data.Time
43import Data.Time
@@ -115,7 +116,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
115 -- handle pictures
116 -- handle pictures
116 picsRef <- newIORef []
117 picsRef <- newIORef []
117 Pandoc _ blocks <- bottomUpM
118 Pandoc _ blocks <- bottomUpM
118− (transformInlines (writerHTMLMathMethod opts') sourceDir picsRef) doc
119+ (transformInline (writerHTMLMathMethod opts') sourceDir picsRef) doc
119 pics <- readIORef picsRef
120 pics <- readIORef picsRef
120 let readPicEntry (oldsrc, newsrc) = do
121 let readPicEntry (oldsrc, newsrc) = do
121 (img,_) <- fetchItem sourceDir oldsrc
122 (img,_) <- fetchItem sourceDir oldsrc
@@ -404,34 +405,40 @@ metadataElement version metadataXML uuid lang title authors date currentTime mbC
404showDateTimeISO8601 :: UTCTime -> String
405showDateTimeISO8601 :: UTCTime -> String
405showDateTimeISO8601 = formatTime defaultTimeLocale "%FT%TZ"
406showDateTimeISO8601 = formatTime defaultTimeLocale "%FT%TZ"
406
407
407−transformInlines :: HTMLMathMethod
408+transformInline :: HTMLMathMethod
408 -> FilePath
409 -> FilePath
409 -> IORef [(FilePath, FilePath)] -- ^ (oldpath, newpath) images
410 -> IORef [(FilePath, FilePath)] -- ^ (oldpath, newpath) images
410− -> [Inline]
411+ -> Inline
411− -> IO [Inline]
412+ -> IO Inline
412−transformInlines _ _ _ (Image lab (src,_) : xs)
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)
413− | isNothing (imageTypeOf src) = return $ Emph lab : xs
418+ | isNothing (imageTypeOf src) = return $ Emph lab
414−transformInlines _ sourceDir picsRef (Image lab (src,tit) : xs) = do
419+ | otherwise = do
415− let src' = unEscapeString src
420+ let src' = unEscapeString src
416− pics <- readIORef picsRef
421+ pics <- readIORef picsRef
417− let oldsrc = sourceDir </> src'
422+ let oldsrc = sourceDir </> src'
418− let ext = takeExtension src'
423+ let ext = takeExtension src'
419− newsrc <- case lookup oldsrc pics of
424+ newsrc <- case lookup oldsrc pics of
420− Just n -> return n
425+ Just n -> return n
421− Nothing -> do
426+ Nothing -> do
422− let new = "images/img" ++ show (length pics) ++ ext
427+ let new = "images/img" ++ show (length pics) ++ ext
423− modifyIORef picsRef ( (oldsrc, new): )
428+ modifyIORef picsRef ( (oldsrc, new): )
424− return new
429+ return new
425− return $ Image lab (newsrc, tit) : xs
430+ return $ Image lab (newsrc, tit)
426−transformInlines (MathML _) _ _ (x@(Math _ _) : xs) = do
431+transformInline (MathML _) _ _ (x@(Math _ _)) = do
427 -- note: ideally we'd use a switch statement to provide a fallback
432 -- note: ideally we'd use a switch statement to provide a fallback
428 -- but switch does not seem to be widely implemented yet, so we just
433 -- but switch does not seem to be widely implemented yet, so we just
429 -- provide the mathml
434 -- provide the mathml
430− let writeHtmlInline opts z = trimr $
431− writeHtmlString opts $ Pandoc (Meta [] [] []) [Plain [z]]
432− result = writeHtmlInline def{writerHTMLMathMethod = MathML Nothing } x
435+ let result = writeHtmlInline def{writerHTMLMathMethod = MathML Nothing } x
433− return $ RawInline "html" result : xs
436+ return $ RawInline "html" result
434−transformInlines _ _ _ xs = return xs
437+transformInline _ _ _ x = return x
438+
439+writeHtmlInline :: WriterOptions -> Inline -> String
440+writeHtmlInline opts z = trimr $
441+ writeHtmlString opts $ Pandoc (Meta [] [] []) [Plain [z]]
435
442
436(!) :: Node t => (t -> Element) -> [(String, String)] -> t -> Element
443(!) :: Node t => (t -> Element) -> [(String, String)] -> t -> Element
437(!) f attrs n = add_attrs (map (\(k,v) -> Attr (unqual k) v) attrs) (f n)
444(!) f attrs n = add_attrs (map (\(k,v) -> Attr (unqual k) v) attrs) (f n)