git.iko.soy/typeable/pandoc
on master help sign in
Shared: new export `splitSentences` [API change].

This was duplicated in the Man and Ms writers, and really belongs in Shared.

commite25c7ce035e7af220d3715ab156eabd01f861e4e authorJohn MacFarlane <jgm@berkeley.edu> dateMon 1 Oct 2018, 22:47 (8 years ago) parent963ba93
3 files changed+26−52
src/Text/Pandoc/Shared.hs+26 src/Text/Pandoc/Writers/Man.hs−26 src/Text/Pandoc/Writers/Ms.hs−26
src/Text/Pandoc/Shared.hs+26
@@ -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
src/Text/Pandoc/Writers/Man.hs−26
@@ -154,32 +154,6 @@ escapeCode = intercalate "\n" . map escapeLine . lines where
154154-- line. groff/troff treats the line-ending period differently.
155155-- See http://code.google.com/p/pandoc/issues/detail?id=148.
156156
157−-- | Returns the first sentence in a list of inlines, and the rest.
158−breakSentence :: [Inline] -> ([Inline], [Inline])
159−breakSentence [] = ([],[])
160−breakSentence xs =
161− let isSentenceEndInline (Str ys@(_:_)) | last ys == '.' = True
162− isSentenceEndInline (Str ys@(_:_)) | last ys == '?' = True
163− isSentenceEndInline LineBreak = True
164− isSentenceEndInline _ = False
165− (as, bs) = break isSentenceEndInline xs
166− in case bs of
167− [] -> (as, [])
168− [c] -> (as ++ [c], [])
169− (c:Space:cs) -> (as ++ [c], cs)
170− (c:SoftBreak:cs) -> (as ++ [c], cs)
171− (Str ".":Str (')':ys):cs) -> (as ++ [Str ".", Str (')':ys)], cs)
172− (x@(Str ('.':')':_)):cs) -> (as ++ [x], cs)
173− (LineBreak:x@(Str ('.':_)):cs) -> (as ++[LineBreak], x:cs)
174− (c:cs) -> (as ++ [c] ++ ds, es)
175− where (ds, es) = breakSentence cs
176−
177−-- | Split a list of inlines into sentences.
178−splitSentences :: [Inline] -> [[Inline]]
179−splitSentences xs =
180− let (sent, rest) = breakSentence xs
181− in if null rest then [sent] else sent : splitSentences rest
182−
183157-- | Convert Pandoc block element to man.
184158blockToMan :: PandocMonad m
185159 => WriterOptions -- ^ Options
src/Text/Pandoc/Writers/Ms.hs−26
@@ -190,32 +190,6 @@ escapeCode = intercalate "\n" . map escapeLine . lines
190190-- line. groff/troff treats the line-ending period differently.
191191-- See http://code.google.com/p/pandoc/issues/detail?id=148.
192192
193−-- | Returns the first sentence in a list of inlines, and the rest.
194−breakSentence :: [Inline] -> ([Inline], [Inline])
195−breakSentence [] = ([],[])
196−breakSentence xs =
197− let isSentenceEndInline (Str ys@(_:_)) | last ys == '.' = True
198− isSentenceEndInline (Str ys@(_:_)) | last ys == '?' = True
199− isSentenceEndInline LineBreak = True
200− isSentenceEndInline _ = False
201− (as, bs) = break isSentenceEndInline xs
202− in case bs of
203− [] -> (as, [])
204− [c] -> (as ++ [c], [])
205− (c:Space:cs) -> (as ++ [c], cs)
206− (c:SoftBreak:cs) -> (as ++ [c], cs)
207− (Str ".":Str (')':ys):cs) -> (as ++ [Str ".", Str (')':ys)], cs)
208− (x@(Str ('.':')':_)):cs) -> (as ++ [x], cs)
209− (LineBreak:x@(Str ('.':_)):cs) -> (as ++[LineBreak], x:cs)
210− (c:cs) -> (as ++ [c] ++ ds, es)
211− where (ds, es) = breakSentence cs
212−
213−-- | Split a list of inlines into sentences.
214−splitSentences :: [Inline] -> [[Inline]]
215−splitSentences xs =
216− let (sent, rest) = breakSentence xs
217− in if null rest then [sent] else sent : splitSentences rest
218−
219193blockToMs :: PandocMonad m
220194 => WriterOptions -- ^ Options
221195 -> Block -- ^ Block element