git.iko.soy/typeable/pandoc
on master help sign in
Org writer: move everything into PandocMonad.
commitb27836666f98c19b2d86d5b63ce2ddf2658bb343 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 30 Mar 2017, 21:31 (10 years ago) parent8d50f37
1 file changed+35−23
src/Text/Pandoc/Writers/Org.hs+35−23
src/Text/Pandoc/Writers/Org.hs+35−23
@@ -36,11 +36,12 @@ module Text.Pandoc.Writers.Org ( writeOrg) where
3636import Control.Monad.State
3737import Data.Char (isAlphaNum, toLower)
3838import Data.List (intersect, intersperse, isPrefixOf, partition, transpose)
39−import Text.Pandoc.Class (PandocMonad)
39+import Text.Pandoc.Class (PandocMonad, report)
4040import Text.Pandoc.Definition
4141import Text.Pandoc.Options
4242import Text.Pandoc.Pretty
4343import Text.Pandoc.Shared
44+import Text.Pandoc.Logging
4445import Text.Pandoc.Templates (renderTemplate')
4546import Text.Pandoc.Writers.Shared
4647
@@ -50,16 +51,18 @@ data WriterState =
5051 , stOptions :: WriterOptions
5152 }
5253
54+type Org = StateT WriterState
55+
5356-- | Convert Pandoc to Org.
5457writeOrg :: PandocMonad m => WriterOptions -> Pandoc -> m String
55−writeOrg opts document = return $
58+writeOrg opts document = do
5659 let st = WriterState { stNotes = [],
5760 stHasMath = False,
5861 stOptions = opts }
59− in evalState (pandocToOrg document) st
62+ evalStateT (pandocToOrg document) st
6063
6164-- | Return Org representation of document.
62−pandocToOrg :: Pandoc -> State WriterState String
65+pandocToOrg :: PandocMonad m => Pandoc -> Org m String
6366pandocToOrg (Pandoc meta blocks) = do
6467 opts <- gets stOptions
6568 let colwidth = if writerWrapText opts == WrapAuto
@@ -81,13 +84,13 @@ pandocToOrg (Pandoc meta blocks) = do
8184 Just tpl -> return $ renderTemplate' tpl context
8285
8386-- | Return Org representation of notes.
84−notesToOrg :: [[Block]] -> State WriterState Doc
87+notesToOrg :: PandocMonad m => [[Block]] -> Org m Doc
8588notesToOrg notes =
8689 mapM (\(num, note) -> noteToOrg num note) (zip [1..] notes) >>=
8790 return . vsep
8891
8992-- | Return Org representation of a note.
90−noteToOrg :: Int -> [Block] -> State WriterState Doc
93+noteToOrg :: PandocMonad m => Int -> [Block] -> Org m Doc
9194noteToOrg num note = do
9295 contents <- blockListToOrg note
9396 let marker = "[fn:" ++ show num ++ "] "
@@ -107,8 +110,9 @@ isRawFormat f =
107110 f == Format "latex" || f == Format "tex" || f == Format "org"
108111
109112-- | Convert Pandoc block element to Org.
110−blockToOrg :: Block -- ^ Block element
111− -> State WriterState Doc
113+blockToOrg :: PandocMonad m
114+ => Block -- ^ Block element
115+ -> Org m Doc
112116blockToOrg Null = return empty
113117blockToOrg (Div (_,classes@(cls:_),kvs) bs) | "drawer" `elem` classes = do
114118 contents <- blockListToOrg bs
@@ -176,9 +180,11 @@ blockToOrg (LineBlock lns) = do
176180blockToOrg (RawBlock "html" str) =
177181 return $ blankline $$ "#+BEGIN_HTML" $$
178182 nest 2 (text str) $$ "#+END_HTML" $$ blankline
179−blockToOrg (RawBlock f str) | isRawFormat f =
180− return $ text str
181−blockToOrg (RawBlock _ _) = return empty
183+blockToOrg b@(RawBlock f str)
184+ | isRawFormat f = return $ text str
185+ | otherwise = do
186+ report $ BlockNotRendered b
187+ return empty
182188blockToOrg HorizontalRule = return $ blankline $$ "--------------" $$ blankline
183189blockToOrg (Header level attr inlines) = do
184190 contents <- inlineListToOrg inlines
@@ -252,21 +258,23 @@ blockToOrg (DefinitionList items) = do
252258 return $ vcat contents $$ blankline
253259
254260-- | Convert bullet list item (list of blocks) to Org.
255−bulletListItemToOrg :: [Block] -> State WriterState Doc
261+bulletListItemToOrg :: PandocMonad m => [Block] -> Org m Doc
256262bulletListItemToOrg items = do
257263 contents <- blockListToOrg items
258264 return $ hang 2 "- " (contents <> cr)
259265
260266-- | Convert ordered list item (a list of blocks) to Org.
261−orderedListItemToOrg :: String -- ^ marker for list item
267+orderedListItemToOrg :: PandocMonad m
268+ => String -- ^ marker for list item
262269 -> [Block] -- ^ list item (list of blocks)
263− -> State WriterState Doc
270+ -> Org m Doc
264271orderedListItemToOrg marker items = do
265272 contents <- blockListToOrg items
266273 return $ hang (length marker + 1) (text marker <> space) (contents <> cr)
267274
268275-- | Convert defintion list item (label, list of blocks) to Org.
269−definitionListItemToOrg :: ([Inline], [[Block]]) -> State WriterState Doc
276+definitionListItemToOrg :: PandocMonad m
277+ => ([Inline], [[Block]]) -> Org m Doc
270278definitionListItemToOrg (label, defs) = do
271279 label' <- inlineListToOrg label
272280 contents <- liftM vcat $ mapM blockListToOrg defs
@@ -299,16 +307,19 @@ attrHtml (ident, classes, kvs) =
299307 in name <> keyword <> ": " <> text (unwords kvStrings) <> cr
300308
301309-- | Convert list of Pandoc block elements to Org.
302−blockListToOrg :: [Block] -- ^ List of block elements
303− -> State WriterState Doc
310+blockListToOrg :: PandocMonad m
311+ => [Block] -- ^ List of block elements
312+ -> Org m Doc
304313blockListToOrg blocks = mapM blockToOrg blocks >>= return . vcat
305314
306315-- | Convert list of Pandoc inline elements to Org.
307−inlineListToOrg :: [Inline] -> State WriterState Doc
316+inlineListToOrg :: PandocMonad m
317+ => [Inline]
318+ -> Org m Doc
308319inlineListToOrg lst = mapM inlineToOrg lst >>= return . hcat
309320
310321-- | Convert Pandoc inline element to Org.
311−inlineToOrg :: Inline -> State WriterState Doc
322+inlineToOrg :: PandocMonad m => Inline -> Org m Doc
312323inlineToOrg (Span (uid, [], []) []) =
313324 return $ "<<" <> text uid <> ">>"
314325inlineToOrg (Span _ lst) =
@@ -343,10 +354,11 @@ inlineToOrg (Math t str) = do
343354 return $ if t == InlineMath
344355 then "$" <> text str <> "$"
345356 else "$$" <> text str <> "$$"
346−inlineToOrg (RawInline f@(Format f') str) =
347− return $ if isRawFormat f
348− then text str
349− else "@@" <> text f' <> ":" <> text str <> "@@"
357+inlineToOrg il@(RawInline f@(Format f') str)
358+ | isRawFormat f = return $ text str
359+ | otherwise = do
360+ report $ InlineNotRendered il
361+ return empty
350362inlineToOrg LineBreak = return (text "\\\\" <> cr)
351363inlineToOrg Space = return space
352364inlineToOrg SoftBreak = do