From 95e6584d754fe42ab714aaf10bc8e92e5c737732 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Sat, 4 Jan 2025 08:51:46 +0100 Subject: [PATCH] Add margins align based on new AST --- lib/Patat/Presentation/Display.hs | 56 +++- tests/golden/inputs/margins01.md | 35 +++ tests/golden/outputs/eval02.md.dump | 4 + tests/golden/outputs/eval05.md.dump | 12 + tests/golden/outputs/eval09.md.dump | 24 ++ tests/golden/outputs/fragments.md.dump | 44 ++++ tests/golden/outputs/fragments02.md.dump | 129 ++++++++++ .../golden/outputs/margins-auto-wrap.md.dump | 1 + tests/golden/outputs/margins01.md.dump | 240 ++++++++++++++++++ 9 files changed, 533 insertions(+), 12 deletions(-) create mode 100644 tests/golden/inputs/margins01.md create mode 100644 tests/golden/outputs/margins01.md.dump diff --git a/lib/Patat/Presentation/Display.hs b/lib/Patat/Presentation/Display.hs index 507fe76..a7aac90 100644 --- a/lib/Patat/Presentation/Display.hs +++ b/lib/Patat/Presentation/Display.hs @@ -186,29 +186,54 @@ dumpPresentation pres@Presentation {..} = -------------------------------------------------------------------------------- prettyFragment :: DisplaySettings -> [Block] -> PP.Doc prettyFragment ds blocks = vertical $ - PP.vcat (map (horizontal . prettyBlock ds) blocks) <> + map horizontal blocks ++ case prettyReferences ds blocks of - [] -> mempty - refs -> PP.hardline <> PP.vcat (map horizontal refs) + [] -> [] + refs -> + let doc0 = PP.vcat refs + size@(r, _) = PP.dimensions doc0 in + [(horizontalIndent size $ horizontalWrap doc0, r)] where Size rows columns = dsSize ds Margins {..} = dsMargins ds - vertical doc0 = - mconcat (replicate top PP.hardline) <> doc0 + -- For every block, calculate the size based on its last fragment. + blockSize block = + let counters = triggersToCounters $ blocksTriggers [block] in + PP.dimensions $ deindent $ horizontalWrap $ + prettyBlock ds {dsCounters = counters} block + + vertical :: [(PP.Doc, Int)] -> PP.Doc + vertical docs0 = mconcat (replicate top PP.hardline) <> doc where top = case mTop of - Auto -> let (r, _) = PP.dimensions doc0 in (rows - r) `div` 2 + Auto -> (rows - actual) `div` 2 NotAuto x -> x - horizontal = horizontalIndent . horizontalWrap + docs1 = [verticalPad r d | (d, r) <- docs0] + actual = sum $ map snd docs1 + doc = PP.vcat $ map fst docs1 - horizontalIndent doc0 = PP.indent indentation indentation doc1 + -- Vertically pad a doc by adding lines below it. + -- Return the actual size as well as the padded doc. + verticalPad :: Int -> PP.Doc -> (PP.Doc, Int) + verticalPad desired doc0 + | actual >= rows = (doc0, actual) + | otherwise = (doc0 <> padding, desired) where - doc1 = case (mLeft, mRight) of - (Auto, Auto) -> PP.deindent doc0 - _ -> doc0 - (_, dcols) = PP.dimensions doc1 + (actual, _) = PP.dimensions doc0 + padding = mconcat $ replicate (desired - actual) PP.hardline + + -- Horizontally align a block. Also returns the desired columns. + horizontal :: Block -> (PP.Doc, Int) + horizontal block = + let size@(r, _) = blockSize block in + (horizontalIndent size $ horizontalWrap $ prettyBlock ds block, r) + + horizontalIndent :: (Int, Int) -> PP.Doc -> PP.Doc + horizontalIndent (_, dcols) doc0 = PP.indent indentation indentation doc1 + where + doc1 = deindent doc0 left = case mLeft of NotAuto x -> x Auto -> case mRight of @@ -216,6 +241,13 @@ prettyFragment ds blocks = vertical $ Auto -> (columns - dcols) `div` 2 indentation = PP.Indentation left mempty + -- Strip leading spaces to horizontally align code blocks etc. + deindent doc0 = case (mLeft, mRight) of + (Auto, Auto) -> PP.deindent doc0 + _ -> doc0 + + -- Rearranges lines to fit into the wrap settings. + horizontalWrap :: PP.Doc -> PP.Doc horizontalWrap doc0 = case dsWrap ds of NoWrap -> doc0 AutoWrap -> PP.wrapAt (Just $ columns - right - left) doc0 diff --git a/tests/golden/inputs/margins01.md b/tests/golden/inputs/margins01.md new file mode 100644 index 0000000..c3e7274 --- /dev/null +++ b/tests/golden/inputs/margins01.md @@ -0,0 +1,35 @@ +--- +title: 'Installing software' +patat: + incrementalLists: true + margins: + left: auto + right: auto + top: auto +... + +# Introduction + +Make sure to note the difference in between using a fragment or not. + +# Entirely fragmented + +1. This is +2. An ordered list + +. . . + +Hahaha! + +- Here is +- A second list + +# Not fragmented + +1. This is +2. An ordered list + +Hahaha! + +- Here is +- A second list diff --git a/tests/golden/outputs/eval02.md.dump b/tests/golden/outputs/eval02.md.dump index eb383c4..e98ccb0 100644 --- a/tests/golden/outputs/eval02.md.dump +++ b/tests/golden/outputs/eval02.md.dump @@ -15,6 +15,10 @@    + + + +  1 / 1  {fragment} diff --git a/tests/golden/outputs/eval05.md.dump b/tests/golden/outputs/eval05.md.dump index bd8f2c6..8724724 100644 --- a/tests/golden/outputs/eval05.md.dump +++ b/tests/golden/outputs/eval05.md.dump @@ -6,10 +6,18 @@   echo foo     + + + +      echo foo     + + + +  1 / 1  {fragment} @@ -29,6 +37,10 @@   echo foo     + + + +  1 / 1  {fragment} diff --git a/tests/golden/outputs/eval09.md.dump b/tests/golden/outputs/eval09.md.dump index ec9bf3e..519d605 100644 --- a/tests/golden/outputs/eval09.md.dump +++ b/tests/golden/outputs/eval09.md.dump @@ -6,6 +6,18 @@   echo foo     + + + + + + + + + + + +  1 / 1  {fragment} @@ -21,6 +33,14 @@   foo     + + + + + + + +  1 / 1  {fragment} @@ -40,6 +60,10 @@   echo foo     + + + +  1 / 1  {fragment} diff --git a/tests/golden/outputs/fragments.md.dump b/tests/golden/outputs/fragments.md.dump index 49d5557..72b03ae 100644 --- a/tests/golden/outputs/fragments.md.dump +++ b/tests/golden/outputs/fragments.md.dump @@ -2,6 +2,17 @@ + + + + + + + + + + +  1 / 2  {fragment} @@ -10,6 +21,16 @@  - This list + + + + + + + + + +  1 / 2  {fragment} @@ -22,6 +43,12 @@ + + + + + +  1 / 2  {fragment} @@ -34,6 +61,12 @@  * item + + + + + +  1 / 2  {fragment} @@ -47,6 +80,11 @@  * by item + + + + +  1 / 2  {fragment} @@ -72,6 +110,10 @@ Legen + + + +  2 / 2  {fragment} @@ -81,6 +123,8 @@ wait for it + +  2 / 2  {fragment} diff --git a/tests/golden/outputs/fragments02.md.dump b/tests/golden/outputs/fragments02.md.dump index d0b0e38..96a3425 100644 --- a/tests/golden/outputs/fragments02.md.dump +++ b/tests/golden/outputs/fragments02.md.dump @@ -4,6 +4,10 @@ Hello + + + +  1 / 3  {fragment} @@ -15,6 +19,8 @@ World + +  1 / 3  {fragment} @@ -37,6 +43,11 @@ + + + + +  2 / 3  {fragment} @@ -47,6 +58,10 @@ 1. This is fragmented + + + +  2 / 3  {fragment} @@ -59,6 +74,8 @@  Inside a list + +  2 / 3  {fragment} @@ -83,6 +100,22 @@ + + + + + + + + + + + + + + + +  3 / 3  @@ -95,6 +128,22 @@ + + + + + + + + + + + + + + + +  3 / 3  {fragment} @@ -107,6 +156,21 @@ + + + + + + + + + + + + + + +  3 / 3  {fragment} @@ -120,6 +184,20 @@ + + + + + + + + + + + + + +  3 / 3  {fragment} @@ -136,6 +214,17 @@ + + + + + + + + + + +  3 / 3  {fragment} @@ -152,6 +241,17 @@  - Some reason + + + + + + + + + + +  3 / 3  {fragment} @@ -169,6 +269,16 @@  - IDK + + + + + + + + + +  3 / 3  {fragment} @@ -190,6 +300,12 @@ + + + + + +  3 / 3  {fragment} @@ -211,6 +327,12 @@  - Some dude + + + + + +  3 / 3  {fragment} @@ -233,6 +355,11 @@  - Or another + + + + +  3 / 3  {fragment} @@ -259,6 +386,7 @@ +  3 / 3  {fragment} @@ -285,6 +413,7 @@  - Did something +  3 / 3  {fragment} diff --git a/tests/golden/outputs/margins-auto-wrap.md.dump b/tests/golden/outputs/margins-auto-wrap.md.dump index 0a5e526..1fb8740 100644 --- a/tests/golden/outputs/margins-auto-wrap.md.dump +++ b/tests/golden/outputs/margins-auto-wrap.md.dump @@ -7,6 +7,7 @@ +  Hello world Hello world Hello world Hello world Hello world Hello world Hello world diff --git a/tests/golden/outputs/margins01.md.dump b/tests/golden/outputs/margins01.md.dump new file mode 100644 index 0000000..8da6bff --- /dev/null +++ b/tests/golden/outputs/margins01.md.dump @@ -0,0 +1,240 @@ + Installing software  + + + + + + + + + + # Introduction + + Make sure to note the difference in between using a fragment or not. + + 1 / 3  + +{slide} + Installing software  + + + + + + + # Entirely fragmented + + + + + + + + + + 2 / 3  + +{fragment} + Installing software  + + + + + + + # Entirely fragmented + + 1. This is + + + + + + + + 2 / 3  + +{fragment} + Installing software  + + + + + + + # Entirely fragmented + + 1. This is + 2. An ordered list + + + + + + + 2 / 3  + +{fragment} + Installing software  + + + + + + + # Entirely fragmented + + 1. This is + 2. An ordered list + + Hahaha! + + + + + 2 / 3  + +{fragment} + Installing software  + + + + + + + # Entirely fragmented + + 1. This is + 2. An ordered list + + Hahaha! + + - Here is + + + 2 / 3  + +{fragment} + Installing software  + + + + + + + # Entirely fragmented + + 1. This is + 2. An ordered list + + Hahaha! + + - Here is + - A second list + + 2 / 3  + +{slide} + Installing software  + + + + + + + + # Not fragmented + + + + + Hahaha! + + + + + 3 / 3  + +{fragment} + Installing software  + + + + + + + + # Not fragmented + + 1. This is + + + Hahaha! + + + + + 3 / 3  + +{fragment} + Installing software  + + + + + + + + # Not fragmented + + 1. This is + 2. An ordered list + + Hahaha! + + + + + 3 / 3  + +{fragment} + Installing software  + + + + + + + + # Not fragmented + + 1. This is + 2. An ordered list + + Hahaha! + + - Here is + + + 3 / 3  + +{fragment} + Installing software  + + + + + + + + # Not fragmented + + 1. This is + 2. An ordered list + + Hahaha! + + - Here is + - A second list + + 3 / 3