@@ -79,6 +79,7 @@ module Text.Pandoc.Shared (
7979 makeMeta,
8080 eastAsianLineBreakFilter,
8181 underlineSpan,
82+ splitSentences,
8283 -- * TagSoup HTML handling
8384 renderTags',
8485 -- * File handling
@@ -582,6 +583,31 @@ eastAsianLineBreakFilter = bottomUp go
582583underlineSpan :: Inlines -> Inlines
583584underlineSpan = B.spanWith ("", ["underline"], [])
584585
586+-- | Returns the first sentence in a list of inlines, and the rest.
587+breakSentence :: [Inline] -> ([Inline], [Inline])
588+breakSentence [] = ([],[])
589+breakSentence xs =
590+ let isSentenceEndInline (Str ys@(_:_)) | last ys == '.' = True
591+ isSentenceEndInline (Str ys@(_:_)) | last ys == '?' = True
592+ isSentenceEndInline LineBreak = True
593+ isSentenceEndInline _ = False
594+ (as, bs) = break isSentenceEndInline xs
595+ in case bs of
596+ [] -> (as, [])
597+ [c] -> (as ++ [c], [])
598+ (c:Space:cs) -> (as ++ [c], cs)
599+ (c:SoftBreak:cs) -> (as ++ [c], cs)
600+ (Str ".":Str (')':ys):cs) -> (as ++ [Str ".", Str (')':ys)], cs)
601+ (x@(Str ('.':')':_)):cs) -> (as ++ [x], cs)
602+ (LineBreak:x@(Str ('.':_)):cs) -> (as ++[LineBreak], x:cs)
603+ (c:cs) -> (as ++ [c] ++ ds, es)
604+ where (ds, es) = breakSentence cs
605+
606+-- | Split a list of inlines into sentences.
607+splitSentences :: [Inline] -> [[Inline]]
608+splitSentences xs =
609+ let (sent, rest) = breakSentence xs
610+ in if null rest then [sent] else sent : splitSentences rest
585611
586612--
587613-- TagSoup HTML handling