diff options
Diffstat (limited to 'src/Text/Pandoc/Pretty.hs')
-rw-r--r-- | src/Text/Pandoc/Pretty.hs | 21 |
1 files changed, 21 insertions, 0 deletions
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 |