@@ -52,7 +52,8 @@ import qualified Data.Map as M
52import Data.Aeson (Value(String), fromJSON, toJSON, Result(..))
52import Data.Aeson (Value(String), fromJSON, toJSON, Result(..))
53import qualified Data.Text as T
53import qualified Data.Text as T
54import Data.Char (isSpace, isPunctuation)
54import Data.Char (isSpace, isPunctuation)
55−import Text.Pandoc.Class (PandocMonad)
55+import Text.Pandoc.Class (PandocMonad, report)
56+import Text.Pandoc.Logging
57data WriterState = WriterState { defListMarker :: String
58data WriterState = WriterState { defListMarker :: String
58 , orderedListLevel :: Int
59 , orderedListLevel :: Int
@@ -62,16 +63,18 @@ data WriterState = WriterState { defListMarker :: String
63-- | Convert Pandoc to AsciiDoc.
64-- | Convert Pandoc to AsciiDoc.
64writeAsciiDoc :: PandocMonad m => WriterOptions -> Pandoc -> m String
65writeAsciiDoc :: PandocMonad m => WriterOptions -> Pandoc -> m String
65−writeAsciiDoc opts document = return $
66+writeAsciiDoc opts document =
66− evalState (pandocToAsciiDoc opts document) WriterState{
67+ evalStateT (pandocToAsciiDoc opts document) WriterState{
67 defListMarker = "::"
68 defListMarker = "::"
68 , orderedListLevel = 1
69 , orderedListLevel = 1
69 , bulletListLevel = 1
70 , bulletListLevel = 1
70 , intraword = False
71 , intraword = False
74+type ADW = StateT WriterState
73-- | Return asciidoc representation of document.
76-- | Return asciidoc representation of document.
74−pandocToAsciiDoc :: WriterOptions -> Pandoc -> State WriterState String
77+pandocToAsciiDoc :: PandocMonad m => WriterOptions -> Pandoc -> ADW m String
75pandocToAsciiDoc opts (Pandoc meta blocks) = do
78pandocToAsciiDoc opts (Pandoc meta blocks) = do
76 let titleblock = not $ null (docTitle meta) && null (docAuthors meta) &&
79 let titleblock = not $ null (docTitle meta) && null (docAuthors meta) &&
77 null (docDate meta)
80 null (docDate meta)
@@ -123,9 +126,10 @@ beginsWithOrderedListMarker str =
123 Right _ -> True
126 Right _ -> True
125-- | Convert Pandoc block element to asciidoc.
128-- | Convert Pandoc block element to asciidoc.
129+blockToAsciiDoc :: PandocMonad m
126−blockToAsciiDoc :: WriterOptions -- ^ Options
130+ => WriterOptions -- ^ Options
127 -> Block -- ^ Block element
131 -> Block -- ^ Block element
128− -> State WriterState Doc
132+ -> ADW m Doc
129blockToAsciiDoc _ Null = return empty
133blockToAsciiDoc _ Null = return empty
130blockToAsciiDoc opts (Plain inlines) = do
134blockToAsciiDoc opts (Plain inlines) = do
131 contents <- inlineListToAsciiDoc opts inlines
135 contents <- inlineListToAsciiDoc opts inlines
@@ -146,9 +150,11 @@ blockToAsciiDoc opts (LineBlock lns) = do
146 let joinWithLinefeeds = nowrap . mconcat . intersperse cr
150 let joinWithLinefeeds = nowrap . mconcat . intersperse cr
147 contents <- joinWithLinefeeds <$> mapM docify lns
151 contents <- joinWithLinefeeds <$> mapM docify lns
148 return $ "[verse]" $$ text "--" $$ contents $$ text "--" $$ blankline
152 return $ "[verse]" $$ text "--" $$ contents $$ text "--" $$ blankline
149−blockToAsciiDoc _ (RawBlock f s)
153+blockToAsciiDoc _ b@(RawBlock f s)
150 | f == "asciidoc" = return $ text s
154 | f == "asciidoc" = return $ text s
156+ report $ BlockNotRendered b
151− | otherwise = return empty
157+ return empty
152blockToAsciiDoc _ HorizontalRule =
158blockToAsciiDoc _ HorizontalRule =
153 return $ blankline <> text "'''''" <> blankline
159 return $ blankline <> text "'''''" <> blankline
154blockToAsciiDoc opts (Header level (ident,_,_) inlines) = do
160blockToAsciiDoc opts (Header level (ident,_,_) inlines) = do
@@ -269,9 +275,10 @@ blockToAsciiDoc opts (Div (ident,_,_) bs) = do
269 return $ identifier $$ contents
275 return $ identifier $$ contents
271-- | Convert bullet list item (list of blocks) to asciidoc.
277-- | Convert bullet list item (list of blocks) to asciidoc.
272−bulletListItemToAsciiDoc :: WriterOptions -> [Block] -> State WriterState Doc
278+bulletListItemToAsciiDoc :: PandocMonad m
279+ => WriterOptions -> [Block] -> ADW m Doc
273bulletListItemToAsciiDoc opts blocks = do
280bulletListItemToAsciiDoc opts blocks = do
274− let addBlock :: Doc -> Block -> State WriterState Doc
281+ let addBlock :: PandocMonad m => Doc -> Block -> ADW m Doc
275 addBlock d b | isEmpty d = chomp `fmap` blockToAsciiDoc opts b
282 addBlock d b | isEmpty d = chomp `fmap` blockToAsciiDoc opts b
276 addBlock d b@(BulletList _) = do x <- blockToAsciiDoc opts b
283 addBlock d b@(BulletList _) = do x <- blockToAsciiDoc opts b
277 return $ d <> cr <> chomp x
284 return $ d <> cr <> chomp x
@@ -287,13 +294,13 @@ bulletListItemToAsciiDoc opts blocks = do
287 return $ marker <> text " " <> contents <> cr
294 return $ marker <> text " " <> contents <> cr
289-- | Convert ordered list item (a list of blocks) to asciidoc.
296-- | Convert ordered list item (a list of blocks) to asciidoc.
297+orderedListItemToAsciiDoc :: PandocMonad m
290−orderedListItemToAsciiDoc :: WriterOptions -- ^ options
298+ => WriterOptions -- ^ options
291 -> String -- ^ list item marker
299 -> String -- ^ list item marker
292 -> [Block] -- ^ list item (list of blocks)
300 -> [Block] -- ^ list item (list of blocks)
293− -> State WriterState Doc
301+ -> ADW m Doc
294orderedListItemToAsciiDoc opts marker blocks = do
302orderedListItemToAsciiDoc opts marker blocks = do
295− let addBlock :: Doc -> Block -> State WriterState Doc
296− addBlock d b | isEmpty d = chomp `fmap` blockToAsciiDoc opts b
303+ let addBlock d b | isEmpty d = chomp `fmap` blockToAsciiDoc opts b
297 addBlock d b@(BulletList _) = do x <- blockToAsciiDoc opts b
304 addBlock d b@(BulletList _) = do x <- blockToAsciiDoc opts b
298 return $ d <> cr <> chomp x
305 return $ d <> cr <> chomp x
299 addBlock d b@(OrderedList _ _) = do x <- blockToAsciiDoc opts b
306 addBlock d b@(OrderedList _ _) = do x <- blockToAsciiDoc opts b
@@ -307,9 +314,10 @@ orderedListItemToAsciiDoc opts marker blocks = do
307 return $ text marker <> text " " <> contents <> cr
314 return $ text marker <> text " " <> contents <> cr
309-- | Convert definition list item (label, list of blocks) to asciidoc.
316-- | Convert definition list item (label, list of blocks) to asciidoc.
310−definitionListItemToAsciiDoc :: WriterOptions
317+definitionListItemToAsciiDoc :: PandocMonad m
311 -> ([Inline],[[Block]])
319 -> ([Inline],[[Block]])
312− -> State WriterState Doc
320+ -> ADW m Doc
313definitionListItemToAsciiDoc opts (label, defs) = do
321definitionListItemToAsciiDoc opts (label, defs) = do
314 labelText <- inlineListToAsciiDoc opts label
322 labelText <- inlineListToAsciiDoc opts label
315 marker <- defListMarker `fmap` get
323 marker <- defListMarker `fmap` get
@@ -317,7 +325,7 @@ definitionListItemToAsciiDoc opts (label, defs) = do
317 then modify (\st -> st{ defListMarker = ";;"})
325 then modify (\st -> st{ defListMarker = ";;"})
318 else modify (\st -> st{ defListMarker = "::"})
326 else modify (\st -> st{ defListMarker = "::"})
319 let divider = cr <> text "+" <> cr
327 let divider = cr <> text "+" <> cr
320− let defsToAsciiDoc :: [Block] -> State WriterState Doc
328+ let defsToAsciiDoc :: PandocMonad m => [Block] -> ADW m Doc
321 defsToAsciiDoc ds = (vcat . intersperse divider . map chomp)
329 defsToAsciiDoc ds = (vcat . intersperse divider . map chomp)
322 `fmap` mapM (blockToAsciiDoc opts) ds
330 `fmap` mapM (blockToAsciiDoc opts) ds
323 defs' <- mapM defsToAsciiDoc defs
331 defs' <- mapM defsToAsciiDoc defs
@@ -326,15 +334,16 @@ definitionListItemToAsciiDoc opts (label, defs) = do
326 return $ labelText <> text marker <> cr <> contents <> cr
334 return $ labelText <> text marker <> cr <> contents <> cr
328-- | Convert list of Pandoc block elements to asciidoc.
336-- | Convert list of Pandoc block elements to asciidoc.
337+blockListToAsciiDoc :: PandocMonad m
329−blockListToAsciiDoc :: WriterOptions -- ^ Options
338+ => WriterOptions -- ^ Options
330 -> [Block] -- ^ List of block elements
339 -> [Block] -- ^ List of block elements
331− -> State WriterState Doc
340+ -> ADW m Doc
332blockListToAsciiDoc opts blocks = cat `fmap` mapM (blockToAsciiDoc opts) blocks
341blockListToAsciiDoc opts blocks = cat `fmap` mapM (blockToAsciiDoc opts) blocks
334data SpacyLocation = End | Start
343data SpacyLocation = End | Start
336-- | Convert list of Pandoc inline elements to asciidoc.
345-- | Convert list of Pandoc inline elements to asciidoc.
337−inlineListToAsciiDoc :: WriterOptions -> [Inline] -> State WriterState Doc
346+inlineListToAsciiDoc :: PandocMonad m => WriterOptions -> [Inline] -> ADW m Doc
338inlineListToAsciiDoc opts lst = do
347inlineListToAsciiDoc opts lst = do
339 oldIntraword <- gets intraword
348 oldIntraword <- gets intraword
340 setIntraword False
349 setIntraword False
@@ -370,14 +379,14 @@ inlineListToAsciiDoc opts lst = do
370 isSpacy Start (Str (c:_)) = isPunctuation c || isSpace c
379 isSpacy Start (Str (c:_)) = isPunctuation c || isSpace c
371 isSpacy _ _ = False
380 isSpacy _ _ = False
373−setIntraword :: Bool -> State WriterState ()
382+setIntraword :: PandocMonad m => Bool -> ADW m ()
374setIntraword b = modify $ \st -> st{ intraword = b }
383setIntraword b = modify $ \st -> st{ intraword = b }
376−withIntraword :: State WriterState a -> State WriterState a
385+withIntraword :: PandocMonad m => ADW m a -> ADW m a
377withIntraword p = setIntraword True *> p <* setIntraword False
386withIntraword p = setIntraword True *> p <* setIntraword False
379-- | Convert Pandoc inline element to asciidoc.
388-- | Convert Pandoc inline element to asciidoc.
380−inlineToAsciiDoc :: WriterOptions -> Inline -> State WriterState Doc
389+inlineToAsciiDoc :: PandocMonad m => WriterOptions -> Inline -> ADW m Doc
381inlineToAsciiDoc opts (Emph lst) = do
390inlineToAsciiDoc opts (Emph lst) = do
382 contents <- inlineListToAsciiDoc opts lst
391 contents <- inlineListToAsciiDoc opts lst
383 isIntraword <- gets intraword
392 isIntraword <- gets intraword
@@ -409,8 +418,11 @@ inlineToAsciiDoc _ (Math InlineMath str) =
409 return $ "latexmath:[$" <> text str <> "$]"
418 return $ "latexmath:[$" <> text str <> "$]"
410inlineToAsciiDoc _ (Math DisplayMath str) =
419inlineToAsciiDoc _ (Math DisplayMath str) =
411 return $ "latexmath:[\\[" <> text str <> "\\]]"
420 return $ "latexmath:[\\[" <> text str <> "\\]]"
412−inlineToAsciiDoc _ (RawInline f s)
421+inlineToAsciiDoc _ il@(RawInline f s)
413 | f == "asciidoc" = return $ text s
422 | f == "asciidoc" = return $ text s
424+ report $ InlineNotRendered il
414 | otherwise = return empty
426 | otherwise = return empty
415inlineToAsciiDoc _ LineBreak = return $ " +" <> cr
427inlineToAsciiDoc _ LineBreak = return $ " +" <> cr
416inlineToAsciiDoc _ Space = return space
428inlineToAsciiDoc _ Space = return space