Skip to content

Commit

Permalink
Add margins align based on new AST
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Jan 4, 2025
1 parent 9a24b4e commit 95e6584
Show file tree
Hide file tree
Showing 9 changed files with 533 additions and 12 deletions.
56 changes: 44 additions & 12 deletions lib/Patat/Presentation/Display.hs
Original file line number Diff line number Diff line change
Expand Up @@ -186,36 +186,68 @@ 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
NotAuto _ -> 0
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
Expand Down
35 changes: 35 additions & 0 deletions tests/golden/inputs/margins01.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/golden/outputs/eval02.md.dump
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
  






 1 / 1 

{fragment}
Expand Down
12 changes: 12 additions & 0 deletions tests/golden/outputs/eval05.md.dump
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
  echo foo 
  





  
  echo foo 
  





 1 / 1 

{fragment}
Expand All @@ -29,6 +37,10 @@
  echo foo 
  





 1 / 1 

{fragment}
Expand Down
24 changes: 24 additions & 0 deletions tests/golden/outputs/eval09.md.dump
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
  echo foo 
  













 1 / 1 

{fragment}
Expand All @@ -21,6 +33,14 @@
  foo 
  









 1 / 1 

{fragment}
Expand All @@ -40,6 +60,10 @@
  echo foo 
  





 1 / 1 

{fragment}
Expand Down
44 changes: 44 additions & 0 deletions tests/golden/outputs/fragments.md.dump
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@














 1 / 2 

{fragment}
Expand All @@ -10,6 +21,16 @@
 - This list












 1 / 2 

{fragment}
Expand All @@ -22,6 +43,12 @@









 1 / 2 

{fragment}
Expand All @@ -34,6 +61,12 @@
 * item








 1 / 2 

{fragment}
Expand All @@ -47,6 +80,11 @@
 * by item







 1 / 2 

{fragment}
Expand All @@ -72,6 +110,10 @@

Legen





 2 / 2 

{fragment}
Expand All @@ -81,6 +123,8 @@

wait for it



 2 / 2 

{fragment}
Expand Down
Loading

0 comments on commit 95e6584

Please sign in to comment.