From 3dfe5f08043885931cb25b8b5b0770ff49945a9c Mon Sep 17 00:00:00 2001 From: Lukasz Stafiniak Date: Sun, 21 Jan 2024 16:23:48 +0100 Subject: [PATCH] Fix breaking into lines; table tests. Will add configuration and tune the table output next. Maybe the html table conversion could be better too. --- src/printbox-md/PrintBox_md.ml | 11 ++- test/test_md.expected | 147 +++++++++++++++++++++++++++++++-- test/test_md.expected.md | 147 +++++++++++++++++++++++++++++++-- test/test_md.ml | 9 +- 4 files changed, 299 insertions(+), 15 deletions(-) diff --git a/src/printbox-md/PrintBox_md.ml b/src/printbox-md/PrintBox_md.ml index e6ebe63..0c0035a 100644 --- a/src/printbox-md/PrintBox_md.ml +++ b/src/printbox-md/PrintBox_md.ml @@ -14,7 +14,7 @@ module Config = struct } let default = { - tables=`Html; + tables=`Text; foldable_trees=false; multiline_preformatted=Code_block; one_line_preformatted=Code_quote; @@ -79,9 +79,12 @@ end let break_lines l = let lines = List.concat_map (String.split_on_char '\n') l in - List.map (fun s -> - if s.[String.length s - 1] = '\r' - then String.sub s 0 (String.length s - 1) else s) lines + List.filter_map (fun s -> + let len = String.length s in + if len = 0 then None + else if s.[len - 1] = '\r' then Some (String.sub s 0 (len - 1)) + else Some s) + lines let pp_indented ~tab_width ~code_block ~code_quote ~infix out s = let open Format in diff --git a/test/test_md.expected b/test/test_md.expected index e64059e..4f41360 100644 --- a/test/test_md.expected +++ b/test/test_md.expected @@ -20,7 +20,20 @@ Test default: > end of 5 > ``` > -- > child 6 +- > > a │looooooooooooooooooooooooo
+ > > │oonng
+ > > ─────┼──────────────────────────
+ > > bx │ ┌─┬─┐
+ > > │ │x│y│
+ > > │ ├─┼─┤
+ > > │ │1│2│
+ > > │ └─┴─┘
+ > > ─────┼──────────────────────────
+ > > │
+ > > │ x │y
+ > > ? │ ──┼──
+ > > │ 10│20
+ > > │ Test uniform unfolded: @@ -44,7 +57,44 @@ Test uniform unfolded:     subbody 5
    one tab end of sub 5
end of 5 -- child 6 +-

+
+
+
+
+
+
+
+
+
+
+
alooooooooooooooooooooooooo
+ oonng
bx
+

+

+
+
+
+
+
+
+
+
xy
12

+

+

+
?
+

+
+
+
+
+
+
+
+
xy
1020

+

+

+
Test foldable:
root @@ -79,7 +129,20 @@ Test foldable: > ``` > >
-- > child 6 +- > > a │looooooooooooooooooooooooo
+ > > │oonng
+ > > ─────┼──────────────────────────
+ > > bx │ ┌─┬─┐
+ > > │ │x│y│
+ > > │ ├─┼─┤
+ > > │ │1│2│
+ > > │ └─┴─┘
+ > > ─────┼──────────────────────────
+ > > │
+ > > │ x │y
+ > > ? │ ──┼──
+ > > │ 10│20
+ > > │ Test uniform tab=2: @@ -112,7 +175,44 @@ Test uniform tab=2:   one tab end of sub 5
end of 5 -- child 6 +-

+
+
+
+
+
+
+
+
+
+
+
alooooooooooooooooooooooooo
+ oonng
bx
+

+

+
+
+
+
+
+
+
+
xy
12

+

+

+
?
+

+
+
+
+
+
+
+
+
xy
1020

+

+

+
Test single quote tab=2: @@ -145,7 +245,44 @@ Test single quote tab=2:     `one tab end of sub 5`
`end of 5` -- child 6 +-

+
+
+
+
+
+
+
+
+
+
+
alooooooooooooooooooooooooo
+ oonng
bx
+

+

+
+
+
+
+
+
+
+
xy
12

+

+

+
?
+

+
+
+
+
+
+
+
+
xy
1020

+

+

+
The end. diff --git a/test/test_md.expected.md b/test/test_md.expected.md index e64059e..4f41360 100644 --- a/test/test_md.expected.md +++ b/test/test_md.expected.md @@ -20,7 +20,20 @@ Test default: > end of 5 > ``` > -- > child 6 +- > > a │looooooooooooooooooooooooo
+ > > │oonng
+ > > ─────┼──────────────────────────
+ > > bx │ ┌─┬─┐
+ > > │ │x│y│
+ > > │ ├─┼─┤
+ > > │ │1│2│
+ > > │ └─┴─┘
+ > > ─────┼──────────────────────────
+ > > │
+ > > │ x │y
+ > > ? │ ──┼──
+ > > │ 10│20
+ > > │ Test uniform unfolded: @@ -44,7 +57,44 @@ Test uniform unfolded:     subbody 5
    one tab end of sub 5
end of 5 -- child 6 +-

+
+
+
+
+
+
+
+
+
+
+
alooooooooooooooooooooooooo
+ oonng
bx
+

+

+
+
+
+
+
+
+
+
xy
12

+

+

+
?
+

+
+
+
+
+
+
+
+
xy
1020

+

+

+
Test foldable:
root @@ -79,7 +129,20 @@ Test foldable: > ``` > >
-- > child 6 +- > > a │looooooooooooooooooooooooo
+ > > │oonng
+ > > ─────┼──────────────────────────
+ > > bx │ ┌─┬─┐
+ > > │ │x│y│
+ > > │ ├─┼─┤
+ > > │ │1│2│
+ > > │ └─┴─┘
+ > > ─────┼──────────────────────────
+ > > │
+ > > │ x │y
+ > > ? │ ──┼──
+ > > │ 10│20
+ > > │ Test uniform tab=2: @@ -112,7 +175,44 @@ Test uniform tab=2:   one tab end of sub 5
end of 5 -- child 6 +-

+
+
+
+
+
+
+
+
+
+
+
alooooooooooooooooooooooooo
+ oonng
bx
+

+

+
+
+
+
+
+
+
+
xy
12

+

+

+
?
+

+
+
+
+
+
+
+
+
xy
1020

+

+

+
Test single quote tab=2: @@ -145,7 +245,44 @@ Test single quote tab=2:     `one tab end of sub 5`
`end of 5` -- child 6 +-

+
+
+
+
+
+
+
+
+
+
+
alooooooooooooooooooooooooo
+ oonng
bx
+

+

+
+
+
+
+
+
+
+
xy
12

+

+

+
?
+

+
+
+
+
+
+
+
+
xy
1020

+

+

+
The end. diff --git a/test/test_md.ml b/test/test_md.ml index f13b169..027e10a 100644 --- a/test/test_md.ml +++ b/test/test_md.ml @@ -1,5 +1,12 @@ let b = let open PrintBox in + let table = + frame @@ grid_l [ + [ text "a"; text "looooooooooooooooooooooooo\noonng"]; + [ text "bx"; center_hv @@ frame @@ record ["x", int 1; "y", int 2]]; + [ pad' ~col:2 ~lines:2 @@ text "?"; + center_hv @@ record ["x", int 10; "y", int 20]]; + ] in tree (frame @@ text "root") [ frame @@ text "child 1"; text_with_style Style.preformatted "child 2"; @@ -14,7 +21,7 @@ let b = [lines_with_style Style.preformatted ["subchild 5"; " body 5"; " subbody 5"; "\tone tab end of sub 5"; "end of 5"]]; - frame @@ text "child 6" + frame table ] let () = print_endline "Test default:"