summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Pretty.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <fiddlosopher@gmail.com>2011-12-31 11:39:33 -0800
committerJohn MacFarlane <fiddlosopher@gmail.com>2011-12-31 11:39:33 -0800
commitc264dc4f5b86688552f663f1487d135b035f0b62 (patch)
treec2b234c0990c96532fc640db5b9474e2418a1630 /src/Text/Pandoc/Pretty.hs
parentc7f6f779089e0336a7070cd2a9b614a8e98e9515 (diff)
Pretty: Added beforeNonBreak combinator.
This allows you to include something conditionally on it being before a nonblank. Used for RST inline math.
Diffstat (limited to 'src/Text/Pandoc/Pretty.hs')
-rw-r--r--src/Text/Pandoc/Pretty.hs21
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