From 57228505d0f6e0acd739c39246de6342b2f33915 Mon Sep 17 00:00:00 2001 From: defanor Date: Fri, 1 Dec 2017 08:55:39 +0300 Subject: Reduce spacing between blocks Insert empty lines conditionally instead of doing that between every pair of blocks. It can be improved further, but looks slightly better than it used to this way. --- Pancake/Rendering.hs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'Pancake') diff --git a/Pancake/Rendering.hs b/Pancake/Rendering.hs index a917104..55aabd6 100644 --- a/Pancake/Rendering.hs +++ b/Pancake/Rendering.hs @@ -484,9 +484,36 @@ renderBlock (P.Div attr b) = do i $ renderBlocks b renderBlock P.Null = pure () --- | Renders block elements with empy lines between them. +-- | Checks whether a block is a list (ordered-, bullet-, or +-- definition-). +isList :: P.Block -> Bool +isList P.OrderedList {} = True +isList P.BulletList {} = True +isList P.DefinitionList {} = True +isList _ = False + +-- | Determines whether a line should be skipped before a block. +skipBefore :: P.Block -> Bool +skipBefore P.Header {} = True +skipBefore P.Para {} = True +skipBefore (P.Div _ (b:_)) = skipBefore b +skipBefore _ = False + +-- | Determines whether a line should be skipped after a block. +skipAfter :: P.Block -> Bool +skipAfter P.Header {} = True +skipAfter P.Para {} = True +skipAfter (P.Div _ bs@(_:_)) = skipAfter $ last bs +skipAfter b = isList b + +-- | Renders block elements with empty lines between some of them. renderBlocks :: [P.Block] -> Renderer () -renderBlocks b = sequence_ (intersperse (storeLines [[]]) $ map renderBlock b) +renderBlocks [] = pure () +renderBlocks [b] = renderBlock b +renderBlocks (b1:bs@(b2:_)) = do + renderBlock b1 + when (skipAfter b1 || skipBefore b2) $ storeLines [[]] + renderBlocks bs -- | Renders a document. renderDoc :: Int -- cgit v1.2.3