diff --git a/lib/Patat/Presentation/Display.hs b/lib/Patat/Presentation/Display.hs index 69c8df6..b88ce6b 100644 --- a/lib/Patat/Presentation/Display.hs +++ b/lib/Patat/Presentation/Display.hs @@ -114,7 +114,7 @@ displayPresentation size pres@Presentation {..} = Margins {..} = margins (activeSettings pres) offsetRow = (sRows canvasSize `div` 2) - (prows `div` 2) offsetCol = ((sCols canvasSize - mLeft - mRight) `div` 2) - (pcols `div` 2) - spaces = PP.NotTrimmable $ PP.spaces offsetCol in + spaces = PP.Indentation offsetCol mempty in mconcat (replicate (offsetRow - 3) PP.hardline) <$$> PP.indent spaces spaces pblock @@ -185,7 +185,7 @@ formatWith ps = wrap . indent wrap = case (psWrap ps, psColumns ps) of (Just True, Just (A.FlexibleNum col)) -> PP.wrapAt (Just $ col - mRight) _ -> id - spaces = PP.NotTrimmable $ PP.spaces mLeft + spaces = PP.Indentation mLeft mempty indent = PP.indent spaces spaces @@ -215,13 +215,13 @@ prettyBlock ds (Pandoc.CodeBlock (_, classes, _) txt) = prettyBlock ds (Pandoc.BulletList bss) = PP.vcat [ PP.indent - (PP.NotTrimmable $ themed ds themeBulletList prefix) - (PP.Trimmable " ") + (PP.Indentation 2 $ themed ds themeBulletList prefix) + (PP.Indentation 4 mempty) (prettyBlocks ds' bs) | bs <- bss ] <> PP.hardline where - prefix = " " <> PP.string [marker] <> " " + prefix = PP.string [marker] <> " " marker = case T.unpack <$> themeBulletListMarkers theme of Just (x : _) -> x _ -> '-' @@ -236,8 +236,8 @@ prettyBlock ds (Pandoc.BulletList bss) = PP.vcat prettyBlock ds (Pandoc.OrderedList _ bss) = PP.vcat [ PP.indent - (PP.NotTrimmable $ themed ds themeOrderedList $ PP.string prefix) - (PP.Trimmable " ") + (PP.Indentation 0 $ themed ds themeOrderedList $ PP.string prefix) + (PP.Indentation 4 mempty) (prettyBlocks ds bs) | (prefix, bs) <- zip padded bss ] <> PP.hardline @@ -253,7 +253,7 @@ prettyBlock _ds (Pandoc.RawBlock _ t) = PP.text t <> PP.hardline prettyBlock _ds Pandoc.HorizontalRule = "---" prettyBlock ds (Pandoc.BlockQuote bs) = - let quote = PP.NotTrimmable (themed ds themeBlockQuote "> ") in + let quote = PP.Indentation 0 (themed ds themeBlockQuote "> ") in PP.indent quote quote (themed ds themeBlockQuote $ prettyBlocks ds bs) prettyBlock ds (Pandoc.DefinitionList terms) = @@ -263,8 +263,8 @@ prettyBlock ds (Pandoc.DefinitionList terms) = themed ds themeDefinitionTerm (prettyInlines ds term) <$$> PP.hardline <> PP.vcat [ PP.indent - (PP.NotTrimmable (themed ds themeDefinitionList ": ")) - (PP.Trimmable " ") $ + (PP.Indentation 0 (themed ds themeDefinitionList ": ")) + (PP.Indentation 4 mempty) $ prettyBlocks ds (Pandoc.plainToPara definition) | definition <- definitions ] @@ -289,7 +289,7 @@ prettyBlock ds (Pandoc.Table _ caption specs thead tbodies tfoot) = prettyBlock ds (Pandoc.Div _attrs blocks) = prettyBlocks ds blocks prettyBlock ds (Pandoc.LineBlock inliness) = - let ind = PP.NotTrimmable (themed ds themeLineBlock "| ") in + let ind = PP.Indentation 0 (themed ds themeLineBlock "| ") in PP.wrapAt Nothing $ PP.indent ind ind $ PP.vcat $ diff --git a/lib/Patat/Presentation/Display/CodeBlock.hs b/lib/Patat/Presentation/Display/CodeBlock.hs index 1fca2e7..01b62a3 100644 --- a/lib/Patat/Presentation/Display/CodeBlock.hs +++ b/lib/Patat/Presentation/Display/CodeBlock.hs @@ -76,7 +76,7 @@ prettyCodeBlock ds classes rawCodeBlock = blockified :: Skylighting.SourceLine -> PP.Doc blockified line = let len = sourceLineLength line - indent = PP.NotTrimmable " " in + indent = PP.Indentation 3 mempty in PP.indent indent indent $ themed ds themeCodeBlock $ " " <> diff --git a/lib/Patat/Presentation/Display/Table.hs b/lib/Patat/Presentation/Display/Table.hs index e7de609..5aa7de4 100644 --- a/lib/Patat/Presentation/Display/Table.hs +++ b/lib/Patat/Presentation/Display/Table.hs @@ -30,7 +30,7 @@ data Table = Table -------------------------------------------------------------------------------- prettyTable :: DisplaySettings -> Table -> PP.Doc prettyTable ds Table {..} = - PP.indent (PP.Trimmable " ") (PP.Trimmable " ") $ + PP.indent indentation indentation $ lineIf (not isHeaderLess) (hcat2 headerHeight [ themed ds themeTableHeader $ PP.align w a (vpad headerHeight header) @@ -48,6 +48,8 @@ prettyTable ds Table {..} = lineIf (not $ PP.null tCaption) (PP.hardline <> "Table: " <> tCaption) where + indentation = PP.Indentation 2 mempty + lineIf cond line = if cond then line <> PP.hardline else mempty joinRows diff --git a/lib/Patat/PrettyPrint.hs b/lib/Patat/PrettyPrint.hs index 295b655..9c7fea3 100644 --- a/lib/Patat/PrettyPrint.hs +++ b/lib/Patat/PrettyPrint.hs @@ -24,7 +24,7 @@ module Patat.PrettyPrint , wrapAt - , Trimmable (..) + , Indentation (..) , indent , ansi @@ -91,10 +91,10 @@ wrapAt wrapAtCol wrapDoc = mkDoc WrapAt {..} -------------------------------------------------------------------------------- -indent :: Trimmable Doc -> Trimmable Doc -> Doc -> Doc +indent :: Indentation Doc -> Indentation Doc -> Doc -> Doc indent firstLineDoc otherLinesDoc doc = mkDoc $ Indent - { indentFirstLine = traverse docToChunks firstLineDoc - , indentOtherLines = traverse docToChunks otherLinesDoc + { indentFirstLine = fmap docToChunks firstLineDoc + , indentOtherLines = fmap docToChunks otherLinesDoc , indentDoc = doc } diff --git a/lib/Patat/PrettyPrint/Internal.hs b/lib/Patat/PrettyPrint/Internal.hs index cbc0d1a..7e5833c 100644 --- a/lib/Patat/PrettyPrint/Internal.hs +++ b/lib/Patat/PrettyPrint/Internal.hs @@ -15,11 +15,11 @@ module Patat.PrettyPrint.Internal , DocE (..) , chunkToDocE + , Indentation (..) + , Doc (..) , docToChunks - , Trimmable (..) - , toString , dimensions , null @@ -119,8 +119,8 @@ data DocE d , ansiDoc :: d } | Indent - { indentFirstLine :: LineBuffer - , indentOtherLines :: LineBuffer + { indentFirstLine :: Indentation [Chunk] + , indentOtherLines :: Indentation [Chunk] , indentDoc :: d } | Control Control @@ -151,9 +151,9 @@ instance IsString Doc where -------------------------------------------------------------------------------- data DocEnv = DocEnv - { deCodes :: [Ansi.SGR] -- ^ Most recent ones first in the list - , deIndent :: LineBuffer -- ^ Don't need to store first-line indent - , deWrap :: Maybe Int -- ^ Wrap at columns + { deCodes :: [Ansi.SGR] -- ^ Most recent ones first in the list + , deIndent :: [Indentation [Chunk]] -- ^ No need to store first-line indent + , deWrap :: Maybe Int -- ^ Wrap at columns } @@ -162,33 +162,34 @@ type DocM = RWS DocEnv Chunks LineBuffer -------------------------------------------------------------------------------- -data Trimmable a - = NotTrimmable !a - | Trimmable !a - deriving (Foldable, Functor, Traversable) +-- | Note that these are reversed so we have fast append +data LineBuffer = LineBuffer [Indentation [Chunk]] [Chunk] -------------------------------------------------------------------------------- --- | Note that this is reversed so we have fast append -type LineBuffer = [Trimmable Chunk] +data Indentation a = Indentation Int a + deriving (Foldable, Functor, Traversable) -------------------------------------------------------------------------------- bufferToChunks :: LineBuffer -> Chunks -bufferToChunks = map trimmableToChunk . reverse . dropWhile isTrimmable +bufferToChunks (LineBuffer ind chunks) = case chunks of + [] -> concatMap indentationToChunks $ reverse $ + dropWhile emptyIndentation ind + _ -> concatMap indentationToChunks (reverse ind) ++ reverse chunks where - isTrimmable (NotTrimmable _) = False - isTrimmable (Trimmable _) = True + emptyIndentation (Indentation _ []) = True + emptyIndentation _ = False - trimmableToChunk (NotTrimmable c) = c - trimmableToChunk (Trimmable c) = c + indentationToChunks (Indentation 0 c) = c + indentationToChunks (Indentation n c) = StringChunk [] (replicate n ' ') : c -------------------------------------------------------------------------------- docToChunks :: Doc -> Chunks docToChunks doc0 = let env0 = DocEnv [] [] Nothing - ((), b, cs) = runRWS (go $ unDoc doc0) env0 mempty in + ((), b, cs) = runRWS (go $ unDoc doc0) env0 (LineBuffer [] []) in optimizeChunks (cs <> bufferToChunks b) where go :: [DocE Doc] -> DocM () @@ -197,7 +198,7 @@ docToChunks doc0 = go (String str : docs) = do chunk <- makeChunk str - modify (NotTrimmable chunk :) + appendChunk chunk go docs go (Softspace : docs) = do @@ -206,7 +207,7 @@ docToChunks doc0 = go (Hardspace : docs) = do chunk <- makeChunk " " - modify (NotTrimmable chunk :) + appendChunk chunk go docs go (Softline : docs) = do @@ -217,7 +218,7 @@ docToChunks doc0 = buffer <- get tell $ bufferToChunks buffer <> [NewlineChunk] indentation <- asks deIndent - modify $ \_ -> if L.null docs then [] else indentation + modify $ \_ -> LineBuffer (if L.null docs then [] else indentation) [] go docs go (WrapAt {..} : docs) = do @@ -230,8 +231,8 @@ docToChunks doc0 = go docs go (Indent {..} : docs) = do - local (\env -> env {deIndent = indentOtherLines ++ deIndent env}) $ do - modify (indentFirstLine ++) + local (\env -> env {deIndent = indentOtherLines : deIndent env}) $ do + modify $ \(LineBuffer i c) -> LineBuffer (indentFirstLine : i) c go (unDoc indentDoc) go docs @@ -245,6 +246,9 @@ docToChunks doc0 = codes <- asks deCodes return $ StringChunk codes str + appendChunk :: Chunk -> DocM () + appendChunk c = modify $ \(LineBuffer i cs) -> LineBuffer i (c : cs) + -- Convert 'Softspace' or 'Softline' to 'Hardspace' or 'Hardline' softConversion :: DocE Doc -> [DocE Doc] -> DocM (DocE Doc) softConversion soft docs = do @@ -316,4 +320,5 @@ mkDoc e = Doc [e] -------------------------------------------------------------------------------- string :: String -> Doc -string = mkDoc . String -- TODO (jaspervdj): Newline conversion? +string "" = Doc [] +string str = mkDoc $ String str -- TODO (jaspervdj): Newline conversion? diff --git a/tests/golden/outputs/03.md.dump b/tests/golden/outputs/03.md.dump index b899630..6567af5 100644 --- a/tests/golden/outputs/03.md.dump +++ b/tests/golden/outputs/03.md.dump @@ -2,8 +2,8 @@ Inline markups: - - ~~striked out~~ - - <http://example.com> + - ~~striked out~~ + - <http://example.com>  1 / 5  @@ -14,21 +14,21 @@ > Quote with embedded list: >  ->  - Hello ->  - World +>  - Hello +>  - World  2 / 5  {slide}  03.md  - - List with an embedded quote: + - List with an embedded quote:  > Tu quoque  Wow rad stuff. - - Second item in that list. + - Second item in that list.  3 / 5  diff --git a/tests/golden/outputs/eval01.md.dump b/tests/golden/outputs/eval01.md.dump index ccf81a3..36b4ad3 100644 --- a/tests/golden/outputs/eval01.md.dump +++ b/tests/golden/outputs/eval01.md.dump @@ -2,13 +2,13 @@ # Slide 1 - - This is some code that is not evaluated: + - This is some code that is not evaluated:      echo foo     - - And here is some code that is evaluated: + - And here is some code that is evaluated:      echo foo  @@ -22,13 +22,13 @@ # Slide 1 - - This is some code that is not evaluated: + - This is some code that is not evaluated:      echo foo     - - And here is some code that is evaluated: + - And here is some code that is evaluated:      foo  diff --git a/tests/golden/outputs/eval02.md.dump b/tests/golden/outputs/eval02.md.dump index edf436f..eb383c4 100644 --- a/tests/golden/outputs/eval02.md.dump +++ b/tests/golden/outputs/eval02.md.dump @@ -2,13 +2,13 @@ # Slide 1 - - This is some code that is not evaluated: + - This is some code that is not evaluated:      echo foo     - - And here is some code that is evaluated: + - And here is some code that is evaluated:      echo foo  @@ -22,13 +22,13 @@ # Slide 1 - - This is some code that is not evaluated: + - This is some code that is not evaluated:      echo foo     - - And here is some code that is evaluated: + - And here is some code that is evaluated:      echo foo  diff --git a/tests/golden/outputs/eval03.md.dump b/tests/golden/outputs/eval03.md.dump index a348987..a676595 100644 --- a/tests/golden/outputs/eval03.md.dump +++ b/tests/golden/outputs/eval03.md.dump @@ -2,13 +2,13 @@ # Slide 1 - - This is some code that is not evaluated: + - This is some code that is not evaluated:      echo foo     - - And here is some code that is evaluated: + - And here is some code that is evaluated:      foo  diff --git a/tests/golden/outputs/eval04.md.dump b/tests/golden/outputs/eval04.md.dump index f43f038..73a568e 100644 --- a/tests/golden/outputs/eval04.md.dump +++ b/tests/golden/outputs/eval04.md.dump @@ -2,13 +2,13 @@ # Slide 1 - - This is some code that is not evaluated: + - This is some code that is not evaluated:      echo foo     - - And here is some code that is evaluated: + - And here is some code that is evaluated:      echo foo  diff --git a/tests/golden/outputs/fragments.md.dump b/tests/golden/outputs/fragments.md.dump index b02f34d..1a029c6 100644 --- a/tests/golden/outputs/fragments.md.dump +++ b/tests/golden/outputs/fragments.md.dump @@ -6,7 +6,7 @@ {fragment}  fragments.md  - - This list + - This list  1 / 2  @@ -14,9 +14,9 @@ {fragment}  fragments.md  - - This list + - This list - - is displayed + - is displayed  1 / 2  @@ -24,11 +24,11 @@ {fragment}  fragments.md  - - This list + - This list - - is displayed + - is displayed -  * item + * item  1 / 2  @@ -36,12 +36,12 @@ {fragment}  fragments.md  - - This list + - This list - - is displayed + - is displayed -  * item -  * by item + * item + * by item  1 / 2  @@ -49,17 +49,17 @@ {fragment}  fragments.md  - - This list + - This list - - is displayed + - is displayed -  * item -  * by item + * item + * by item - - Or sometimes + - Or sometimes -  * all at -  * once + * all at + * once  1 / 2  diff --git a/tests/golden/outputs/issue-111.md.dump b/tests/golden/outputs/issue-111.md.dump index 6871b99..21a4510 100644 --- a/tests/golden/outputs/issue-111.md.dump +++ b/tests/golden/outputs/issue-111.md.dump @@ -32,8 +32,8 @@ > This is a block quote with a fairly long paragraph >  ->  - With ->  - a nice ->  - list +>  - With +>  - a nice +>  - list  3 / 3  diff --git a/tests/golden/outputs/lists.md.dump b/tests/golden/outputs/lists.md.dump index 8b6d683..1daada7 100644 --- a/tests/golden/outputs/lists.md.dump +++ b/tests/golden/outputs/lists.md.dump @@ -1,19 +1,19 @@  lists.md  - - This is a nested list. + - This is a nested list. -  * The nested items should have different list markers. + * The nested items should have different list markers. -  * I mean, they can be the same, but it doesn't look nice. + * I mean, they can be the same, but it doesn't look nice.  printf("Nested code block!\n") -  * Cool right? + * Cool right?  Definitely super cool - - One final item + - One final item  1 / 1  diff --git a/tests/golden/outputs/margins.md.dump b/tests/golden/outputs/margins.md.dump index 5622549..af32fb9 100644 --- a/tests/golden/outputs/margins.md.dump +++ b/tests/golden/outputs/margins.md.dump @@ -1,14 +1,14 @@   margins.md  -  +  This text will have 10 spaces on  the left. -  -  - So -  * will -  * these -  * bullets -  + - So + * will + * these + * bullets + +  This line will have 10 spaces on  the left, but will also break  after "left". diff --git a/tests/golden/outputs/meta.md.dump b/tests/golden/outputs/meta.md.dump index 7d92347..c6fd627 100644 --- a/tests/golden/outputs/meta.md.dump +++ b/tests/golden/outputs/meta.md.dump @@ -1,11 +1,11 @@  meta.md  - < Hello - < World -  > How -  > Are -  > You -  > Doing + < Hello + < World + > How + > Are + > You + > Doing  1 / 1  diff --git a/tests/golden/outputs/tables.md.dump b/tests/golden/outputs/tables.md.dump index 1205bd0..714503b 100644 --- a/tests/golden/outputs/tables.md.dump +++ b/tests/golden/outputs/tables.md.dump @@ -35,7 +35,7 @@  -------- ------- ------- ------------------------  First row 12.0 Example of a row that   spans multiple lines.  -  +  Second row 5.0 Here's another one. Note  the blank line between   rows.  @@ -53,7 +53,7 @@  ------ --- ---- ------------------------  First row 12.0 Example of a row that   spans multiple lines.  -  +  Second row 5.0 Here's another one. Note  the blank line between   rows.  diff --git a/tests/golden/outputs/themes.md.dump b/tests/golden/outputs/themes.md.dump index a4f7f21..e2ba579 100644 --- a/tests/golden/outputs/themes.md.dump +++ b/tests/golden/outputs/themes.md.dump @@ -1,9 +1,9 @@  themes.md  - - This is a simple list. -  + With nested items. -  + One or two bold. + - This is a simple list. + + With nested items. + + One or two bold. - - The list theming is customized a bit. + - The list theming is customized a bit.  1 / 1  diff --git a/tests/golden/outputs/wrapping.md.dump b/tests/golden/outputs/wrapping.md.dump index cec026b..638f65c 100644 --- a/tests/golden/outputs/wrapping.md.dump +++ b/tests/golden/outputs/wrapping.md.dump @@ -12,11 +12,11 @@  1 2 3 4 5   6 7 8 9 10  - - This is a list - - This list has a really long sentence + - This is a list + - This list has a really long sentence  in it which should also be wrapped  with proper indentation - - Another item + - Another item This line is long, and then ends with  code