@@ -1,3 +1,4 @@
1+{-# LANGUAGE PatternGuards #-}
2Copyright (C) 2010 John MacFarlane <jgm@berkeley.edu>
3Copyright (C) 2010 John MacFarlane <jgm@berkeley.edu>
@@ -30,7 +31,7 @@ Conversion of 'Pandoc' documents to EPUB.
30module Text.Pandoc.Writers.EPUB ( writeEPUB ) where
31module Text.Pandoc.Writers.EPUB ( writeEPUB ) where
31import Data.IORef
32import Data.IORef
32import Data.Maybe ( fromMaybe, isNothing )
33import Data.Maybe ( fromMaybe, isNothing )
33−import Data.List ( isInfixOf, intercalate, isPrefixOf )
34+import Data.List ( isInfixOf, intercalate )
34import System.Environment ( getEnv )
35import System.Environment ( getEnv )
35import Text.Printf (printf)
36import Text.Printf (printf)
36import System.FilePath ( (</>), takeBaseName, takeExtension, takeFileName )
37import System.FilePath ( (</>), takeBaseName, takeExtension, takeFileName )
@@ -53,7 +54,7 @@ import Text.Pandoc.UUID
53import Text.Pandoc.Writers.HTML
54import Text.Pandoc.Writers.HTML
54import Text.Pandoc.Writers.Markdown ( writePlain )
55import Text.Pandoc.Writers.Markdown ( writePlain )
55import Data.Char ( toLower )
56import Data.Char ( toLower )
56−import Network.URI ( unEscapeString )
57+import Network.URI ( isAbsoluteURI, unEscapeString )
57import Text.Pandoc.MIME (getMimeType)
58import Text.Pandoc.MIME (getMimeType)
58import Prelude hiding (catch)
59import Prelude hiding (catch)
59import Control.Exception (catch, SomeException)
60import Control.Exception (catch, SomeException)
@@ -116,7 +117,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
116 -- handle pictures
117 -- handle pictures
117 picsRef <- newIORef []
118 picsRef <- newIORef []
118 Pandoc _ blocks <- bottomUpM
119 Pandoc _ blocks <- bottomUpM
119− (transformInline (writerHTMLMathMethod opts') sourceDir picsRef) doc
120+ (transformInline opts' sourceDir picsRef) doc
120 pics <- readIORef picsRef
121 pics <- readIORef picsRef
121 let readPicEntry (oldsrc, newsrc) = do
122 let readPicEntry (oldsrc, newsrc) = do
122 (img,_) <- fetchItem sourceDir oldsrc
123 (img,_) <- fetchItem sourceDir oldsrc
@@ -405,17 +406,16 @@ metadataElement version metadataXML uuid lang title authors date currentTime mbC
405showDateTimeISO8601 :: UTCTime -> String
406showDateTimeISO8601 :: UTCTime -> String
406showDateTimeISO8601 = formatTime defaultTimeLocale "%FT%TZ"
407showDateTimeISO8601 = formatTime defaultTimeLocale "%FT%TZ"
408−transformInline :: HTMLMathMethod
409+transformInline :: WriterOptions
409 -> FilePath
410 -> FilePath
410 -> IORef [(FilePath, FilePath)] -- ^ (oldpath, newpath) images
411 -> IORef [(FilePath, FilePath)] -- ^ (oldpath, newpath) images
411 -> Inline
412 -> Inline
412 -> IO Inline
413 -> IO Inline
413−transformInline _ sourceDir picsRef (Image lab (src,tit))
414+transformInline opts sourceDir picsRef (Image lab (src,tit))
414− | "http://chart.apis.google.com" `isPrefixOf` src = do
415+ | isAbsoluteURI src = do
415− raw <- makeSelfContained Nothing $
416+ raw <- makeSelfContained Nothing
416− writeHtmlInline def (Image lab (src,tit))
417+ $ writeHtmlInline opts (Image lab (src,tit))
417− return (RawInline "html" raw)
418+ return $ RawInline "html" raw
418− | isNothing (imageTypeOf src) = return $ Emph lab
419 | otherwise = do
419 | otherwise = do
420 let src' = unEscapeString src
420 let src' = unEscapeString src
421 pics <- readIORef picsRef
421 pics <- readIORef picsRef
@@ -428,17 +428,16 @@ transformInline _ sourceDir picsRef (Image lab (src,tit))
428 modifyIORef picsRef ( (oldsrc, new): )
428 modifyIORef picsRef ( (oldsrc, new): )
429 return new
429 return new
430 return $ Image lab (newsrc, tit)
430 return $ Image lab (newsrc, tit)
431−transformInline (MathML _) _ _ (x@(Math _ _)) = do
431+transformInline opts _ _ (x@(Math _ _))
432− -- note: ideally we'd use a switch statement to provide a fallback
433− -- but switch does not seem to be widely implemented yet, so we just
434− -- provide the mathml
435− let result = writeHtmlInline def{writerHTMLMathMethod = MathML Nothing } x
432+ | WebTeX _ <- writerHTMLMathMethod opts = do
433+ raw <- makeSelfContained Nothing $ writeHtmlInline opts x
436− return $ RawInline "html" result
434+ return $ RawInline "html" raw
437transformInline _ _ _ x = return x
435transformInline _ _ _ x = return x
439writeHtmlInline :: WriterOptions -> Inline -> String
437writeHtmlInline :: WriterOptions -> Inline -> String
440writeHtmlInline opts z = trimr $
438writeHtmlInline opts z = trimr $
439+ writeHtmlString opts{ writerStandalone = False }
441− writeHtmlString opts $ Pandoc (Meta [] [] []) [Plain [z]]
440+ $ Pandoc (Meta [] [] []) [Plain [z]]
443(!) :: Node t => (t -> Element) -> [(String, String)] -> t -> Element
442(!) :: Node t => (t -> Element) -> [(String, String)] -> t -> Element
444(!) f attrs n = add_attrs (map (\(k,v) -> Attr (unqual k) v) attrs) (f n)
443(!) f attrs n = add_attrs (map (\(k,v) -> Attr (unqual k) v) attrs) (f n)