@@ -31,7 +31,7 @@ Conversion of 'Pandoc' documents to reStructuredText.
3131
3232reStructuredText: <http://docutils.sourceforge.net/rst.html>
3333-}
34−module Text.Pandoc.Writers.RST ( writeRST ) where
34+module Text.Pandoc.Writers.RST ( writeRST, flatten ) where
3535import Prelude
3636import Control.Monad.State.Strict
3737import Data.Char (isSpace, toLower)
@@ -377,8 +377,10 @@ blockListToRST :: PandocMonad m
377377blockListToRST = blockListToRST' False
378378
379379transformInlines :: [Inline] -> [Inline]
380−transformInlines = stripLeadingTrailingSpace . insertBS
381− . filter hasContents . removeSpaceAfterDisplayMath
380+transformInlines = insertBS .
381+ filter hasContents .
382+ removeSpaceAfterDisplayMath .
383+ concatMap (transformNested . flatten)
382384 where -- empty inlines are not valid RST syntax
383385 hasContents :: Inline -> Bool
384386 hasContents (Str "") = False
@@ -412,6 +414,8 @@ transformInlines = stripLeadingTrailingSpace . insertBS
412414 x : insertBS (y : zs)
413415 insertBS (x:ys) = x : insertBS ys
414416 insertBS [] = []
417+ transformNested :: [Inline] -> [Inline]
418+ transformNested = map (mapNested stripLeadingTrailingSpace)
415419 surroundComplex :: Inline -> Inline -> Bool
416420 surroundComplex (Str s@(_:_)) (Str s'@(_:_)) =
417421 case (last s, head s') of
@@ -449,6 +453,74 @@ transformInlines = stripLeadingTrailingSpace . insertBS
449453 isComplex (Span _ (x:_)) = isComplex x
450454 isComplex _ = False
451455
456+-- | Flattens nested inlines. Extracts nested inlines and goes through
457+-- them either collapsing them in the outer inline container or
458+-- pulling them out of it
459+flatten :: Inline -> [Inline]
460+flatten outer = combineAll $ dropInlineParent outer
461+ where combineAll = foldl combine []
462+
463+ combine :: [Inline] -> Inline -> [Inline]
464+ combine f i =
465+ case (outer, i) of
466+ -- quotes are not rendered using RST inlines, so we can keep
467+ -- them and they will be readable and parsable
468+ (Quoted _ _, _) -> keep f i
469+ (_, Quoted _ _) -> keep f i
470+ -- parent inlines would prevent links from being correctly
471+ -- parsed, in this case we prioritise the content over the
472+ -- style
473+ (_, Link _ _ _) -> emerge f i
474+ -- always give priority to strong text over emphasis
475+ (Emph _, Strong _) -> emerge f i
476+ -- drop all other nested styles
477+ (_, _) -> collapse f i
478+
479+ emerge f i = f <> [i]
480+ keep f i = appendToLast f [i]
481+ collapse f i = appendToLast f $ dropInlineParent i
482+
483+ appendToLast :: [Inline] -> [Inline] -> [Inline]
484+ appendToLast [] toAppend = [setInlineChildren outer toAppend]
485+ appendToLast flattened toAppend
486+ | isOuter lastFlat = init flattened <> [appendTo lastFlat toAppend]
487+ | otherwise = flattened <> [setInlineChildren outer toAppend]
488+ where lastFlat = last flattened
489+ appendTo o i = mapNested (<> i) o
490+ isOuter i = emptyParent i == emptyParent outer
491+ emptyParent i = setInlineChildren i []
492+
493+mapNested :: ([Inline] -> [Inline]) -> Inline -> Inline
494+mapNested f i = setInlineChildren i (f (dropInlineParent i))
495+
496+dropInlineParent :: Inline -> [Inline]
497+dropInlineParent (Link _ i _) = i
498+dropInlineParent (Emph i) = i
499+dropInlineParent (Strong i) = i
500+dropInlineParent (Strikeout i) = i
501+dropInlineParent (Superscript i) = i
502+dropInlineParent (Subscript i) = i
503+dropInlineParent (SmallCaps i) = i
504+dropInlineParent (Cite _ i) = i
505+dropInlineParent (Image _ i _) = i
506+dropInlineParent (Span _ i) = i
507+dropInlineParent (Quoted _ i) = i
508+dropInlineParent i = [i] -- not a parent, like Str or Space
509+
510+setInlineChildren :: Inline -> [Inline] -> Inline
511+setInlineChildren (Link a _ t) i = Link a i t
512+setInlineChildren (Emph _) i = Emph i
513+setInlineChildren (Strong _) i = Strong i
514+setInlineChildren (Strikeout _) i = Strikeout i
515+setInlineChildren (Superscript _) i = Superscript i
516+setInlineChildren (Subscript _) i = Subscript i
517+setInlineChildren (SmallCaps _) i = SmallCaps i
518+setInlineChildren (Quoted q _) i = Quoted q i
519+setInlineChildren (Cite c _) i = Cite c i
520+setInlineChildren (Image a _ t) i = Image a i t
521+setInlineChildren (Span a _) i = Span a i
522+setInlineChildren leaf _ = leaf
523+
452524inlineListToRST :: PandocMonad m => [Inline] -> RST m Doc
453525inlineListToRST = writeInlines . walk transformInlines
454526