From 33b4bc837196582956d8bd5538b6676be6a0f8e9 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 5 Dec 2016 00:49:53 +0100 Subject: Pretty: Added `afterBreak`. This makes it possible to insert escape codes for content that needs escaping at the beginning of a line. --- src/Text/Pandoc/Pretty.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Text/Pandoc/Pretty.hs b/src/Text/Pandoc/Pretty.hs index b9f646282..256f38b0c 100644 --- a/src/Text/Pandoc/Pretty.hs +++ b/src/Text/Pandoc/Pretty.hs @@ -45,6 +45,7 @@ module Text.Pandoc.Pretty ( , hang , beforeNonBlank , nowrap + , afterBreak , offset , minOffset , height @@ -101,6 +102,7 @@ data D = Text Int String | BeforeNonBlank Doc | Flush Doc | BreakingSpace + | AfterBreak String | CarriageReturn | NewLine | BlankLines Int -- number of blank lines @@ -306,6 +308,7 @@ renderList (BreakingSpace : BreakingSpace : xs) = renderList (BreakingSpace:xs) renderList (BreakingSpace : xs) = do let isText (Text _ _) = True isText (Block _ _) = True + isText (AfterBreak _) = True isText _ = False let isBreakingSpace BreakingSpace = True isBreakingSpace _ = False @@ -321,6 +324,13 @@ renderList (BreakingSpace : xs) = do outp 1 " " renderList xs' +renderList (AfterBreak s : xs) = do + st <- get + if newlines st > 0 + then outp (realLength s) s + else return () + renderList xs + renderList (Block i1 s1 : Block i2 s2 : xs) = renderList (mergeBlocks False (IsBlock i1 s1) (IsBlock i2 s2) : xs) @@ -422,6 +432,11 @@ nowrap doc = Doc $ mapWithIndex replaceSpace $ unDoc doc where replaceSpace _ BreakingSpace = Text 1 " " replaceSpace _ x = x +-- | Content to print only if it comes at the beginning of a line, +-- to be used e.g. for escaping line-initial `.` in groff man. +afterBreak :: String -> Doc +afterBreak s = Doc $ singleton (AfterBreak s) + -- | Returns the width of a 'Doc'. offset :: Doc -> Int offset d = case map realLength . lines . render Nothing $ d of -- cgit v1.2.3