git.iko.soy/typeable/pandoc
on master help sign in
Org writer: move everything into PandocMonad.
b278366John MacFarlane
src/Text/Pandoc/Writers/Org.hs+35−23
@@ -36,11 +36,12 @@ module Text.Pandoc.Writers.Org ( writeOrg) where
36import Control.Monad.State
36import Control.Monad.State
37import Data.Char (isAlphaNum, toLower)
37import Data.Char (isAlphaNum, toLower)
38import Data.List (intersect, intersperse, isPrefixOf, partition, transpose)
38import Data.List (intersect, intersperse, isPrefixOf, partition, transpose)
39−import Text.Pandoc.Class (PandocMonad)
39+import Text.Pandoc.Class (PandocMonad, report)
40import Text.Pandoc.Definition
40import Text.Pandoc.Definition
41import Text.Pandoc.Options
41import Text.Pandoc.Options
42import Text.Pandoc.Pretty
42import Text.Pandoc.Pretty
43import Text.Pandoc.Shared
43import Text.Pandoc.Shared
44+import Text.Pandoc.Logging
44import Text.Pandoc.Templates (renderTemplate')
45import Text.Pandoc.Templates (renderTemplate')
45import Text.Pandoc.Writers.Shared
46import Text.Pandoc.Writers.Shared
46
47
@@ -50,16 +51,18 @@ data WriterState =
50 , stOptions :: WriterOptions
51 , stOptions :: WriterOptions
51 }
52 }
52
53
54+type Org = StateT WriterState
55+
53-- | Convert Pandoc to Org.
56-- | Convert Pandoc to Org.
54writeOrg :: PandocMonad m => WriterOptions -> Pandoc -> m String
57writeOrg :: PandocMonad m => WriterOptions -> Pandoc -> m String
55−writeOrg opts document = return $
58+writeOrg opts document = do
56 let st = WriterState { stNotes = [],
59 let st = WriterState { stNotes = [],
57 stHasMath = False,
60 stHasMath = False,
58 stOptions = opts }
61 stOptions = opts }
59− in evalState (pandocToOrg document) st
62+ evalStateT (pandocToOrg document) st
60
63
61-- | Return Org representation of document.
64-- | Return Org representation of document.
62−pandocToOrg :: Pandoc -> State WriterState String
65+pandocToOrg :: PandocMonad m => Pandoc -> Org m String
63pandocToOrg (Pandoc meta blocks) = do
66pandocToOrg (Pandoc meta blocks) = do
64 opts <- gets stOptions
67 opts <- gets stOptions
65 let colwidth = if writerWrapText opts == WrapAuto
68 let colwidth = if writerWrapText opts == WrapAuto
@@ -81,13 +84,13 @@ pandocToOrg (Pandoc meta blocks) = do
81 Just tpl -> return $ renderTemplate' tpl context
84 Just tpl -> return $ renderTemplate' tpl context
82
85
83-- | Return Org representation of notes.
86-- | Return Org representation of notes.
84−notesToOrg :: [[Block]] -> State WriterState Doc
87+notesToOrg :: PandocMonad m => [[Block]] -> Org m Doc
85notesToOrg notes =
88notesToOrg notes =
86 mapM (\(num, note) -> noteToOrg num note) (zip [1..] notes) >>=
89 mapM (\(num, note) -> noteToOrg num note) (zip [1..] notes) >>=
87 return . vsep
90 return . vsep
88
91
89-- | Return Org representation of a note.
92-- | Return Org representation of a note.
90−noteToOrg :: Int -> [Block] -> State WriterState Doc
93+noteToOrg :: PandocMonad m => Int -> [Block] -> Org m Doc
91noteToOrg num note = do
94noteToOrg num note = do
92 contents <- blockListToOrg note
95 contents <- blockListToOrg note
93 let marker = "[fn:" ++ show num ++ "] "
96 let marker = "[fn:" ++ show num ++ "] "
@@ -107,8 +110,9 @@ isRawFormat f =
107 f == Format "latex" || f == Format "tex" || f == Format "org"
110 f == Format "latex" || f == Format "tex" || f == Format "org"
108
111
109-- | Convert Pandoc block element to Org.
112-- | Convert Pandoc block element to Org.
113+blockToOrg :: PandocMonad m
110−blockToOrg :: Block -- ^ Block element
114+ => Block -- ^ Block element
111− -> State WriterState Doc
115+ -> Org m Doc
112blockToOrg Null = return empty
116blockToOrg Null = return empty
113blockToOrg (Div (_,classes@(cls:_),kvs) bs) | "drawer" `elem` classes = do
117blockToOrg (Div (_,classes@(cls:_),kvs) bs) | "drawer" `elem` classes = do
114 contents <- blockListToOrg bs
118 contents <- blockListToOrg bs
@@ -176,9 +180,11 @@ blockToOrg (LineBlock lns) = do
176blockToOrg (RawBlock "html" str) =
180blockToOrg (RawBlock "html" str) =
177 return $ blankline $$ "#+BEGIN_HTML" $$
181 return $ blankline $$ "#+BEGIN_HTML" $$
178 nest 2 (text str) $$ "#+END_HTML" $$ blankline
182 nest 2 (text str) $$ "#+END_HTML" $$ blankline
179−blockToOrg (RawBlock f str) | isRawFormat f =
183+blockToOrg b@(RawBlock f str)
180− return $ text str
184+ | isRawFormat f = return $ text str
185+ | otherwise = do
186+ report $ BlockNotRendered b
181−blockToOrg (RawBlock _ _) = return empty
187+ return empty
182blockToOrg HorizontalRule = return $ blankline $$ "--------------" $$ blankline
188blockToOrg HorizontalRule = return $ blankline $$ "--------------" $$ blankline
183blockToOrg (Header level attr inlines) = do
189blockToOrg (Header level attr inlines) = do
184 contents <- inlineListToOrg inlines
190 contents <- inlineListToOrg inlines
@@ -252,21 +258,23 @@ blockToOrg (DefinitionList items) = do
252 return $ vcat contents $$ blankline
258 return $ vcat contents $$ blankline
253
259
254-- | Convert bullet list item (list of blocks) to Org.
260-- | Convert bullet list item (list of blocks) to Org.
255−bulletListItemToOrg :: [Block] -> State WriterState Doc
261+bulletListItemToOrg :: PandocMonad m => [Block] -> Org m Doc
256bulletListItemToOrg items = do
262bulletListItemToOrg items = do
257 contents <- blockListToOrg items
263 contents <- blockListToOrg items
258 return $ hang 2 "- " (contents <> cr)
264 return $ hang 2 "- " (contents <> cr)
259
265
260-- | Convert ordered list item (a list of blocks) to Org.
266-- | Convert ordered list item (a list of blocks) to Org.
267+orderedListItemToOrg :: PandocMonad m
261−orderedListItemToOrg :: String -- ^ marker for list item
268+ => String -- ^ marker for list item
262 -> [Block] -- ^ list item (list of blocks)
269 -> [Block] -- ^ list item (list of blocks)
263− -> State WriterState Doc
270+ -> Org m Doc
264orderedListItemToOrg marker items = do
271orderedListItemToOrg marker items = do
265 contents <- blockListToOrg items
272 contents <- blockListToOrg items
266 return $ hang (length marker + 1) (text marker <> space) (contents <> cr)
273 return $ hang (length marker + 1) (text marker <> space) (contents <> cr)
267
274
268-- | Convert defintion list item (label, list of blocks) to Org.
275-- | 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
270definitionListItemToOrg (label, defs) = do
278definitionListItemToOrg (label, defs) = do
271 label' <- inlineListToOrg label
279 label' <- inlineListToOrg label
272 contents <- liftM vcat $ mapM blockListToOrg defs
280 contents <- liftM vcat $ mapM blockListToOrg defs
@@ -299,16 +307,19 @@ attrHtml (ident, classes, kvs) =
299 in name <> keyword <> ": " <> text (unwords kvStrings) <> cr
307 in name <> keyword <> ": " <> text (unwords kvStrings) <> cr
300
308
301-- | Convert list of Pandoc block elements to Org.
309-- | Convert list of Pandoc block elements to Org.
310+blockListToOrg :: PandocMonad m
302−blockListToOrg :: [Block] -- ^ List of block elements
311+ => [Block] -- ^ List of block elements
303− -> State WriterState Doc
312+ -> Org m Doc
304blockListToOrg blocks = mapM blockToOrg blocks >>= return . vcat
313blockListToOrg blocks = mapM blockToOrg blocks >>= return . vcat
305
314
306-- | Convert list of Pandoc inline elements to Org.
315-- | Convert list of Pandoc inline elements to Org.
307−inlineListToOrg :: [Inline] -> State WriterState Doc
316+inlineListToOrg :: PandocMonad m
317+ => [Inline]
318+ -> Org m Doc
308inlineListToOrg lst = mapM inlineToOrg lst >>= return . hcat
319inlineListToOrg lst = mapM inlineToOrg lst >>= return . hcat
309
320
310-- | Convert Pandoc inline element to Org.
321-- | Convert Pandoc inline element to Org.
311−inlineToOrg :: Inline -> State WriterState Doc
322+inlineToOrg :: PandocMonad m => Inline -> Org m Doc
312inlineToOrg (Span (uid, [], []) []) =
323inlineToOrg (Span (uid, [], []) []) =
313 return $ "<<" <> text uid <> ">>"
324 return $ "<<" <> text uid <> ">>"
314inlineToOrg (Span _ lst) =
325inlineToOrg (Span _ lst) =
@@ -343,10 +354,11 @@ inlineToOrg (Math t str) = do
343 return $ if t == InlineMath
354 return $ if t == InlineMath
344 then "$" <> text str <> "$"
355 then "$" <> text str <> "$"
345 else "$$" <> text str <> "$$"
356 else "$$" <> text str <> "$$"
346−inlineToOrg (RawInline f@(Format f') str) =
357+inlineToOrg il@(RawInline f@(Format f') str)
347− return $ if isRawFormat f
358+ | isRawFormat f = return $ text str
348− then text str
359+ | otherwise = do
349− else "@@" <> text f' <> ":" <> text str <> "@@"
360+ report $ InlineNotRendered il
361+ return empty
350inlineToOrg LineBreak = return (text "\\\\" <> cr)
362inlineToOrg LineBreak = return (text "\\\\" <> cr)
351inlineToOrg Space = return space
363inlineToOrg Space = return space
352inlineToOrg SoftBreak = do
364inlineToOrg SoftBreak = do