git.iko.soy/typeable/pandoc
on master help sign in
Textile writer: moved into PandocMonad.
d8a3228John MacFarlane
src/Text/Pandoc/Writers/Textile.hs+48−31
@@ -33,7 +33,8 @@ module Text.Pandoc.Writers.Textile ( writeTextile ) where
33import Control.Monad.State
33import Control.Monad.State
34import Data.Char (isSpace)
34import Data.Char (isSpace)
35import Data.List (intercalate)
35import Data.List (intercalate)
36−import Text.Pandoc.Class (PandocMonad)
36+import Text.Pandoc.Class (PandocMonad, report)
37+import Text.Pandoc.Logging
37import Text.Pandoc.Definition
38import Text.Pandoc.Definition
38import Text.Pandoc.ImageSize
39import Text.Pandoc.ImageSize
39import Text.Pandoc.Options
40import Text.Pandoc.Options
@@ -50,15 +51,20 @@ data WriterState = WriterState {
50 , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
51 , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
51 }
52 }
52
53
54+type TW = StateT WriterState
55+
53-- | Convert Pandoc to Textile.
56-- | Convert Pandoc to Textile.
54writeTextile :: PandocMonad m => WriterOptions -> Pandoc -> m String
57writeTextile :: PandocMonad m => WriterOptions -> Pandoc -> m String
55−writeTextile opts document = return $
58+writeTextile opts document =
56− evalState (pandocToTextile opts document)
59+ evalStateT (pandocToTextile opts document)
57− WriterState { stNotes = [], stListLevel = [], stStartNum = Nothing,
60+ WriterState { stNotes = [],
61+ stListLevel = [],
62+ stStartNum = Nothing,
58 stUseTags = False }
63 stUseTags = False }
59
64
60-- | Return Textile representation of document.
65-- | Return Textile representation of document.
66+pandocToTextile :: PandocMonad m
61−pandocToTextile :: WriterOptions -> Pandoc -> State WriterState String
67+ => WriterOptions -> Pandoc -> TW m String
62pandocToTextile opts (Pandoc meta blocks) = do
68pandocToTextile opts (Pandoc meta blocks) = do
63 metadata <- metaToJSON opts (blockListToTextile opts)
69 metadata <- metaToJSON opts (blockListToTextile opts)
64 (inlineListToTextile opts) meta
70 (inlineListToTextile opts) meta
@@ -70,7 +76,7 @@ pandocToTextile opts (Pandoc meta blocks) = do
70 Nothing -> return main
76 Nothing -> return main
71 Just tpl -> return $ renderTemplate' tpl context
77 Just tpl -> return $ renderTemplate' tpl context
72
78
73−withUseTags :: State WriterState a -> State WriterState a
79+withUseTags :: PandocMonad m => TW m a -> TW m a
74withUseTags action = do
80withUseTags action = do
75 oldUseTags <- gets stUseTags
81 oldUseTags <- gets stUseTags
76 modify $ \s -> s { stUseTags = True }
82 modify $ \s -> s { stUseTags = True }
@@ -102,9 +108,10 @@ escapeStringForTextile :: String -> String
102escapeStringForTextile = concatMap escapeCharForTextile
108escapeStringForTextile = concatMap escapeCharForTextile
103
109
104-- | Convert Pandoc block element to Textile.
110-- | Convert Pandoc block element to Textile.
111+blockToTextile :: PandocMonad m
105−blockToTextile :: WriterOptions -- ^ Options
112+ => WriterOptions -- ^ Options
106− -> Block -- ^ Block element
113+ -> Block -- ^ Block element
107− -> State WriterState String
114+ -> TW m String
108
115
109blockToTextile _ Null = return ""
116blockToTextile _ Null = return ""
110
117
@@ -134,9 +141,11 @@ blockToTextile opts (Para inlines) = do
134blockToTextile opts (LineBlock lns) =
141blockToTextile opts (LineBlock lns) =
135 blockToTextile opts $ linesToPara lns
142 blockToTextile opts $ linesToPara lns
136
143
137−blockToTextile _ (RawBlock f str)
144+blockToTextile _ b@(RawBlock f str)
138 | f == Format "html" || f == Format "textile" = return str
145 | f == Format "html" || f == Format "textile" = return str
139− | otherwise = return ""
146+ | otherwise = do
147+ report $ BlockNotRendered b
148+ return ""
140
149
141blockToTextile _ HorizontalRule = return "<hr />\n"
150blockToTextile _ HorizontalRule = return "<hr />\n"
142
151
@@ -262,7 +271,8 @@ listAttribsToString (startnum, numstyle, _) =
262 else "")
271 else "")
263
272
264-- | Convert bullet or ordered list item (list of blocks) to Textile.
273-- | Convert bullet or ordered list item (list of blocks) to Textile.
274+listItemToTextile :: PandocMonad m
265−listItemToTextile :: WriterOptions -> [Block] -> State WriterState String
275+ => WriterOptions -> [Block] -> TW m String
266listItemToTextile opts items = do
276listItemToTextile opts items = do
267 contents <- blockListToTextile opts items
277 contents <- blockListToTextile opts items
268 useTags <- gets stUseTags
278 useTags <- gets stUseTags
@@ -278,9 +288,10 @@ listItemToTextile opts items = do
278 Nothing -> return $ marker ++ " " ++ contents
288 Nothing -> return $ marker ++ " " ++ contents
279
289
280-- | Convert definition list item (label, list of blocks) to Textile.
290-- | Convert definition list item (label, list of blocks) to Textile.
281−definitionListItemToTextile :: WriterOptions
291+definitionListItemToTextile :: PandocMonad m
292+ => WriterOptions
282 -> ([Inline],[[Block]])
293 -> ([Inline],[[Block]])
283− -> State WriterState String
294+ -> TW m String
284definitionListItemToTextile opts (label, items) = do
295definitionListItemToTextile opts (label, items) = do
285 labelText <- inlineListToTextile opts label
296 labelText <- inlineListToTextile opts label
286 contents <- mapM (blockListToTextile opts) items
297 contents <- mapM (blockListToTextile opts) items
@@ -326,11 +337,12 @@ vcat = intercalate "\n"
326-- Auxiliary functions for tables. (TODO: these are common to HTML, MediaWiki,
337-- Auxiliary functions for tables. (TODO: these are common to HTML, MediaWiki,
327-- and Textile writers, and should be abstracted out.)
338-- and Textile writers, and should be abstracted out.)
328
339
340+tableRowToTextile :: PandocMonad m
329−tableRowToTextile :: WriterOptions
341+ => WriterOptions
330− -> [String]
342+ -> [String]
331− -> Int
343+ -> Int
332− -> [[Block]]
344+ -> [[Block]]
333− -> State WriterState String
345+ -> TW m String
334tableRowToTextile opts alignStrings rownum cols' = do
346tableRowToTextile opts alignStrings rownum cols' = do
335 let celltype = if rownum == 0 then "th" else "td"
347 let celltype = if rownum == 0 then "th" else "td"
336 let rowclass = case rownum of
348 let rowclass = case rownum of
@@ -349,11 +361,12 @@ alignmentToString alignment = case alignment of
349 AlignCenter -> "center"
361 AlignCenter -> "center"
350 AlignDefault -> "left"
362 AlignDefault -> "left"
351
363
352−tableItemToTextile :: WriterOptions
364+tableItemToTextile :: PandocMonad m
365+ => WriterOptions
353− -> String
366+ -> String
354− -> String
367+ -> String
355− -> [Block]
368+ -> [Block]
356− -> State WriterState String
369+ -> TW m String
357tableItemToTextile opts celltype align' item = do
370tableItemToTextile opts celltype align' item = do
358 let mkcell x = "<" ++ celltype ++ " align=\"" ++ align' ++ "\">" ++
371 let mkcell x = "<" ++ celltype ++ " align=\"" ++ align' ++ "\">" ++
359 x ++ "</" ++ celltype ++ ">"
372 x ++ "</" ++ celltype ++ ">"
@@ -361,19 +374,21 @@ tableItemToTextile opts celltype align' item = do
361 return $ mkcell contents
374 return $ mkcell contents
362
375
363-- | Convert list of Pandoc block elements to Textile.
376-- | Convert list of Pandoc block elements to Textile.
377+blockListToTextile :: PandocMonad m
364−blockListToTextile :: WriterOptions -- ^ Options
378+ => WriterOptions -- ^ Options
365− -> [Block] -- ^ List of block elements
379+ -> [Block] -- ^ List of block elements
366− -> State WriterState String
380+ -> TW m String
367blockListToTextile opts blocks =
381blockListToTextile opts blocks =
368 mapM (blockToTextile opts) blocks >>= return . vcat
382 mapM (blockToTextile opts) blocks >>= return . vcat
369
383
370-- | Convert list of Pandoc inline elements to Textile.
384-- | Convert list of Pandoc inline elements to Textile.
385+inlineListToTextile :: PandocMonad m
371−inlineListToTextile :: WriterOptions -> [Inline] -> State WriterState String
386+ => WriterOptions -> [Inline] -> TW m String
372inlineListToTextile opts lst =
387inlineListToTextile opts lst =
373 mapM (inlineToTextile opts) lst >>= return . concat
388 mapM (inlineToTextile opts) lst >>= return . concat
374
389
375-- | Convert Pandoc inline element to Textile.
390-- | Convert Pandoc inline element to Textile.
376−inlineToTextile :: WriterOptions -> Inline -> State WriterState String
391+inlineToTextile :: PandocMonad m => WriterOptions -> Inline -> TW m String
377
392
378inlineToTextile opts (Span _ lst) =
393inlineToTextile opts (Span _ lst) =
379 inlineListToTextile opts lst
394 inlineListToTextile opts lst
@@ -430,11 +445,13 @@ inlineToTextile _ (Str str) = return $ escapeStringForTextile str
430inlineToTextile _ (Math _ str) =
445inlineToTextile _ (Math _ str) =
431 return $ "<span class=\"math\">" ++ escapeStringForXML str ++ "</math>"
446 return $ "<span class=\"math\">" ++ escapeStringForXML str ++ "</math>"
432
447
433−inlineToTextile opts (RawInline f str)
448+inlineToTextile opts il@(RawInline f str)
434 | f == Format "html" || f == Format "textile" = return str
449 | f == Format "html" || f == Format "textile" = return str
435 | (f == Format "latex" || f == Format "tex") &&
450 | (f == Format "latex" || f == Format "tex") &&
436 isEnabled Ext_raw_tex opts = return str
451 isEnabled Ext_raw_tex opts = return str
437− | otherwise = return ""
452+ | otherwise = do
453+ report $ InlineNotRendered il
454+ return ""
438
455
439inlineToTextile _ LineBreak = return "\n"
456inlineToTextile _ LineBreak = return "\n"
440
457