git.iko.soy/typeable/pandoc
on master help sign in
Convert writers to use PandocMonad typeclass.
957eee2Jesse Rosenthal
src/Text/Pandoc/Writers/Docx.hs+38−44
@@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
28
29Conversion of 'Pandoc' documents to docx.
29Conversion of 'Pandoc' documents to docx.
30-}
30-}
31−module Text.Pandoc.Writers.Docx ( writeDocx, writeDocxPure ) where
31+module Text.Pandoc.Writers.Docx ( writeDocx ) where
32import Data.List ( intercalate, isPrefixOf, isSuffixOf )
32import Data.List ( intercalate, isPrefixOf, isSuffixOf )
33import qualified Data.ByteString as B
33import qualified Data.ByteString as B
34import qualified Data.ByteString.Lazy as BL
34import qualified Data.ByteString.Lazy as BL
@@ -65,8 +65,8 @@ import Text.Pandoc.MIME (MimeType, getMimeType, getMimeTypeDef,
65import Control.Applicative ((<|>))
65import Control.Applicative ((<|>))
66import Data.Maybe (fromMaybe, mapMaybe, maybeToList, isNothing)
66import Data.Maybe (fromMaybe, mapMaybe, maybeToList, isNothing)
67import Data.Char (ord, isSpace, toLower)
67import Data.Char (ord, isSpace, toLower)
68−import Text.Pandoc.Free (PandocAction, runIO)
68+import Text.Pandoc.Class (PandocMonad)
69−import qualified Text.Pandoc.Free as P
69+import qualified Text.Pandoc.Class as P
70
70
71data ListMarker = NoMarker
71data ListMarker = NoMarker
72 | BulletMarker
72 | BulletMarker
@@ -146,7 +146,7 @@ defaultWriterState = WriterState{
146 , stDynamicTextProps = []
146 , stDynamicTextProps = []
147 }
147 }
148
148
149−type WS = ReaderT WriterEnv (StateT WriterState (PandocAction))
149+type WS m = ReaderT WriterEnv (StateT WriterState m)
150
150
151mknode :: Node t => String -> [(String,String)] -> t -> Element
151mknode :: Node t => String -> [(String,String)] -> t -> Element
152mknode s attrs =
152mknode s attrs =
@@ -215,17 +215,11 @@ metaValueToInlines _ = []
215
215
216
216
217
217
218+writeDocx :: (PandocMonad m)
218−writeDocx :: WriterOptions -- ^ Writer options
219+ => WriterOptions -- ^ Writer options
219 -> Pandoc -- ^ Document to convert
220 -> Pandoc -- ^ Document to convert
220− -> IO BL.ByteString
221+ -> m BL.ByteString
221−writeDocx opts doc = runIO $ writeDocxPure opts doc
222+writeDocx opts doc@(Pandoc meta _) = do
222−
223−
224−-- | Produce an Docx file from a Pandoc document.
225−writeDocxPure :: WriterOptions -- ^ Writer options
226− -> Pandoc -- ^ Document to convert
227− -> PandocAction BL.ByteString
228−writeDocxPure opts doc@(Pandoc meta _) = do
229 let datadir = writerUserDataDir opts
223 let datadir = writerUserDataDir opts
230 let doc' = walk fixDisplayMath $ doc
224 let doc' = walk fixDisplayMath $ doc
231 username <- P.lookupEnv "USERNAME"
225 username <- P.lookupEnv "USERNAME"
@@ -611,7 +605,7 @@ styleToOpenXml sm style =
611 $ backgroundColor style )
605 $ backgroundColor style )
612 ]
606 ]
613
607
614−copyChildren :: Archive -> Archive -> String -> Integer -> [String] -> PandocAction Entry
608+copyChildren :: (PandocMonad m) => Archive -> Archive -> String -> Integer -> [String] -> m Entry
615copyChildren refArchive distArchive path timestamp elNames = do
609copyChildren refArchive distArchive path timestamp elNames = do
616 ref <- parseXml refArchive distArchive path
610 ref <- parseXml refArchive distArchive path
617 dist <- parseXml distArchive distArchive path
611 dist <- parseXml distArchive distArchive path
@@ -630,7 +624,7 @@ copyChildren refArchive distArchive path timestamp elNames = do
630baseListId :: Int
624baseListId :: Int
631baseListId = 1000
625baseListId = 1000
632
626
633−mkNumbering :: [ListMarker] -> PandocAction [Element]
627+mkNumbering :: (PandocMonad m) => [ListMarker] -> m [Element]
634mkNumbering lists = do
628mkNumbering lists = do
635 elts <- mapM mkAbstractNum (ordNub lists)
629 elts <- mapM mkAbstractNum (ordNub lists)
636 return $ elts ++ zipWith mkNum lists [baseListId..(baseListId + length lists - 1)]
630 return $ elts ++ zipWith mkNum lists [baseListId..(baseListId + length lists - 1)]
@@ -646,7 +640,7 @@ mkNum marker numid =
646 map (\lvl -> mknode "w:lvlOverride" [("w:ilvl",show (lvl :: Int))]
640 map (\lvl -> mknode "w:lvlOverride" [("w:ilvl",show (lvl :: Int))]
647 $ mknode "w:startOverride" [("w:val",show start)] ()) [0..6]
641 $ mknode "w:startOverride" [("w:val",show start)] ()) [0..6]
648
642
649−mkAbstractNum :: ListMarker -> PandocAction Element
643+mkAbstractNum :: (PandocMonad m) => ListMarker -> m Element
650mkAbstractNum marker = do
644mkAbstractNum marker = do
651 gen <- P.newStdGen
645 gen <- P.newStdGen
652 let (nsid, _) = randomR (0x10000000 :: Integer, 0xFFFFFFFF :: Integer) gen
646 let (nsid, _) = randomR (0x10000000 :: Integer, 0xFFFFFFFF :: Integer) gen
@@ -701,11 +695,11 @@ mkLvl marker lvl =
701 patternFor TwoParens s = "(" ++ s ++ ")"
695 patternFor TwoParens s = "(" ++ s ++ ")"
702 patternFor _ s = s ++ "."
696 patternFor _ s = s ++ "."
703
697
704−getNumId :: WS Int
698+getNumId :: (PandocMonad m) => WS m Int
705getNumId = (((baseListId - 1) +) . length) `fmap` gets stLists
699getNumId = (((baseListId - 1) +) . length) `fmap` gets stLists
706
700
707
701
708−makeTOC :: WriterOptions -> WS [Element]
702+makeTOC :: (PandocMonad m) => WriterOptions -> WS m [Element]
709makeTOC opts | writerTableOfContents opts = do
703makeTOC opts | writerTableOfContents opts = do
710 let depth = "1-"++(show (writerTOCDepth opts))
704 let depth = "1-"++(show (writerTOCDepth opts))
711 let tocCmd = "TOC \\o \""++depth++"\" \\h \\z \\u"
705 let tocCmd = "TOC \\o \""++depth++"\" \\h \\z \\u"
@@ -735,7 +729,7 @@ makeTOC _ = return []
735
729
736-- | Convert Pandoc document to two lists of
730-- | Convert Pandoc document to two lists of
737-- OpenXML elements (the main document and footnotes).
731-- OpenXML elements (the main document and footnotes).
738−writeOpenXML :: WriterOptions -> Pandoc -> WS ([Element], [Element])
732+writeOpenXML :: (PandocMonad m) => WriterOptions -> Pandoc -> WS m ([Element], [Element])
739writeOpenXML opts (Pandoc meta blocks) = do
733writeOpenXML opts (Pandoc meta blocks) = do
740 let tit = docTitle meta ++ case lookupMeta "subtitle" meta of
734 let tit = docTitle meta ++ case lookupMeta "subtitle" meta of
741 Just (MetaBlocks [Plain xs]) -> LineBreak : xs
735 Just (MetaBlocks [Plain xs]) -> LineBreak : xs
@@ -770,13 +764,13 @@ writeOpenXML opts (Pandoc meta blocks) = do
770 return (meta' ++ doc', notes')
764 return (meta' ++ doc', notes')
771
765
772-- | Convert a list of Pandoc blocks to OpenXML.
766-- | Convert a list of Pandoc blocks to OpenXML.
773−blocksToOpenXML :: WriterOptions -> [Block] -> WS [Element]
767+blocksToOpenXML :: (PandocMonad m) => WriterOptions -> [Block] -> WS m [Element]
774blocksToOpenXML opts bls = concat `fmap` mapM (blockToOpenXML opts) bls
768blocksToOpenXML opts bls = concat `fmap` mapM (blockToOpenXML opts) bls
775
769
776pCustomStyle :: String -> Element
770pCustomStyle :: String -> Element
777pCustomStyle sty = mknode "w:pStyle" [("w:val",sty)] ()
771pCustomStyle sty = mknode "w:pStyle" [("w:val",sty)] ()
778
772
779−pStyleM :: String -> WS XML.Element
773+pStyleM :: (PandocMonad m) => String -> WS m XML.Element
780pStyleM styleName = do
774pStyleM styleName = do
781 styleMaps <- gets stStyleMaps
775 styleMaps <- gets stStyleMaps
782 let sty' = getStyleId styleName $ sParaStyleMap styleMaps
776 let sty' = getStyleId styleName $ sParaStyleMap styleMaps
@@ -785,13 +779,13 @@ pStyleM styleName = do
785rCustomStyle :: String -> Element
779rCustomStyle :: String -> Element
786rCustomStyle sty = mknode "w:rStyle" [("w:val",sty)] ()
780rCustomStyle sty = mknode "w:rStyle" [("w:val",sty)] ()
787
781
788−rStyleM :: String -> WS XML.Element
782+rStyleM :: (PandocMonad m) => String -> WS m XML.Element
789rStyleM styleName = do
783rStyleM styleName = do
790 styleMaps <- gets stStyleMaps
784 styleMaps <- gets stStyleMaps
791 let sty' = getStyleId styleName $ sCharStyleMap styleMaps
785 let sty' = getStyleId styleName $ sCharStyleMap styleMaps
792 return $ mknode "w:rStyle" [("w:val",sty')] ()
786 return $ mknode "w:rStyle" [("w:val",sty')] ()
793
787
794−getUniqueId :: PandocAction String
788+getUniqueId :: (PandocMonad m) => m String
795-- the + 20 is to ensure that there are no clashes with the rIds
789-- the + 20 is to ensure that there are no clashes with the rIds
796-- already in word/document.xml.rel
790-- already in word/document.xml.rel
797getUniqueId = (show . (+ 20)) <$> P.newUniqueHash
791getUniqueId = (show . (+ 20)) <$> P.newUniqueHash
@@ -801,10 +795,10 @@ dynamicStyleKey :: String
801dynamicStyleKey = "custom-style"
795dynamicStyleKey = "custom-style"
802
796
803-- | Convert a Pandoc block element to OpenXML.
797-- | Convert a Pandoc block element to OpenXML.
804−blockToOpenXML :: WriterOptions -> Block -> WS [Element]
798+blockToOpenXML :: (PandocMonad m) => WriterOptions -> Block -> WS m [Element]
805blockToOpenXML opts blk = withDirection $ blockToOpenXML' opts blk
799blockToOpenXML opts blk = withDirection $ blockToOpenXML' opts blk
806
800
807−blockToOpenXML' :: WriterOptions -> Block -> WS [Element]
801+blockToOpenXML' :: (PandocMonad m) => WriterOptions -> Block -> WS m [Element]
808blockToOpenXML' _ Null = return []
802blockToOpenXML' _ Null = return []
809blockToOpenXML' opts (Div (ident,classes,kvs) bs)
803blockToOpenXML' opts (Div (ident,classes,kvs) bs)
810 | Just sty <- lookup dynamicStyleKey kvs = do
804 | Just sty <- lookup dynamicStyleKey kvs = do
@@ -955,7 +949,7 @@ blockToOpenXML' opts (DefinitionList items) = do
955 setFirstPara
949 setFirstPara
956 return l
950 return l
957
951
958−definitionListItemToOpenXML :: WriterOptions -> ([Inline],[[Block]]) -> WS [Element]
952+definitionListItemToOpenXML :: (PandocMonad m) => WriterOptions -> ([Inline],[[Block]]) -> WS m [Element]
959definitionListItemToOpenXML opts (term,defs) = do
953definitionListItemToOpenXML opts (term,defs) = do
960 term' <- withParaProp (pCustomStyle "DefinitionTerm")
954 term' <- withParaProp (pCustomStyle "DefinitionTerm")
961 $ blockToOpenXML opts (Para term)
955 $ blockToOpenXML opts (Para term)
@@ -963,12 +957,12 @@ definitionListItemToOpenXML opts (term,defs) = do
963 $ concat `fmap` mapM (blocksToOpenXML opts) defs
957 $ concat `fmap` mapM (blocksToOpenXML opts) defs
964 return $ term' ++ defs'
958 return $ term' ++ defs'
965
959
966−addList :: ListMarker -> WS ()
960+addList :: (PandocMonad m) => ListMarker -> WS m ()
967addList marker = do
961addList marker = do
968 lists <- gets stLists
962 lists <- gets stLists
969 modify $ \st -> st{ stLists = lists ++ [marker] }
963 modify $ \st -> st{ stLists = lists ++ [marker] }
970
964
971−listItemToOpenXML :: WriterOptions -> Int -> [Block] -> WS [Element]
965+listItemToOpenXML :: (PandocMonad m) => WriterOptions -> Int -> [Block] -> WS m [Element]
972listItemToOpenXML _ _ [] = return []
966listItemToOpenXML _ _ [] = return []
973listItemToOpenXML opts numid (first:rest) = do
967listItemToOpenXML opts numid (first:rest) = do
974 first' <- withNumId numid $ blockToOpenXML opts first
968 first' <- withNumId numid $ blockToOpenXML opts first
@@ -984,30 +978,30 @@ alignmentToString alignment = case alignment of
984 AlignDefault -> "left"
978 AlignDefault -> "left"
985
979
986-- | Convert a list of inline elements to OpenXML.
980-- | Convert a list of inline elements to OpenXML.
987−inlinesToOpenXML :: WriterOptions -> [Inline] -> WS [Element]
981+inlinesToOpenXML :: (PandocMonad m) => WriterOptions -> [Inline] -> WS m [Element]
988inlinesToOpenXML opts lst = concat `fmap` mapM (inlineToOpenXML opts) lst
982inlinesToOpenXML opts lst = concat `fmap` mapM (inlineToOpenXML opts) lst
989
983
990−withNumId :: Int -> WS a -> WS a
984+withNumId :: (PandocMonad m) => Int -> WS m a -> WS m a
991withNumId numid = local $ \env -> env{ envListNumId = numid }
985withNumId numid = local $ \env -> env{ envListNumId = numid }
992
986
993−asList :: WS a -> WS a
987+asList :: (PandocMonad m) => WS m a -> WS m a
994asList = local $ \env -> env{ envListLevel = envListLevel env + 1 }
988asList = local $ \env -> env{ envListLevel = envListLevel env + 1 }
995
989
996−getTextProps :: WS [Element]
990+getTextProps :: (PandocMonad m) => WS m [Element]
997getTextProps = do
991getTextProps = do
998 props <- asks envTextProperties
992 props <- asks envTextProperties
999 return $ if null props
993 return $ if null props
1000 then []
994 then []
1001 else [mknode "w:rPr" [] props]
995 else [mknode "w:rPr" [] props]
1002
996
1003−withTextProp :: Element -> WS a -> WS a
997+withTextProp :: PandocMonad m => Element -> WS m a -> WS m a
1004withTextProp d p =
998withTextProp d p =
1005 local (\env -> env {envTextProperties = d : envTextProperties env}) p
999 local (\env -> env {envTextProperties = d : envTextProperties env}) p
1006
1000
1007−withTextPropM :: WS Element -> WS a -> WS a
1001+withTextPropM :: PandocMonad m => WS m Element -> WS m a -> WS m a
1008withTextPropM = (. flip withTextProp) . (>>=)
1002withTextPropM = (. flip withTextProp) . (>>=)
1009
1003
1010−getParaProps :: Bool -> WS [Element]
1004+getParaProps :: PandocMonad m => Bool -> WS m [Element]
1011getParaProps displayMathPara = do
1005getParaProps displayMathPara = do
1012 props <- asks envParaProperties
1006 props <- asks envParaProperties
1013 listLevel <- asks envListLevel
1007 listLevel <- asks envListLevel
@@ -1022,14 +1016,14 @@ getParaProps displayMathPara = do
1022 [] -> []
1016 [] -> []
1023 ps -> [mknode "w:pPr" [] ps]
1017 ps -> [mknode "w:pPr" [] ps]
1024
1018
1025−withParaProp :: Element -> WS a -> WS a
1019+withParaProp :: PandocMonad m => Element -> WS m a -> WS m a
1026withParaProp d p =
1020withParaProp d p =
1027 local (\env -> env {envParaProperties = d : envParaProperties env}) p
1021 local (\env -> env {envParaProperties = d : envParaProperties env}) p
1028
1022
1029−withParaPropM :: WS Element -> WS a -> WS a
1023+withParaPropM :: PandocMonad m => WS m Element -> WS m a -> WS m a
1030withParaPropM = (. flip withParaProp) . (>>=)
1024withParaPropM = (. flip withParaProp) . (>>=)
1031
1025
1032−formattedString :: String -> WS [Element]
1026+formattedString :: PandocMonad m => String -> WS m [Element]
1033formattedString str = do
1027formattedString str = do
1034 props <- getTextProps
1028 props <- getTextProps
1035 inDel <- asks envInDel
1029 inDel <- asks envInDel
@@ -1038,14 +1032,14 @@ formattedString str = do
1038 [ mknode (if inDel then "w:delText" else "w:t")
1032 [ mknode (if inDel then "w:delText" else "w:t")
1039 [("xml:space","preserve")] (stripInvalidChars str) ] ]
1033 [("xml:space","preserve")] (stripInvalidChars str) ] ]
1040
1034
1041−setFirstPara :: WS ()
1035+setFirstPara :: PandocMonad m => WS m ()
1042setFirstPara = modify $ \s -> s { stFirstPara = True }
1036setFirstPara = modify $ \s -> s { stFirstPara = True }
1043
1037
1044-- | Convert an inline element to OpenXML.
1038-- | Convert an inline element to OpenXML.
1045−inlineToOpenXML :: WriterOptions -> Inline -> WS [Element]
1039+inlineToOpenXML :: PandocMonad m => WriterOptions -> Inline -> WS m [Element]
1046inlineToOpenXML opts il = withDirection $ inlineToOpenXML' opts il
1040inlineToOpenXML opts il = withDirection $ inlineToOpenXML' opts il
1047
1041
1048−inlineToOpenXML' :: WriterOptions -> Inline -> WS [Element]
1042+inlineToOpenXML' :: PandocMonad m => WriterOptions -> Inline -> WS m [Element]
1049inlineToOpenXML' _ (Str str) = formattedString str
1043inlineToOpenXML' _ (Str str) = formattedString str
1050inlineToOpenXML' opts Space = inlineToOpenXML opts (Str " ")
1044inlineToOpenXML' opts Space = inlineToOpenXML opts (Str " ")
1051inlineToOpenXML' opts SoftBreak = inlineToOpenXML opts (Str " ")
1045inlineToOpenXML' opts SoftBreak = inlineToOpenXML opts (Str " ")
@@ -1281,7 +1275,7 @@ defaultFootnotes = [ mknode "w:footnote"
1281 [ mknode "w:r" [] $
1275 [ mknode "w:r" [] $
1282 [ mknode "w:continuationSeparator" [] ()]]]]
1276 [ mknode "w:continuationSeparator" [] ()]]]]
1283
1277
1284−parseXml :: Archive -> Archive -> String -> PandocAction Element
1278+parseXml :: (PandocMonad m) => Archive -> Archive -> String -> m Element
1285parseXml refArchive distArchive relpath =
1279parseXml refArchive distArchive relpath =
1286 case findEntryByPath relpath refArchive `mplus`
1280 case findEntryByPath relpath refArchive `mplus`
1287 findEntryByPath relpath distArchive of
1281 findEntryByPath relpath distArchive of
@@ -1299,7 +1293,7 @@ fitToPage (x, y) pageWidth
1299 (pageWidth, floor $ ((fromIntegral pageWidth) / x) * y)
1293 (pageWidth, floor $ ((fromIntegral pageWidth) / x) * y)
1300 | otherwise = (floor x, floor y)
1294 | otherwise = (floor x, floor y)
1301
1295
1302−withDirection :: WS a -> WS a
1296+withDirection :: PandocMonad m => WS m a -> WS m a
1303withDirection x = do
1297withDirection x = do
1304 isRTL <- asks envRTL
1298 isRTL <- asks envRTL
1305 paraProps <- asks envParaProperties
1299 paraProps <- asks envParaProperties
src/Text/Pandoc/Writers/EPUB.hs+25−24
@@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
28
29Conversion of 'Pandoc' documents to EPUB.
29Conversion of 'Pandoc' documents to EPUB.
30-}
30-}
31−module Text.Pandoc.Writers.EPUB ( writeEPUB, writeEPUBPure ) where
31+module Text.Pandoc.Writers.EPUB ( writeEPUB ) where
32import qualified Data.Map as M
32import qualified Data.Map as M
33import qualified Data.Set as Set
33import qualified Data.Set as Set
34import Data.Maybe ( fromMaybe, catMaybes )
34import Data.Maybe ( fromMaybe, catMaybes )
@@ -64,8 +64,8 @@ import Data.Char ( toLower, isDigit, isAlphaNum )
64import Text.Pandoc.MIME (MimeType, getMimeType, extensionFromMimeType)
64import Text.Pandoc.MIME (MimeType, getMimeType, extensionFromMimeType)
65import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
65import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
66import Text.HTML.TagSoup (Tag(TagOpen), fromAttrib, parseTags)
66import Text.HTML.TagSoup (Tag(TagOpen), fromAttrib, parseTags)
67−import Text.Pandoc.Free (PandocAction, runIO)
67+import Text.Pandoc.Class (PandocMonad)
68−import qualified Text.Pandoc.Free as P
68+import qualified Text.Pandoc.Class as P
69
69
70-- A Chapter includes a list of blocks and maybe a section
70-- A Chapter includes a list of blocks and maybe a section
71-- number offset. Note, some chapters are unnumbered. The section
71-- number offset. Note, some chapters are unnumbered. The section
@@ -76,7 +76,7 @@ data Chapter = Chapter (Maybe [Int]) [Block]
76data EPUBState = EPUBState { stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
76data EPUBState = EPUBState { stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
77 }
77 }
78
78
79−type E = StateT EPUBState PandocAction
79+type E m = StateT EPUBState m
80
80
81data EPUBMetadata = EPUBMetadata{
81data EPUBMetadata = EPUBMetadata{
82 epubIdentifier :: [Identifier]
82 epubIdentifier :: [Identifier]
@@ -145,7 +145,7 @@ removeNote :: Inline -> Inline
145removeNote (Note _) = Str ""
145removeNote (Note _) = Str ""
146removeNote x = x
146removeNote x = x
147
147
148−getEPUBMetadata :: WriterOptions -> Meta -> E EPUBMetadata
148+getEPUBMetadata :: PandocMonad m => WriterOptions -> Meta -> E m EPUBMetadata
149getEPUBMetadata opts meta = do
149getEPUBMetadata opts meta = do
150 let md = metadataFromMeta opts meta
150 let md = metadataFromMeta opts meta
151 let elts = onlyElems $ parseXML $ writerEpubMetadata opts
151 let elts = onlyElems $ parseXML $ writerEpubMetadata opts
@@ -335,23 +335,20 @@ metadataFromMeta opts meta = EPUBMetadata{
335 _ -> Nothing
335 _ -> Nothing
336
336
337-- | Produce an EPUB file from a Pandoc document.
337-- | Produce an EPUB file from a Pandoc document.
338+writeEPUB :: PandocMonad m
338−writeEPUB :: WriterOptions -- ^ Writer options
339+ => WriterOptions -- ^ Writer options
339 -> Pandoc -- ^ Document to convert
340 -> Pandoc -- ^ Document to convert
340− -> IO B.ByteString
341+ -> m B.ByteString
341−writeEPUB opts doc = runIO $ writeEPUBPure opts doc
342+writeEPUB opts doc =
342−
343−writeEPUBPure :: WriterOptions -- ^ Writer options
344− -> Pandoc -- ^ Document to convert
345− -> PandocAction B.ByteString
346−writeEPUBPure opts doc =
347 let initState = EPUBState { stMediaPaths = []
343 let initState = EPUBState { stMediaPaths = []
348 }
344 }
349 in
345 in
350 evalStateT (pandocToEPUB opts doc) initState
346 evalStateT (pandocToEPUB opts doc) initState
351
347
348+pandocToEPUB :: PandocMonad m
352−pandocToEPUB :: WriterOptions
349+ => WriterOptions
353− -> Pandoc
350+ -> Pandoc
354− -> E B.ByteString
351+ -> E m B.ByteString
355pandocToEPUB opts doc@(Pandoc meta _) = do
352pandocToEPUB opts doc@(Pandoc meta _) = do
356 let version = fromMaybe EPUB2 (writerEpubVersion opts)
353 let version = fromMaybe EPUB2 (writerEpubVersion opts)
357 let epub3 = version == EPUB3
354 let epub3 = version == EPUB3
@@ -829,10 +826,11 @@ metadataElement version md currentTime =
829showDateTimeISO8601 :: UTCTime -> String
826showDateTimeISO8601 :: UTCTime -> String
830showDateTimeISO8601 = formatTime defaultTimeLocale "%FT%TZ"
827showDateTimeISO8601 = formatTime defaultTimeLocale "%FT%TZ"
831
828
829+transformTag :: PandocMonad m
832−transformTag :: WriterOptions
830+ => WriterOptions
833 -- -> IORef [(FilePath, (FilePath, Maybe Entry))] -- ^ (oldpath, newpath, entry) media
831 -- -> IORef [(FilePath, (FilePath, Maybe Entry))] -- ^ (oldpath, newpath, entry) media
834 -> Tag String
832 -> Tag String
835− -> E (Tag String)
833+ -> E m (Tag String)
836transformTag opts tag@(TagOpen name attr)
834transformTag opts tag@(TagOpen name attr)
837 | name `elem` ["video", "source", "img", "audio"] &&
835 | name `elem` ["video", "source", "img", "audio"] &&
838 lookup "data-external" attr == Nothing = do
836 lookup "data-external" attr == Nothing = do
@@ -846,9 +844,10 @@ transformTag opts tag@(TagOpen name attr)
846 return $ TagOpen name attr'
844 return $ TagOpen name attr'
847transformTag _ tag = return tag
845transformTag _ tag = return tag
848
846
847+modifyMediaRef :: PandocMonad m
849−modifyMediaRef :: WriterOptions
848+ => WriterOptions
850 -> FilePath
849 -> FilePath
851− -> E FilePath
850+ -> E m FilePath
852modifyMediaRef _ "" = return ""
851modifyMediaRef _ "" = return ""
853modifyMediaRef opts oldsrc = do
852modifyMediaRef opts oldsrc = do
854 media <- gets stMediaPaths
853 media <- gets stMediaPaths
@@ -872,10 +871,11 @@ modifyMediaRef opts oldsrc = do
872 modify $ \st -> st{ stMediaPaths = (oldsrc, (new, mbEntry)):media}
871 modify $ \st -> st{ stMediaPaths = (oldsrc, (new, mbEntry)):media}
873 return new
872 return new
874
873
874+transformBlock :: PandocMonad m
875−transformBlock :: WriterOptions
875+ => WriterOptions
876 -- -> IORef [(FilePath, (FilePath, Maybe Entry))] -- ^ (oldpath, newpath, entry) media
876 -- -> IORef [(FilePath, (FilePath, Maybe Entry))] -- ^ (oldpath, newpath, entry) media
877 -> Block
877 -> Block
878− -> E Block
878+ -> E m Block
879transformBlock opts (RawBlock fmt raw)
879transformBlock opts (RawBlock fmt raw)
880 | fmt == Format "html" = do
880 | fmt == Format "html" = do
881 let tags = parseTags raw
881 let tags = parseTags raw
@@ -883,10 +883,11 @@ transformBlock opts (RawBlock fmt raw)
883 return $ RawBlock fmt (renderTags' tags')
883 return $ RawBlock fmt (renderTags' tags')
884transformBlock _ b = return b
884transformBlock _ b = return b
885
885
886+transformInline :: PandocMonad m
886−transformInline :: WriterOptions
887+ => WriterOptions
887 -- -> IORef [(FilePath, (FilePath, Maybe Entry))] -- ^ (oldpath, newpath) media
888 -- -> IORef [(FilePath, (FilePath, Maybe Entry))] -- ^ (oldpath, newpath) media
888 -> Inline
889 -> Inline
889− -> E Inline
890+ -> E m Inline
890transformInline opts (Image attr lab (src,tit)) = do
891transformInline opts (Image attr lab (src,tit)) = do
891 newsrc <- modifyMediaRef opts src
892 newsrc <- modifyMediaRef opts src
892 return $ Image attr lab (newsrc, tit)
893 return $ Image attr lab (newsrc, tit)
src/Text/Pandoc/Writers/FB2.hs+27−30
@@ -25,7 +25,7 @@ FictionBook is an XML-based e-book format. For more information see:
25<http://www.fictionbook.org/index.php/Eng:XML_Schema_Fictionbook_2.1>
25<http://www.fictionbook.org/index.php/Eng:XML_Schema_Fictionbook_2.1>
26
26
27-}
27-}
28−module Text.Pandoc.Writers.FB2 (writeFB2, writeFB2Pure) where
28+module Text.Pandoc.Writers.FB2 (writeFB2) where
29
29
30import Control.Monad.State (StateT, evalStateT, get, modify, lift)
30import Control.Monad.State (StateT, evalStateT, get, modify, lift)
31import Control.Monad.State (liftM)
31import Control.Monad.State (liftM)
@@ -45,8 +45,8 @@ import Text.Pandoc.Definition
45import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..), def)
45import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..), def)
46import Text.Pandoc.Shared (orderedListMarkers, isHeaderBlock, capitalize,
46import Text.Pandoc.Shared (orderedListMarkers, isHeaderBlock, capitalize,
47 linesToPara)
47 linesToPara)
48−import Text.Pandoc.Free (PandocAction, runIO)
48+import Text.Pandoc.Class (PandocMonad)
49−import qualified Text.Pandoc.Free as P
49+import qualified Text.Pandoc.Class as P
50
50
51-- | Data to be written at the end of the document:
51-- | Data to be written at the end of the document:
52-- (foot)notes, URLs, references, images.
52-- (foot)notes, URLs, references, images.
@@ -59,7 +59,7 @@ data FbRenderState = FbRenderState
59 } deriving (Show)
59 } deriving (Show)
60
60
61-- | FictionBook building monad.
61-- | FictionBook building monad.
62−type FBM = StateT FbRenderState PandocAction
62+type FBM m = StateT FbRenderState m
63
63
64newFB :: FbRenderState
64newFB :: FbRenderState
65newFB = FbRenderState { footnotes = [], imagesToFetch = []
65newFB = FbRenderState { footnotes = [], imagesToFetch = []
@@ -72,19 +72,16 @@ instance Show ImageMode where
72 show InlineImage = "inlineImageType"
72 show InlineImage = "inlineImageType"
73
73
74-- | Produce an FB2 document from a 'Pandoc' document.
74-- | Produce an FB2 document from a 'Pandoc' document.
75+writeFB2 :: PandocMonad m
75−writeFB2 :: WriterOptions -- ^ conversion options
76+ => WriterOptions -- ^ conversion options
76 -> Pandoc -- ^ document to convert
77 -> Pandoc -- ^ document to convert
77− -> IO String -- ^ FictionBook2 document (not encoded yet)
78+ -> m String -- ^ FictionBook2 document (not encoded yet)
78−writeFB2 opts doc = runIO $ writeFB2Pure opts doc
79+writeFB2 opts doc = flip evalStateT newFB $ pandocToFB2 opts doc
79
80
80−writeFB2Pure :: WriterOptions
81+pandocToFB2 :: PandocMonad m
81− -> Pandoc
82− -> PandocAction String
83−writeFB2Pure opts doc = flip evalStateT newFB $ pandocToFB2 opts doc
84−
85−pandocToFB2 :: WriterOptions
82+ => WriterOptions
86 -> Pandoc
83 -> Pandoc
87− -> FBM String
84+ -> FBM m String
88pandocToFB2 opts (Pandoc meta blocks) = do
85pandocToFB2 opts (Pandoc meta blocks) = do
89 modify (\s -> s { writerOptions = opts { writerOptions = opts } })
86 modify (\s -> s { writerOptions = opts { writerOptions = opts } })
90 desc <- description meta
87 desc <- description meta
@@ -104,7 +101,7 @@ pandocToFB2 opts (Pandoc meta blocks) = do
104 in [ uattr "xmlns" xmlns
101 in [ uattr "xmlns" xmlns
105 , attr ("xmlns", "l") xlink ]
102 , attr ("xmlns", "l") xlink ]
106
103
107−frontpage :: Meta -> FBM [Content]
104+frontpage :: PandocMonad m => Meta -> FBM m [Content]
108frontpage meta' = do
105frontpage meta' = do
109 t <- cMapM toXml . docTitle $ meta'
106 t <- cMapM toXml . docTitle $ meta'
110 return $
107 return $
@@ -113,7 +110,7 @@ frontpage meta' = do
113 (docAuthors meta' ++ [docDate meta']))
110 (docAuthors meta' ++ [docDate meta']))
114 ]
111 ]
115
112
116−description :: Meta -> FBM Content
113+description :: PandocMonad m => Meta -> FBM m Content
117description meta' = do
114description meta' = do
118 bt <- booktitle meta'
115 bt <- booktitle meta'
119 let as = authors meta'
116 let as = authors meta'
@@ -123,7 +120,7 @@ description meta' = do
123 , el "document-info" [ el "program-used" "pandoc" ] -- FIXME: +version
120 , el "document-info" [ el "program-used" "pandoc" ] -- FIXME: +version
124 ]
121 ]
125
122
126−booktitle :: Meta -> FBM [Content]
123+booktitle :: PandocMonad m => Meta -> FBM m [Content]
127booktitle meta' = do
124booktitle meta' = do
128 t <- cMapM toXml . docTitle $ meta'
125 t <- cMapM toXml . docTitle $ meta'
129 return $ if null t
126 return $ if null t
@@ -148,7 +145,7 @@ author ss =
148 ([]) -> []
145 ([]) -> []
149 in list $ el "author" (names ++ email)
146 in list $ el "author" (names ++ email)
150
147
151−docdate :: Meta -> FBM [Content]
148+docdate :: PandocMonad m => Meta -> FBM m [Content]
152docdate meta' = do
149docdate meta' = do
153 let ss = docDate meta'
150 let ss = docDate meta'
154 d <- cMapM toXml ss
151 d <- cMapM toXml ss
@@ -158,12 +155,12 @@ docdate meta' = do
158
155
159-- | Divide the stream of blocks into sections and convert to XML
156-- | Divide the stream of blocks into sections and convert to XML
160-- representation.
157-- representation.
161−renderSections :: Int -> [Block] -> FBM [Content]
158+renderSections :: PandocMonad m => Int -> [Block] -> FBM m [Content]
162renderSections level blocks = do
159renderSections level blocks = do
163 let secs = splitSections level blocks
160 let secs = splitSections level blocks
164 mapM (renderSection level) secs
161 mapM (renderSection level) secs
165
162
166−renderSection :: Int -> ([Inline], [Block]) -> FBM Content
163+renderSection :: PandocMonad m => Int -> ([Inline], [Block]) -> FBM m Content
167renderSection level (ttl, body) = do
164renderSection level (ttl, body) = do
168 title <- if null ttl
165 title <- if null ttl
169 then return []
166 then return []
@@ -210,7 +207,7 @@ splitSections level blocks = reverse $ revSplit (reverse blocks)
210 sameLevel _ = False
207 sameLevel _ = False
211
208
212-- | Make another FictionBook body with footnotes.
209-- | Make another FictionBook body with footnotes.
213−renderFootnotes :: FBM [Content]
210+renderFootnotes :: PandocMonad m => FBM m [Content]
214renderFootnotes = do
211renderFootnotes = do
215 fns <- footnotes `liftM` get
212 fns <- footnotes `liftM` get
216 if null fns
213 if null fns
@@ -224,14 +221,14 @@ renderFootnotes = do
224
221
225-- | Fetch images and encode them for the FictionBook XML.
222-- | Fetch images and encode them for the FictionBook XML.
226-- Return image data and a list of hrefs of the missing images.
223-- Return image data and a list of hrefs of the missing images.
227−fetchImages :: [(String,String)] -> PandocAction ([Content],[String])
224+fetchImages :: PandocMonad m => [(String,String)] -> m ([Content],[String])
228fetchImages links = do
225fetchImages links = do
229 imgs <- mapM (uncurry fetchImage) links
226 imgs <- mapM (uncurry fetchImage) links
230 return $ (rights imgs, lefts imgs)
227 return $ (rights imgs, lefts imgs)
231
228
232-- | Fetch image data from disk or from network and make a <binary> XML section.
229-- | Fetch image data from disk or from network and make a <binary> XML section.
233-- Return either (Left hrefOfMissingImage) or (Right xmlContent).
230-- Return either (Left hrefOfMissingImage) or (Right xmlContent).
234−fetchImage :: String -> String -> PandocAction (Either String Content)
231+fetchImage :: PandocMonad m => String -> String -> m (Either String Content)
235fetchImage href link = do
232fetchImage href link = do
236 mbimg <-
233 mbimg <-
237 case (isURI link, readDataURI link) of
234 case (isURI link, readDataURI link) of
@@ -298,7 +295,7 @@ linkID :: Int -> String
298linkID i = "l" ++ (show i)
295linkID i = "l" ++ (show i)
299
296
300-- | Convert a block-level Pandoc's element to FictionBook XML representation.
297-- | Convert a block-level Pandoc's element to FictionBook XML representation.
301−blockToXml :: Block -> FBM [Content]
298+blockToXml :: PandocMonad m => Block -> FBM m [Content]
302blockToXml (Plain ss) = cMapM toXml ss -- FIXME: can lead to malformed FB2
299blockToXml (Plain ss) = cMapM toXml ss -- FIXME: can lead to malformed FB2
303blockToXml (Para [Math DisplayMath formula]) = insertMath NormalImage formula
300blockToXml (Para [Math DisplayMath formula]) = insertMath NormalImage formula
304-- title beginning with fig: indicates that the image is a figure
301-- title beginning with fig: indicates that the image is a figure
@@ -362,11 +359,11 @@ blockToXml (Table caption aligns _ headers rows) = do
362 c <- return . el "emphasis" =<< cMapM toXml caption
359 c <- return . el "emphasis" =<< cMapM toXml caption
363 return [el "table" (hd : bd), el "p" c]
360 return [el "table" (hd : bd), el "p" c]
364 where
361 where
365− mkrow :: String -> [TableCell] -> [Alignment] -> FBM Content
362+ mkrow :: PandocMonad m => String -> [TableCell] -> [Alignment] -> FBM m Content
366 mkrow tag cells aligns' =
363 mkrow tag cells aligns' =
367 (el "tr") `liftM` (mapM (mkcell tag) (zip cells aligns'))
364 (el "tr") `liftM` (mapM (mkcell tag) (zip cells aligns'))
368 --
365 --
369− mkcell :: String -> (TableCell, Alignment) -> FBM Content
366+ mkcell :: PandocMonad m => String -> (TableCell, Alignment) -> FBM m Content
370 mkcell tag (cell, align) = do
367 mkcell tag (cell, align) = do
371 cblocks <- cMapM blockToXml cell
368 cblocks <- cMapM blockToXml cell
372 return $ el tag ([align_attr align], cblocks)
369 return $ el tag ([align_attr align], cblocks)
@@ -410,7 +407,7 @@ indent = indentBlock
410 in intercalate [LineBreak] $ map ((Str spacer):) lns
407 in intercalate [LineBreak] $ map ((Str spacer):) lns
411
408
412-- | Convert a Pandoc's Inline element to FictionBook XML representation.
409-- | Convert a Pandoc's Inline element to FictionBook XML representation.
413−toXml :: Inline -> FBM [Content]
410+toXml :: PandocMonad m => Inline -> FBM m [Content]
414toXml (Str s) = return [txt s]
411toXml (Str s) = return [txt s]
415toXml (Span _ ils) = cMapM toXml ils
412toXml (Span _ ils) = cMapM toXml ils
416toXml (Emph ss) = list `liftM` wrap "emphasis" ss
413toXml (Emph ss) = list `liftM` wrap "emphasis" ss
@@ -462,7 +459,7 @@ toXml (Note bs) = do
462 , uattr "type" "note" ]
459 , uattr "type" "note" ]
463 , fn_ref )
460 , fn_ref )
464
461
465−insertMath :: ImageMode -> String -> FBM [Content]
462+insertMath :: PandocMonad m => ImageMode -> String -> FBM m [Content]
466insertMath immode formula = do
463insertMath immode formula = do
467 htmlMath <- return . writerHTMLMathMethod . writerOptions =<< get
464 htmlMath <- return . writerHTMLMathMethod . writerOptions =<< get
468 case htmlMath of
465 case htmlMath of
@@ -473,7 +470,7 @@ insertMath immode formula = do
473 insertImage immode img
470 insertImage immode img
474 _ -> return [el "code" formula]
471 _ -> return [el "code" formula]
475
472
476−insertImage :: ImageMode -> Inline -> FBM [Content]
473+insertImage :: PandocMonad m => ImageMode -> Inline -> FBM m [Content]
477insertImage immode (Image _ alt (url,ttl)) = do
474insertImage immode (Image _ alt (url,ttl)) = do
478 images <- imagesToFetch `liftM` get
475 images <- imagesToFetch `liftM` get
479 let n = 1 + length images
476 let n = 1 + length images
@@ -539,7 +536,7 @@ replaceImagesWithAlt missingHrefs body =
539
536
540
537
541-- | Wrap all inlines with an XML tag (given its unqualified name).
538-- | Wrap all inlines with an XML tag (given its unqualified name).
542−wrap :: String -> [Inline] -> FBM Content
539+wrap :: PandocMonad m => String -> [Inline] -> FBM m Content
543wrap tagname inlines = el tagname `liftM` cMapM toXml inlines
540wrap tagname inlines = el tagname `liftM` cMapM toXml inlines
544
541
545-- " Create a singleton list.
542-- " Create a singleton list.
src/Text/Pandoc/Writers/ICML.hs+16−20
@@ -28,8 +28,8 @@ import Data.Text as Text (breakOnAll, pack)
28import Control.Monad.State
28import Control.Monad.State
29import Network.URI (isURI)
29import Network.URI (isURI)
30import qualified Data.Set as Set
30import qualified Data.Set as Set
31−import Text.Pandoc.Free (runIO, PandocAction)
31+import Text.Pandoc.Class (PandocMonad)
32−import qualified Text.Pandoc.Free as P
32+import qualified Text.Pandoc.Class as P
33
33
34type Style = [String]
34type Style = [String]
35type Hyperlink = [(Int, String)]
35type Hyperlink = [(Int, String)]
@@ -42,7 +42,7 @@ data WriterState = WriterState{
42 , maxListDepth :: Int
42 , maxListDepth :: Int
43 }
43 }
44
44
45−type WS a = StateT WriterState PandocAction a
45+type WS m = StateT WriterState m
46
46
47defaultWriterState :: WriterState
47defaultWriterState :: WriterState
48defaultWriterState = WriterState{
48defaultWriterState = WriterState{
@@ -124,12 +124,8 @@ footnoteName = "Footnote"
124citeName = "Cite"
124citeName = "Cite"
125
125
126-- | Convert Pandoc document to string in ICML format.
126-- | Convert Pandoc document to string in ICML format.
127−writeICML :: WriterOptions -> Pandoc -> IO String
128−writeICML opts doc = runIO $ writeICMLPure opts doc
129−
130−-- | Convert Pandoc document to string in ICML format.
131−writeICMLPure :: WriterOptions -> Pandoc -> PandocAction String
127+writeICML :: PandocMonad m => WriterOptions -> Pandoc -> m String
132−writeICMLPure opts (Pandoc meta blocks) = do
128+writeICML opts (Pandoc meta blocks) = do
133 let colwidth = if writerWrapText opts == WrapAuto
129 let colwidth = if writerWrapText opts == WrapAuto
134 then Just $ writerColumns opts
130 then Just $ writerColumns opts
135 else Nothing
131 else Nothing
@@ -288,13 +284,13 @@ hyperlinksToDoc (x:xs) = hyp x $$ hyperlinksToDoc xs
288
284
289
285
290-- | Convert a list of Pandoc blocks to ICML.
286-- | Convert a list of Pandoc blocks to ICML.
291−blocksToICML :: WriterOptions -> Style -> [Block] -> WS Doc
287+blocksToICML :: PandocMonad m => WriterOptions -> Style -> [Block] -> WS m Doc
292blocksToICML opts style lst = do
288blocksToICML opts style lst = do
293 docs <- mapM (blockToICML opts style) lst
289 docs <- mapM (blockToICML opts style) lst
294 return $ intersperseBrs docs
290 return $ intersperseBrs docs
295
291
296-- | Convert a Pandoc block element to ICML.
292-- | Convert a Pandoc block element to ICML.
297−blockToICML :: WriterOptions -> Style -> Block -> WS Doc
293+blockToICML :: PandocMonad m => WriterOptions -> Style -> Block -> WS m Doc
298blockToICML opts style (Plain lst) = parStyle opts style lst
294blockToICML opts style (Plain lst) = parStyle opts style lst
299-- title beginning with fig: indicates that the image is a figure
295-- title beginning with fig: indicates that the image is a figure
300blockToICML opts style (Para img@[Image _ txt (_,'f':'i':'g':':':_)]) = do
296blockToICML opts style (Para img@[Image _ txt (_,'f':'i':'g':':':_)]) = do
@@ -364,7 +360,7 @@ blockToICML opts style (Div _ lst) = blocksToICML opts style lst
364blockToICML _ _ Null = return empty
360blockToICML _ _ Null = return empty
365
361
366-- | Convert a list of lists of blocks to ICML list items.
362-- | Convert a list of lists of blocks to ICML list items.
367−listItemsToICML :: WriterOptions -> String -> Style -> Maybe ListAttributes -> [[Block]] -> WS Doc
363+listItemsToICML :: PandocMonad m => WriterOptions -> String -> Style -> Maybe ListAttributes -> [[Block]] -> WS m Doc
368listItemsToICML _ _ _ _ [] = return empty
364listItemsToICML _ _ _ _ [] = return empty
369listItemsToICML opts listType style attribs (first:rest) = do
365listItemsToICML opts listType style attribs (first:rest) = do
370 st <- get
366 st <- get
@@ -379,7 +375,7 @@ listItemsToICML opts listType style attribs (first:rest) = do
379 return $ intersperseBrs docs
375 return $ intersperseBrs docs
380
376
381-- | Convert a list of blocks to ICML list items.
377-- | Convert a list of blocks to ICML list items.
382−listItemToICML :: WriterOptions -> Style -> Bool-> Maybe ListAttributes -> [Block] -> WS Doc
378+listItemToICML :: PandocMonad m => WriterOptions -> Style -> Bool-> Maybe ListAttributes -> [Block] -> WS m Doc
383listItemToICML opts style isFirst attribs item =
379listItemToICML opts style isFirst attribs item =
384 let makeNumbStart (Just (beginsWith, numbStl, _)) =
380 let makeNumbStart (Just (beginsWith, numbStl, _)) =
385 let doN DefaultStyle = []
381 let doN DefaultStyle = []
@@ -406,7 +402,7 @@ listItemToICML opts style isFirst attribs item =
406 return $ intersperseBrs (f : r)
402 return $ intersperseBrs (f : r)
407 else blocksToICML opts stl' item
403 else blocksToICML opts stl' item
408
404
409−definitionListItemToICML :: WriterOptions -> Style -> ([Inline],[[Block]]) -> WS Doc
405+definitionListItemToICML :: PandocMonad m => WriterOptions -> Style -> ([Inline],[[Block]]) -> WS m Doc
410definitionListItemToICML opts style (term,defs) = do
406definitionListItemToICML opts style (term,defs) = do
411 term' <- parStyle opts (defListTermName:style) term
407 term' <- parStyle opts (defListTermName:style) term
412 defs' <- mapM (blocksToICML opts (defListDefName:style)) defs
408 defs' <- mapM (blocksToICML opts (defListDefName:style)) defs
@@ -414,11 +410,11 @@ definitionListItemToICML opts style (term,defs) = do
414
410
415
411
416-- | Convert a list of inline elements to ICML.
412-- | Convert a list of inline elements to ICML.
417−inlinesToICML :: WriterOptions -> Style -> [Inline] -> WS Doc
413+inlinesToICML :: PandocMonad m => WriterOptions -> Style -> [Inline] -> WS m Doc
418inlinesToICML opts style lst = vcat `fmap` mapM (inlineToICML opts style) (mergeSpaces lst)
414inlinesToICML opts style lst = vcat `fmap` mapM (inlineToICML opts style) (mergeSpaces lst)
419
415
420-- | Convert an inline element to ICML.
416-- | Convert an inline element to ICML.
421−inlineToICML :: WriterOptions -> Style -> Inline -> WS Doc
417+inlineToICML :: PandocMonad m => WriterOptions -> Style -> Inline -> WS m Doc
422inlineToICML _ style (Str str) = charStyle style $ text $ escapeStringForXML str
418inlineToICML _ style (Str str) = charStyle style $ text $ escapeStringForXML str
423inlineToICML opts style (Emph lst) = inlinesToICML opts (emphName:style) lst
419inlineToICML opts style (Emph lst) = inlinesToICML opts (emphName:style) lst
424inlineToICML opts style (Strong lst) = inlinesToICML opts (strongName:style) lst
420inlineToICML opts style (Strong lst) = inlinesToICML opts (strongName:style) lst
@@ -458,7 +454,7 @@ inlineToICML opts style (Note lst) = footnoteToICML opts style lst
458inlineToICML opts style (Span _ lst) = inlinesToICML opts style lst
454inlineToICML opts style (Span _ lst) = inlinesToICML opts style lst
459
455
460-- | Convert a list of block elements to an ICML footnote.
456-- | Convert a list of block elements to an ICML footnote.
461−footnoteToICML :: WriterOptions -> Style -> [Block] -> WS Doc
457+footnoteToICML :: PandocMonad m => WriterOptions -> Style -> [Block] -> WS m Doc
462footnoteToICML opts style lst =
458footnoteToICML opts style lst =
463 let insertTab (Para ls) = blockToICML opts (footnoteName:style) $ Para $ (Str "\t"):ls
459 let insertTab (Para ls) = blockToICML opts (footnoteName:style) $ Para $ (Str "\t"):ls
464 insertTab block = blockToICML opts (footnoteName:style) block
460 insertTab block = blockToICML opts (footnoteName:style) block
@@ -489,7 +485,7 @@ intersperseBrs :: [Doc] -> Doc
489intersperseBrs = vcat . intersperse (selfClosingTag "Br" []) . filter (not . isEmpty)
485intersperseBrs = vcat . intersperse (selfClosingTag "Br" []) . filter (not . isEmpty)
490
486
491-- | Wrap a list of inline elements in an ICML Paragraph Style
487-- | Wrap a list of inline elements in an ICML Paragraph Style
492−parStyle :: WriterOptions -> Style -> [Inline] -> WS Doc
488+parStyle :: PandocMonad m => WriterOptions -> Style -> [Inline] -> WS m Doc
493parStyle opts style lst =
489parStyle opts style lst =
494 let slipIn x y = if null y
490 let slipIn x y = if null y
495 then x
491 then x
@@ -513,7 +509,7 @@ parStyle opts style lst =
513 state $ \st -> (cont, st{ blockStyles = Set.insert stlStr $ blockStyles st })
509 state $ \st -> (cont, st{ blockStyles = Set.insert stlStr $ blockStyles st })
514
510
515-- | Wrap a Doc in an ICML Character Style.
511-- | Wrap a Doc in an ICML Character Style.
516−charStyle :: Style -> Doc -> WS Doc
512+charStyle :: PandocMonad m => Style -> Doc -> WS m Doc
517charStyle style content =
513charStyle style content =
518 let (stlStr, attrs) = styleToStrAttr style
514 let (stlStr, attrs) = styleToStrAttr style
519 doc = inTags True "CharacterStyleRange" attrs $ inTagsSimple "Content" $ flush content
515 doc = inTags True "CharacterStyleRange" attrs $ inTagsSimple "Content" $ flush content
@@ -535,7 +531,7 @@ styleToStrAttr style =
535 in (stlStr, attrs)
531 in (stlStr, attrs)
536
532
537-- | Assemble an ICML Image.
533-- | Assemble an ICML Image.
538−imageICML :: WriterOptions -> Style -> Attr -> Target -> WS Doc
534+imageICML :: PandocMonad m => WriterOptions -> Style -> Attr -> Target -> WS m Doc
539imageICML opts style attr (src, _) = do
535imageICML opts style attr (src, _) = do
540 res <- lift $ P.fetchItem (writerSourceURL opts) src
536 res <- lift $ P.fetchItem (writerSourceURL opts) src
541 imgS <- case res of
537 imgS <- case res of
src/Text/Pandoc/Writers/ODT.hs+12−15
@@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
28
29Conversion of 'Pandoc' documents to ODT.
29Conversion of 'Pandoc' documents to ODT.
30-}
30-}
31−module Text.Pandoc.Writers.ODT ( writeODTPure, writeODT ) where
31+module Text.Pandoc.Writers.ODT ( writeODT ) where
32import Data.List ( isPrefixOf )
32import Data.List ( isPrefixOf )
33import Data.Maybe ( fromMaybe )
33import Data.Maybe ( fromMaybe )
34import Text.XML.Light.Output
34import Text.XML.Light.Output
@@ -49,33 +49,30 @@ import Text.Pandoc.XML
49import Text.Pandoc.Pretty
49import Text.Pandoc.Pretty
50import qualified Control.Exception as E
50import qualified Control.Exception as E
51import System.FilePath ( takeExtension, takeDirectory, (<.>))
51import System.FilePath ( takeExtension, takeDirectory, (<.>))
52−import Text.Pandoc.Free ( PandocAction, runIO )
52+import Text.Pandoc.Class ( PandocMonad )
53−import qualified Text.Pandoc.Free as P
53+import qualified Text.Pandoc.Class as P
54
54
55data ODTState = ODTState { stEntries :: [Entry]
55data ODTState = ODTState { stEntries :: [Entry]
56 }
56 }
57
57
58−type O = StateT ODTState PandocAction
58+type O m = StateT ODTState m
59
59
60-- | Produce an ODT file from a Pandoc document.
60-- | Produce an ODT file from a Pandoc document.
61+writeODT :: PandocMonad m
61−writeODT :: WriterOptions -- ^ Writer options
62+ => WriterOptions -- ^ Writer options
62 -> Pandoc -- ^ Document to convert
63 -> Pandoc -- ^ Document to convert
63− -> IO B.ByteString
64+ -> m B.ByteString
64−writeODT opts doc = runIO $ writeODTPure opts doc
65−
66−writeODTPure :: WriterOptions
67− -> Pandoc
68− -> PandocAction B.ByteString
69−writeODTPure opts doc =
65+writeODT opts doc =
70 let initState = ODTState{ stEntries = []
66 let initState = ODTState{ stEntries = []
71 }
67 }
72 in
68 in
73 evalStateT (pandocToODT opts doc) initState
69 evalStateT (pandocToODT opts doc) initState
74
70
75-- | Produce an ODT file from a Pandoc document.
71-- | Produce an ODT file from a Pandoc document.
72+pandocToODT :: PandocMonad m
76−pandocToODT :: WriterOptions -- ^ Writer options
73+ => WriterOptions -- ^ Writer options
77 -> Pandoc -- ^ Document to convert
74 -> Pandoc -- ^ Document to convert
78− -> O B.ByteString
75+ -> O m B.ByteString
79pandocToODT opts doc@(Pandoc meta _) = do
76pandocToODT opts doc@(Pandoc meta _) = do
80 let datadir = writerUserDataDir opts
77 let datadir = writerUserDataDir opts
81 let title = docTitle meta
78 let title = docTitle meta
@@ -145,7 +142,7 @@ pandocToODT opts doc@(Pandoc meta _) = do
145 return $ fromArchive archive''
142 return $ fromArchive archive''
146
143
147-- | transform both Image and Math elements
144-- | transform both Image and Math elements
148−transformPicMath :: WriterOptions ->Inline -> O Inline
145+transformPicMath :: PandocMonad m => WriterOptions ->Inline -> O m Inline
149transformPicMath opts (Image attr@(id', cls, _) lab (src,t)) = do
146transformPicMath opts (Image attr@(id', cls, _) lab (src,t)) = do
150 res <- lift $ P.fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
147 res <- lift $ P.fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
151 case res of
148 case res of
src/Text/Pandoc/Writers/RTF.hs+4−9
@@ -29,7 +29,6 @@ Conversion of 'Pandoc' documents to RTF (rich text format).
29-}
29-}
30module Text.Pandoc.Writers.RTF ( writeRTF
30module Text.Pandoc.Writers.RTF ( writeRTF
31 , writeRTFWithEmbeddedImages
31 , writeRTFWithEmbeddedImages
32− , writeRTFWithEmbeddedImagesPure
33 ) where
32 ) where
34import Text.Pandoc.Definition
33import Text.Pandoc.Definition
35import Text.Pandoc.Options
34import Text.Pandoc.Options
@@ -44,13 +43,13 @@ import qualified Data.ByteString as B
44import qualified Data.Map as M
43import qualified Data.Map as M
45import Text.Printf ( printf )
44import Text.Printf ( printf )
46import Text.Pandoc.ImageSize
45import Text.Pandoc.ImageSize
47−import Text.Pandoc.Free (PandocAction, runIO)
46+import Text.Pandoc.Class (PandocMonad)
48−import qualified Text.Pandoc.Free as P
47+import qualified Text.Pandoc.Class as P
49
48
50-- | Convert Image inlines into a raw RTF embedded image, read from a file,
49-- | Convert Image inlines into a raw RTF embedded image, read from a file,
51-- or a MediaBag, or the internet.
50-- or a MediaBag, or the internet.
52-- If file not found or filetype not jpeg or png, leave the inline unchanged.
51-- If file not found or filetype not jpeg or png, leave the inline unchanged.
53−rtfEmbedImage :: WriterOptions -> Inline -> PandocAction Inline
52+rtfEmbedImage :: PandocMonad m => WriterOptions -> Inline -> m Inline
54rtfEmbedImage opts x@(Image attr _ (src,_)) = do
53rtfEmbedImage opts x@(Image attr _ (src,_)) = do
55 result <- P.fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
54 result <- P.fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
56 case result of
55 case result of
@@ -83,12 +82,8 @@ rtfEmbedImage _ x = return x
83
82
84-- | Convert Pandoc to a string in rich text format, with
83-- | Convert Pandoc to a string in rich text format, with
85-- images embedded as encoded binary data.
84-- images embedded as encoded binary data.
86−writeRTFWithEmbeddedImages :: WriterOptions -> Pandoc -> IO String
85+writeRTFWithEmbeddedImages :: PandocMonad m => WriterOptions -> Pandoc -> m String
87writeRTFWithEmbeddedImages options doc =
86writeRTFWithEmbeddedImages options doc =
88− runIO $ writeRTF options `fmap` walkM (rtfEmbedImage options) doc
89−
90−writeRTFWithEmbeddedImagesPure :: WriterOptions -> Pandoc -> PandocAction String
91−writeRTFWithEmbeddedImagesPure options doc =
92 writeRTF options `fmap` walkM (rtfEmbedImage options) doc
87 writeRTF options `fmap` walkM (rtfEmbedImage options) doc
93
88
94-- | Convert Pandoc to a string in rich text format.
89-- | Convert Pandoc to a string in rich text format.