From c264dc4f5b86688552f663f1487d135b035f0b62 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 31 Dec 2011 11:39:33 -0800 Subject: Pretty: Added beforeNonBreak combinator. This allows you to include something conditionally on it being before a nonblank. Used for RST inline math. --- src/Text/Pandoc/Pretty.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/Text/Pandoc') diff --git a/src/Text/Pandoc/Pretty.hs b/src/Text/Pandoc/Pretty.hs index 5c6eee27c..042f247ad 100644 --- a/src/Text/Pandoc/Pretty.hs +++ b/src/Text/Pandoc/Pretty.hs @@ -42,6 +42,7 @@ module Text.Pandoc.Pretty ( , flush , nest , hang + , beforeNonBlank , nowrap , offset , height @@ -91,6 +92,7 @@ type DocState a = State (RenderState a) () data D = Text Int String | Block Int [String] | Prefixed String Doc + | BeforeNonBlank Doc | Flush Doc | BreakingSpace | CarriageReturn @@ -107,6 +109,14 @@ instance Show Doc where instance IsString Doc where fromString = text +isBlank :: D -> Bool +isBlank BreakingSpace = True +isBlank CarriageReturn = True +isBlank NewLine = True +isBlank BlankLine = True +isBlank (Text _ (c:_)) = isSpace c +isBlank _ = False + -- | True if the document is empty. isEmpty :: Doc -> Bool isEmpty = null . toList . unDoc @@ -241,6 +251,12 @@ renderList (Flush d : xs) = do modify $ \s -> s{ usePrefix = oldUsePrefix } renderList xs +renderList (BeforeNonBlank d : xs) = + case xs of + (x:_) | isBlank x -> renderList xs + | otherwise -> renderDoc d >> renderList xs + [] -> renderList xs + renderList (BlankLine : xs) = do st <- get case output st of @@ -371,6 +387,11 @@ nest ind = prefixed (replicate ind ' ') hang :: Int -> Doc -> Doc -> Doc hang ind start doc = start <> nest ind doc +-- | @beforeNonBlank d@ conditionally includes @d@ unless it is +-- followed by blank space. +beforeNonBlank :: Doc -> Doc +beforeNonBlank d = Doc $ singleton (BeforeNonBlank d) + -- | Makes a 'Doc' non-reflowable. nowrap :: Doc -> Doc nowrap doc = Doc $ fromList $ map replaceSpace $ toList $ unDoc doc -- cgit v1.2.3