diff --git a/src/printbox-md/PrintBox_md.ml b/src/printbox-md/PrintBox_md.ml index 5c5d32b..5829347 100644 --- a/src/printbox-md/PrintBox_md.ml +++ b/src/printbox-md/PrintBox_md.ml @@ -10,6 +10,7 @@ module Config = struct foldable_trees: bool; multiline_preformatted: preformatted; one_line_preformatted: preformatted; + non_preformatted: [`Minimal | `Stylized]; frames: [`Quotation | `Stylized]; tab_width: int; } @@ -20,6 +21,7 @@ module Config = struct foldable_trees=false; multiline_preformatted=Code_block; one_line_preformatted=Code_quote; + non_preformatted=`Minimal; frames=`Quotation; tab_width=4; } @@ -29,6 +31,7 @@ module Config = struct foldable_trees=true; multiline_preformatted=Stylized; one_line_preformatted=Stylized; + non_preformatted=`Stylized; frames=`Stylized; tab_width=4; } @@ -61,10 +64,19 @@ end let preformatted_conf = if multiline then c.Config.multiline_preformatted else c.Config.one_line_preformatted in let stylized = in_span || preformatted_conf = Config.Stylized in + let code_block = + preformatted && not stylized && preformatted_conf = Config.Code_block in + let code_quote = + preformatted && not stylized && preformatted_conf = Config.Code_quote in + let handle_space = not code_block && not code_quote in + let pre_block = + handle_space && multiline && c.Config.non_preformatted = `Stylized in let s = (match bg_color with None -> [] | Some c -> ["background-color", encode_color c]) @ (match fg_color with None -> [] | Some c -> ["color", encode_color c]) @ - (if stylized && preformatted then ["font-family", "monospace"] else []) + (if stylized && preformatted then ["font-family", "monospace"] else []) @ + (if handle_space && not multiline && c.Config.non_preformatted = `Stylized + then ["white-space", "pre"] else []) in let inline = in_span || not multiline in let spec_pre, spec_post = @@ -81,11 +93,7 @@ end | false, _ -> "", "" | true, false -> "**", "**" | true, true -> "", "" in - let code_block = - preformatted && not stylized && preformatted_conf = Config.Code_block in - let code_quote = - preformatted && not stylized && preformatted_conf = Config.Code_quote in - bold_pre ^ sty_pre, sty_post ^ bold_post, code_block, code_quote, inline + bold_pre ^ sty_pre, sty_post ^ bold_post, code_block, pre_block, code_quote, inline let break_lines l = let lines = List.concat @@ List.map (String.split_on_char '\n') l in @@ -212,20 +220,22 @@ let pp c out b = | B.Text {l; style} -> let l = break_lines l in let multiline = List.length l > 1 in - let sty_pre, sty_post, code_block, code_quote, inline = + let sty_pre, sty_post, code_block, pre_block, code_quote, inline = style_format c ~in_span ~multiline style in let preformat = pp_string_escaped ~tab_width:c.Config.tab_width ~code_block ~code_quote ~html:in_span in pp_print_string out sty_pre; if not inline && String.length sty_pre > 0 then fprintf out "@,%s" prefix; if code_block then fprintf out "```@,%s" prefix; + if pre_block then fprintf out {|
|}; (* use html for gb_color, fg_color and md for bold, preformatted. *) pp_print_list ~pp_sep:(fun out () -> - if not code_block then pp_print_string out "
"; + if not code_block && not pre_block then pp_print_string out "
"; fprintf out "@,%s" prefix) preformat out l; if not inline && String.length sty_pre > 0 then fprintf out "@,%s" prefix; + if pre_block then fprintf out "
"; if code_block then fprintf out "@,%s```@,%s" prefix prefix; pp_print_string out sty_post | B.Frame b -> diff --git a/src/printbox-md/readme.ml b/src/printbox-md/readme.ml new file mode 100644 index 0000000..e69de29 diff --git a/test/test_md.expected b/test/test_md.expected index d605cd8..169dcd2 100644 --- a/test/test_md.expected +++ b/test/test_md.expected @@ -66,52 +66,52 @@ Test default: Test uniform unfolded: -root -- child 1 -- child 2 -- line 1
- line 2
- line 3 -- a row 1
- a row 2.1
- a row 2.2
- a row 3 +root +- child 1 +- child 2 +-
line 1 + line 2 + line 3
+- a row 1
+
a row 2.1 + a row 2.2

+ a row 3 -
- b row 1
+ b row 1
- b row 2.1
- b row 2.2
- b row 3 +
b row 2.1 + b row 2.2
+ b row 3 -
- c row 1
+ c row 1
- c row 2.1
- c row 2.2
- c row 3 +
c row 2.1 + c row 2.2
+ c row 3 -
- - header 3 - - subchild 3 + - header 3 + - subchild 3
- - - header 4 - - \ - - \ - - & \*\*subchild\*\* 4 + - header 4 + - \ + - \ + - & \*\*subchild\*\* 4 -
- header 5 + header 5 -
- subchild 5
-  body 5
-   subbody 5
-   one tab end of sub 5
+
subchild 5 +  body 5 +   subbody 5 +   one tab end of sub 5 end of 5 -
+
-
@@ -157,16 +157,16 @@ Test uniform unfolded:
-- header 1 |header 2 |header 3 +- header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3 -
- header 1 |header 2 |header 3 + header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3
Test foldable: @@ -250,64 +250,64 @@ Test foldable: Test uniform tab=2, text tables: -
root +
root -- child 1 -- child 2 -- line 1
- line 2
- line 3 -- a row 1
- a row 2.1
- a row 2.2
- a row 3 +- child 1 +- child 2 +-
line 1 + line 2 + line 3
+- a row 1
+
a row 2.1 + a row 2.2

+ a row 3 -
- b row 1
+ b row 1
- b row 2.1
- b row 2.2
- b row 3 +
b row 2.1 + b row 2.2
+ b row 3 -
- c row 1
+ c row 1
- c row 2.1
- c row 2.2
- c row 3 +
c row 2.1 + c row 2.2
+ c row 3 -
- -
header 3 + -
header 3 - - subchild 3 + - subchild 3
-
- -
header 4 + -
header 4 - -
<returns> + -
<returns> - - \ + - \
- - & \*\*subchild\*\* 4 + - & \*\*subchild\*\* 4
-
-
header 5 +
header 5 -
- subchild 5
-  body 5
-   subbody 5
- one tab end of sub 5
+
subchild 5 +  body 5 +   subbody 5 + one tab end of sub 5 end of 5 -
+
-
@@ -315,87 +315,87 @@ Test uniform tab=2, text tables:
- a   │looooooooooooooooooooooooo
-    │oonng
- ─────┼──────────────────────────
- bx   │       ┌─┬─┐
-    │       │x│y│
-    │       ├─┼─┤
-    │       │1│2│
-    │       └─┴─┘
- ─────┼──────────────────────────
-    │
-    │       x │y
-  ?  │       ──┼──
-    │       10│20
+
a   │looooooooooooooooooooooooo +    │oonng + ─────┼────────────────────────── + bx   │       ┌─┬─┐ +    │       │x│y│ +    │       ├─┼─┤ +    │       │1│2│ +    │       └─┴─┘ + ─────┼──────────────────────────    │ +    │       x │y +  ?  │       ──┼── +    │       10│20 +    │ +
- -- header 1 |header 2 |header 3 +- header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3 -
- header 1 |header 2 |header 3 + header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3
Test single quote tab=2, text tables: -
root +
root -- child 1 -- child 2 -- line 1
- line 2
- line 3 -- a row 1
- a row 2.1
- a row 2.2
- a row 3 +- child 1 +- child 2 +-
line 1 + line 2 + line 3
+- a row 1
+
a row 2.1 + a row 2.2

+ a row 3 -
- b row 1
+ b row 1
- b row 2.1
- b row 2.2
- b row 3 +
b row 2.1 + b row 2.2
+ b row 3 -
- c row 1
+ c row 1
- c row 2.1
- c row 2.2
- c row 3 +
c row 2.1 + c row 2.2
+ c row 3 -
- -
header 3 + -
header 3 - - subchild 3 + - subchild 3
-
- -
header 4 + -
header 4 - -
<returns> + -
<returns> - - \ + - \
- - & \*\*subchild\*\* 4 + - & \*\*subchild\*\* 4
-
-
header 5 +
header 5 - `subchild 5`
`body 5`
@@ -424,16 +424,16 @@ Test single quote tab=2, text tables:    `│`
-- header 1 |header 2 |header 3 +- header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3 -
- header 1 |header 2 |header 3 + header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3
diff --git a/test/test_md.expected.md b/test/test_md.expected.md index d605cd8..169dcd2 100644 --- a/test/test_md.expected.md +++ b/test/test_md.expected.md @@ -66,52 +66,52 @@ Test default: Test uniform unfolded: -root -- child 1 -- child 2 -- line 1
- line 2
- line 3 -- a row 1
- a row 2.1
- a row 2.2
- a row 3 +root +- child 1 +- child 2 +-
line 1 + line 2 + line 3
+- a row 1
+
a row 2.1 + a row 2.2

+ a row 3 -
- b row 1
+ b row 1
- b row 2.1
- b row 2.2
- b row 3 +
b row 2.1 + b row 2.2
+ b row 3 -
- c row 1
+ c row 1
- c row 2.1
- c row 2.2
- c row 3 +
c row 2.1 + c row 2.2
+ c row 3 -
- - header 3 - - subchild 3 + - header 3 + - subchild 3
- - - header 4 - - \ - - \ - - & \*\*subchild\*\* 4 + - header 4 + - \ + - \ + - & \*\*subchild\*\* 4 -
- header 5 + header 5 -
- subchild 5
-  body 5
-   subbody 5
-   one tab end of sub 5
+
subchild 5 +  body 5 +   subbody 5 +   one tab end of sub 5 end of 5 -
+
-
@@ -157,16 +157,16 @@ Test uniform unfolded:
-- header 1 |header 2 |header 3 +- header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3 -
- header 1 |header 2 |header 3 + header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3
Test foldable: @@ -250,64 +250,64 @@ Test foldable:
Test uniform tab=2, text tables: -
root +
root -- child 1 -- child 2 -- line 1
- line 2
- line 3 -- a row 1
- a row 2.1
- a row 2.2
- a row 3 +- child 1 +- child 2 +-
line 1 + line 2 + line 3
+- a row 1
+
a row 2.1 + a row 2.2

+ a row 3 -
- b row 1
+ b row 1
- b row 2.1
- b row 2.2
- b row 3 +
b row 2.1 + b row 2.2
+ b row 3 -
- c row 1
+ c row 1
- c row 2.1
- c row 2.2
- c row 3 +
c row 2.1 + c row 2.2
+ c row 3 -
- -
header 3 + -
header 3 - - subchild 3 + - subchild 3
-
- -
header 4 + -
header 4 - -
<returns> + -
<returns> - - \ + - \
- - & \*\*subchild\*\* 4 + - & \*\*subchild\*\* 4
-
-
header 5 +
header 5 -
- subchild 5
-  body 5
-   subbody 5
- one tab end of sub 5
+
subchild 5 +  body 5 +   subbody 5 + one tab end of sub 5 end of 5 -
+
-
@@ -315,87 +315,87 @@ Test uniform tab=2, text tables:
- a   │looooooooooooooooooooooooo
-    │oonng
- ─────┼──────────────────────────
- bx   │       ┌─┬─┐
-    │       │x│y│
-    │       ├─┼─┤
-    │       │1│2│
-    │       └─┴─┘
- ─────┼──────────────────────────
-    │
-    │       x │y
-  ?  │       ──┼──
-    │       10│20
+
a   │looooooooooooooooooooooooo +    │oonng + ─────┼────────────────────────── + bx   │       ┌─┬─┐ +    │       │x│y│ +    │       ├─┼─┤ +    │       │1│2│ +    │       └─┴─┘ + ─────┼──────────────────────────    │ +    │       x │y +  ?  │       ──┼── +    │       10│20 +    │ +
- -- header 1 |header 2 |header 3 +- header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3 -
- header 1 |header 2 |header 3 + header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3
Test single quote tab=2, text tables: -
root +
root -- child 1 -- child 2 -- line 1
- line 2
- line 3 -- a row 1
- a row 2.1
- a row 2.2
- a row 3 +- child 1 +- child 2 +-
line 1 + line 2 + line 3
+- a row 1
+
a row 2.1 + a row 2.2

+ a row 3 -
- b row 1
+ b row 1
- b row 2.1
- b row 2.2
- b row 3 +
b row 2.1 + b row 2.2
+ b row 3 -
- c row 1
+ c row 1
- c row 2.1
- c row 2.2
- c row 3 +
c row 2.1 + c row 2.2
+ c row 3 -
- -
header 3 + -
header 3 - - subchild 3 + - subchild 3
-
- -
header 4 + -
header 4 - -
<returns> + -
<returns> - - \ + - \
- - & \*\*subchild\*\* 4 + - & \*\*subchild\*\* 4
-
-
header 5 +
header 5 - `subchild 5`
`body 5`
@@ -424,16 +424,16 @@ Test single quote tab=2, text tables:    `│`
-- header 1 |header 2 |header 3 +- header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3 -
- header 1 |header 2 |header 3 + header 1 |header 2 |header 3 -----------------------------------------------|-----------------------------------------------|----------------------------------------------- - cell 1.1 |cell 1.2|cell 1.3 - cell 2.1|cell 2.2 |cell 2.3 + cell 1.1 |cell 1.2|cell 1.3 + cell 2.1|cell 2.2 |cell 2.3