git.iko.soy/typeable/pandoc
on master help sign in
Finish pure writer of FB2.
commitec548c632ece93203d4012e06939b3bfc79c736a authorJesse Rosenthal <jrosenthal@jhu.edu> dateFri 18 Nov 2016, 16:54 (10 years ago) parent0152c7d
1 file changed+17−10
src/Text/Pandoc/Writers/FB2.hs+17−10
src/Text/Pandoc/Writers/FB2.hs+17−10
@@ -25,10 +25,10 @@ FictionBook is an XML-based e-book format. For more information see:
2525<http://www.fictionbook.org/index.php/Eng:XML_Schema_Fictionbook_2.1>
2626
2727-}
28−module Text.Pandoc.Writers.FB2 (writeFB2) where
28+module Text.Pandoc.Writers.FB2 (writeFB2, writeFB2Pure) where
2929
30−import Control.Monad.State (StateT, evalStateT, get, modify)
31−import Control.Monad.State (liftM, liftIO)
30+import Control.Monad.State (StateT, evalStateT, get, modify, lift)
31+import Control.Monad.State (liftM)
3232import Data.ByteString.Base64 (encode)
3333import Data.Char (toLower, isSpace, isAscii, isControl)
3434import Data.List (intersperse, intercalate, isPrefixOf, stripPrefix)
@@ -44,7 +44,9 @@ import qualified Data.ByteString.Char8 as B8
4444import Text.Pandoc.Definition
4545import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..), def)
4646import Text.Pandoc.Shared (orderedListMarkers, isHeaderBlock, capitalize,
47− linesToPara, fetchItem)
47+ linesToPara)
48+import Text.Pandoc.Free (PandocAction, runIO)
49+import qualified Text.Pandoc.Free as P
4850
4951-- | Data to be written at the end of the document:
5052-- (foot)notes, URLs, references, images.
@@ -57,7 +59,7 @@ data FbRenderState = FbRenderState
5759 } deriving (Show)
5860
5961-- | FictionBook building monad.
60−type FBM = StateT FbRenderState IO
62+type FBM = StateT FbRenderState PandocAction
6163
6264newFB :: FbRenderState
6365newFB = FbRenderState { footnotes = [], imagesToFetch = []
@@ -73,7 +75,12 @@ instance Show ImageMode where
7375writeFB2 :: WriterOptions -- ^ conversion options
7476 -> Pandoc -- ^ document to convert
7577 -> IO String -- ^ FictionBook2 document (not encoded yet)
76−writeFB2 opts doc = flip evalStateT newFB $ pandocToFB2 opts doc
78+writeFB2 opts doc = runIO $ writeFB2Pure opts doc
79+
80+writeFB2Pure :: WriterOptions
81+ -> Pandoc
82+ -> PandocAction String
83+writeFB2Pure opts doc = flip evalStateT newFB $ pandocToFB2 opts doc
7784
7885pandocToFB2 :: WriterOptions
7986 -> Pandoc
@@ -85,7 +92,7 @@ pandocToFB2 opts (Pandoc meta blocks) = do
8592 secs <- renderSections 1 blocks
8693 let body = el "body" $ fp ++ secs
8794 notes <- renderFootnotes
88− (imgs,missing) <- liftM imagesToFetch get >>= \s -> liftIO (fetchImages s)
95+ (imgs,missing) <- liftM imagesToFetch get >>= \s -> lift (fetchImages s)
8996 let body' = replaceImagesWithAlt missing body
9097 let fb2_xml = el "FictionBook" (fb2_attrs, [desc, body'] ++ notes ++ imgs)
9198 return $ xml_head ++ (showContent fb2_xml) ++ "\n"
@@ -217,14 +224,14 @@ renderFootnotes = do
217224
218225-- | Fetch images and encode them for the FictionBook XML.
219226-- Return image data and a list of hrefs of the missing images.
220−fetchImages :: [(String,String)] -> IO ([Content],[String])
227+fetchImages :: [(String,String)] -> PandocAction ([Content],[String])
221228fetchImages links = do
222229 imgs <- mapM (uncurry fetchImage) links
223230 return $ (rights imgs, lefts imgs)
224231
225232-- | Fetch image data from disk or from network and make a <binary> XML section.
226233-- Return either (Left hrefOfMissingImage) or (Right xmlContent).
227−fetchImage :: String -> String -> IO (Either String Content)
234+fetchImage :: String -> String -> PandocAction (Either String Content)
228235fetchImage href link = do
229236 mbimg <-
230237 case (isURI link, readDataURI link) of
@@ -235,7 +242,7 @@ fetchImage href link = do
235242 else return Nothing
236243 (True, Just _) -> return Nothing -- not base64-encoded
237244 _ -> do
238− response <- fetchItem Nothing link
245+ response <- P.fetchItem Nothing link
239246 case response of
240247 Right (bs, Just mime) -> return $ Just (mime, B8.unpack $ encode bs)
241248 _ -> return $ Nothing