git.iko.soy/typeable/pandoc
on master help sign in
AsciiDoc writer: Double markers in intraword emphasis.

Closes #1441.

commit0f01421f81f6075d7f76e66b3d2515cddf5ccdb9 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 20 Jul 2014, 12:24 (12 years ago) parenta7b6453
3 files changed+72−18
src/Text/Pandoc/Writers/AsciiDoc.hs+46−11 tests/Tests/Writers/AsciiDoc.hs+22−3 tests/writer.asciidoc+4−4
src/Text/Pandoc/Writers/AsciiDoc.hs+46−11
@@ -49,10 +49,12 @@ import Control.Monad.State
4949import qualified Data.Map as M
5050import Data.Aeson (Value(String), fromJSON, toJSON, Result(..))
5151import qualified Data.Text as T
52+import Control.Applicative ((<*), (*>))
5253
5354data WriterState = WriterState { defListMarker :: String
5455 , orderedListLevel :: Int
5556 , bulletListLevel :: Int
57+ , intraword :: Bool
5658 }
5759
5860-- | Convert Pandoc to AsciiDoc.
@@ -62,6 +64,7 @@ writeAsciiDoc opts document =
6264 defListMarker = "::"
6365 , orderedListLevel = 1
6466 , bulletListLevel = 1
67+ , intraword = False
6568 }
6669
6770-- | Return asciidoc representation of document.
@@ -123,7 +126,7 @@ blockToAsciiDoc _ Null = return empty
123126blockToAsciiDoc opts (Plain inlines) = do
124127 contents <- inlineListToAsciiDoc opts inlines
125128 return $ contents <> cr
126−blockToAsciiDoc opts (Para [Image alt (src,'f':'i':'g':':':tit)]) =
129+blockToAsciiDoc opts (Para [Image alt (src,'f':'i':'g':':':tit)]) = do
127130 blockToAsciiDoc opts (Para [Image alt (src,tit)])
128131blockToAsciiDoc opts (Para inlines) = do
129132 contents <- inlineListToAsciiDoc opts inlines
@@ -317,17 +320,51 @@ blockListToAsciiDoc opts blocks = cat `fmap` mapM (blockToAsciiDoc opts) blocks
317320
318321-- | Convert list of Pandoc inline elements to asciidoc.
319322inlineListToAsciiDoc :: WriterOptions -> [Inline] -> State WriterState Doc
320−inlineListToAsciiDoc opts lst =
321− mapM (inlineToAsciiDoc opts) lst >>= return . cat
323+inlineListToAsciiDoc opts lst = do
324+ oldIntraword <- gets intraword
325+ setIntraword False
326+ result <- go lst
327+ setIntraword oldIntraword
328+ return result
329+ where go [] = return empty
330+ go (y:x:xs)
331+ | not (isSpacy y) = do
332+ y' <- if isSpacy x
333+ then inlineToAsciiDoc opts y
334+ else withIntraword $ inlineToAsciiDoc opts y
335+ x' <- withIntraword $ inlineToAsciiDoc opts x
336+ xs' <- go xs
337+ return (y' <> x' <> xs')
338+ | x /= Space && x /= LineBreak = do
339+ y' <- withIntraword $ inlineToAsciiDoc opts y
340+ xs' <- go (x:xs)
341+ return (y' <> xs')
342+ go (x:xs) = do
343+ x' <- inlineToAsciiDoc opts x
344+ xs' <- go xs
345+ return (x' <> xs')
346+ isSpacy Space = True
347+ isSpacy LineBreak = True
348+ isSpacy _ = False
349+
350+setIntraword :: Bool -> State WriterState ()
351+setIntraword b = modify $ \st -> st{ intraword = b }
352+
353+withIntraword :: State WriterState a -> State WriterState a
354+withIntraword p = setIntraword True *> p <* setIntraword False
322355
323356-- | Convert Pandoc inline element to asciidoc.
324357inlineToAsciiDoc :: WriterOptions -> Inline -> State WriterState Doc
325358inlineToAsciiDoc opts (Emph lst) = do
326359 contents <- inlineListToAsciiDoc opts lst
327− return $ "_" <> contents <> "_"
360+ isIntraword <- gets intraword
361+ let marker = if isIntraword then "__" else "_"
362+ return $ marker <> contents <> marker
328363inlineToAsciiDoc opts (Strong lst) = do
329364 contents <- inlineListToAsciiDoc opts lst
330− return $ "*" <> contents <> "*"
365+ isIntraword <- gets intraword
366+ let marker = if isIntraword then "**" else "*"
367+ return $ marker <> contents <> marker
331368inlineToAsciiDoc opts (Strikeout lst) = do
332369 contents <- inlineListToAsciiDoc opts lst
333370 return $ "[line-through]*" <> contents <> "*"
@@ -338,12 +375,10 @@ inlineToAsciiDoc opts (Subscript lst) = do
338375 contents <- inlineListToAsciiDoc opts lst
339376 return $ "~" <> contents <> "~"
340377inlineToAsciiDoc opts (SmallCaps lst) = inlineListToAsciiDoc opts lst
341−inlineToAsciiDoc opts (Quoted SingleQuote lst) = do
342− contents <- inlineListToAsciiDoc opts lst
343− return $ "`" <> contents <> "'"
344−inlineToAsciiDoc opts (Quoted DoubleQuote lst) = do
345− contents <- inlineListToAsciiDoc opts lst
346− return $ "``" <> contents <> "''"
378+inlineToAsciiDoc opts (Quoted SingleQuote lst) =
379+ inlineListToAsciiDoc opts (Str "`" : lst ++ [Str "'"])
380+inlineToAsciiDoc opts (Quoted DoubleQuote lst) =
381+ inlineListToAsciiDoc opts (Str "``" : lst ++ [Str "''"])
347382inlineToAsciiDoc _ (Code _ str) = return $
348383 text "`" <> text (escapeStringUsing (backslashEscapes "`") str) <> "`"
349384inlineToAsciiDoc _ (Str str) = return $ text $ escapeString str
tests/Tests/Writers/AsciiDoc.hs+22−3
@@ -1,4 +1,3 @@
1−{-# LANGUAGE OverloadedStrings #-}
21module Tests.Writers.AsciiDoc (tests) where
32
43import Test.Framework
@@ -12,7 +11,27 @@ asciidoc :: (ToString a, ToPandoc a) => a -> String
1211asciidoc = writeAsciiDoc def{ writerWrapText = False } . toPandoc
1312
1413tests :: [Test]
15−tests = [ testGroup "tables"
14+tests = [ testGroup "emphasis"
15+ [ test asciidoc "emph word before" $
16+ para (text "foo" <> emph (text "bar")) =?>
17+ "foo__bar__"
18+ , test asciidoc "emph word after" $
19+ para (emph (text "foo") <> text "bar") =?>
20+ "__foo__bar"
21+ , test asciidoc "emph quoted" $
22+ para (doubleQuoted (emph (text "foo"))) =?>
23+ "``__foo__''"
24+ , test asciidoc "strong word before" $
25+ para (text "foo" <> strong (text "bar")) =?>
26+ "foo**bar**"
27+ , test asciidoc "strong word after" $
28+ para (strong (text "foo") <> text "bar") =?>
29+ "**foo**bar"
30+ , test asciidoc "strong quoted" $
31+ para (singleQuoted (strong (text "foo"))) =?>
32+ "`**foo**'"
33+ ]
34+ , testGroup "tables"
1635 [ test asciidoc "empty cells" $
1736 simpleTable [] [[mempty],[mempty]] =?> unlines
1837 [ "[cols=\"\",]"
@@ -22,7 +41,7 @@ tests = [ testGroup "tables"
2241 , "|===="
2342 ]
2443 , test asciidoc "multiblock cells" $
25− simpleTable [] [[para "Para 1" <> para "Para 2"]]
44+ simpleTable [] [[para (text "Para 1") <> para (text "Para 2")]]
2645 =?> unlines
2746 [ "[cols=\"\",]"
2847 , "|====="
tests/writer.asciidoc+4−4
@@ -429,11 +429,11 @@ Hr’s:
429429Inline Markup
430430-------------
431431
432−This is _emphasized_, and so _is this_.
432+This is __emphasized__, and so __is this__.
433433
434−This is *strong*, and so *is this*.
434+This is **strong**, and so **is this**.
435435
436−An _link:/url[emphasized link]_.
436+An __link:/url[emphasized link]__.
437437
438438*_This is strong and em._*
439439
@@ -445,7 +445,7 @@ So is *_this_* word.
445445
446446This is code: `>`, `$`, `\`, `\$`, `<html>`.
447447
448−[line-through]*This is _strikeout_.*
448+[line-through]*This is __strikeout__.*
449449
450450Superscripts: a^bc^d a^_hello_^ a^hello there^.
451451