git.iko.soy/typeable/pandoc
on master help sign in
AsciiDoc writer: avoid unnecessary use of "unconstrained" emphasis.
46d8b42John MacFarlane
src/Text/Pandoc/Writers/AsciiDoc.hs+17−7
@@ -51,6 +51,7 @@ import Control.Monad.State
51import qualified Data.Map as M
51import 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
54+import Data.Char (isSpace, isPunctuation)
54
55
55data WriterState = WriterState { defListMarker :: String
56data WriterState = WriterState { defListMarker :: String
56 , orderedListLevel :: Int
57 , orderedListLevel :: Int
@@ -321,6 +322,8 @@ blockListToAsciiDoc :: WriterOptions -- ^ Options
321 -> State WriterState Doc
322 -> State WriterState Doc
322blockListToAsciiDoc opts blocks = cat `fmap` mapM (blockToAsciiDoc opts) blocks
323blockListToAsciiDoc opts blocks = cat `fmap` mapM (blockToAsciiDoc opts) blocks
323
324
325+data SpacyLocation = End | Start
326+
324-- | Convert list of Pandoc inline elements to asciidoc.
327-- | Convert list of Pandoc inline elements to asciidoc.
325inlineListToAsciiDoc :: WriterOptions -> [Inline] -> State WriterState Doc
328inlineListToAsciiDoc :: WriterOptions -> [Inline] -> State WriterState Doc
326inlineListToAsciiDoc opts lst = do
329inlineListToAsciiDoc opts lst = do
@@ -331,14 +334,14 @@ inlineListToAsciiDoc opts lst = do
331 return result
334 return result
332 where go [] = return empty
335 where go [] = return empty
333 go (y:x:xs)
336 go (y:x:xs)
334− | not (isSpacy y) = do
337+ | not (isSpacy End y) = do
335− y' <- if isSpacy x
338+ y' <- if isSpacy Start x
336 then inlineToAsciiDoc opts y
339 then inlineToAsciiDoc opts y
337 else withIntraword $ inlineToAsciiDoc opts y
340 else withIntraword $ inlineToAsciiDoc opts y
338 x' <- withIntraword $ inlineToAsciiDoc opts x
341 x' <- withIntraword $ inlineToAsciiDoc opts x
339 xs' <- go xs
342 xs' <- go xs
340 return (y' <> x' <> xs')
343 return (y' <> x' <> xs')
341− | not (isSpacy x) = do
344+ | not (isSpacy Start x) = do
342 y' <- withIntraword $ inlineToAsciiDoc opts y
345 y' <- withIntraword $ inlineToAsciiDoc opts y
343 xs' <- go (x:xs)
346 xs' <- go (x:xs)
344 return (y' <> xs')
347 return (y' <> xs')
@@ -346,10 +349,17 @@ inlineListToAsciiDoc opts lst = do
346 x' <- inlineToAsciiDoc opts x
349 x' <- inlineToAsciiDoc opts x
347 xs' <- go xs
350 xs' <- go xs
348 return (x' <> xs')
351 return (x' <> xs')
352+ isSpacy :: SpacyLocation -> Inline -> Bool
349− isSpacy Space = True
353+ isSpacy _ Space = True
350− isSpacy LineBreak = True
354+ isSpacy _ LineBreak = True
351− isSpacy SoftBreak = True
355+ isSpacy _ SoftBreak = True
356+ -- Note that \W characters count as spacy in AsciiDoc
357+ -- for purposes of determining interword:
358+ isSpacy End (Str xs) = case reverse xs of
359+ c:_ -> isPunctuation c || isSpace c
360+ _ -> False
361+ isSpacy Start (Str (c:_)) = isPunctuation c || isSpace c
352− isSpacy _ = False
362+ isSpacy _ _ = False
353
363
354setIntraword :: Bool -> State WriterState ()
364setIntraword :: Bool -> State WriterState ()
355setIntraword b = modify $ \st -> st{ intraword = b }
365setIntraword b = modify $ \st -> st{ intraword = b }
tests/writer.asciidoc+4−4
@@ -435,11 +435,11 @@ Hr’s:
435Inline Markup
435Inline Markup
436-------------
436-------------
437
437
438−This is __emphasized__, and so __is this__.
438+This is _emphasized_, and so _is this_.
439
439
440−This is **strong**, and so **is this**.
440+This is *strong*, and so *is this*.
441
441
442−An __link:/url[emphasized link]__.
442+An _link:/url[emphasized link]_.
443
443
444*_This is strong and em._*
444*_This is strong and em._*
445
445
@@ -451,7 +451,7 @@ So is *_this_* word.
451
451
452This is code: `>`, `$`, `\`, `\$`, `<html>`.
452This is code: `>`, `$`, `\`, `\$`, `<html>`.
453
453
454−[line-through]*This is __strikeout__.*
454+[line-through]*This is _strikeout_.*
455
455
456Superscripts: a^bc^d a^_hello_^ a^hello there^.
456Superscripts: a^bc^d a^_hello_^ a^hello there^.
457
457