Skip to content

Commit

Permalink
Account for text styling when shaping Markdown tables
Browse files Browse the repository at this point in the history
  • Loading branch information
lukstafi committed Jan 26, 2024
1 parent 249de66 commit e1b96d3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
10 changes: 7 additions & 3 deletions src/printbox-md/PrintBox_md.ml
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,11 @@ let rec multiline_heuristic c b =
let rec line_of_length_heuristic_exn c b =
match B.view b with
| B.Empty | B.Text {l=[]; _} -> 0
| B.Text {l=[s]; _} ->
if String.contains s '\n' then raise Not_found else String.length s
| B.Text {l=[s]; style} ->
let from_bold = if style.B.Style.bold then 4 else 0 in
let from_code =
if style.B.Style.preformatted then if String.contains s '`' then 6 else 2 else 0 in
if String.contains s '\n' then raise Not_found else String.length s + from_bold + from_code
| B.Text _ -> raise Not_found
| B.Frame _ when c.Config.frames = `As_table -> raise Not_found
| B.Frame b ->
Expand Down Expand Up @@ -292,7 +295,8 @@ let pp c out b =
(Array.map (fun _ -> 0) rows.(0)) rows in
let n_rows = Array.length rows and n_cols = Array.length rows.(0) in
Array.iteri (fun i header ->
loop ~no_block:true ~no_md ~prefix:"" @@ remove_bold header;
let header = remove_bold header in
loop ~no_block:true ~no_md ~prefix:"" header;
if i < n_cols - 1 then
let len = line_of_length_heuristic_exn c header in
fprintf out "%s|" (String.make (max 0 @@ lengths.(i) - len) ' ')
Expand Down
8 changes: 4 additions & 4 deletions src/printbox-md/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ Trees are rendered as:

There is a special case carved out for Markdown syntax tables.

Header|cells |[must be]|bold.
------|---------|---------|-----------------
Rows |[must be]|single |line.
[Only]|then |**we get** |a Markdown table.
Header |cells |[must be] |bold.
----------|---------|-------------|-----------------
Rows |[must be]|single |line.
[Only] |then |**we get** |a Markdown table.

<div>
<table class="framed">
Expand Down
32 changes: 16 additions & 16 deletions test/test_md.expected
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ Test default:
> >
> >
> >
- header 1 |header 2 |[header 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]
> ----------|----------|----------
> cell 1.1 |[cell 1.2]|cell 1.3
> [cell 2.1]|cell 2.2 |**cell 2.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**
- > header 1 |header 2 |[header 3]
> ------------|------------|--------------
> cell 1.1 |[cell 1.2] |cell 1.3
> [cell 2.1] |cell 2.2 |**cell 2.3**

Test uniform unfolded:

Expand Down Expand Up @@ -262,14 +262,14 @@ Test foldable:
> >
> >
> >
- header 1 |header 2 |[header 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]
> ----------|----------|----------
> cell 1.1 |[cell 1.2]|cell 1.3
> [cell 2.1]|cell 2.2 |**cell 2.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**
- > header 1 |header 2 |[header 3]
> ------------|------------|--------------
> cell 1.1 |[cell 1.2] |cell 1.3
> [cell 2.1] |cell 2.2 |**cell 2.3**
</details>


Expand Down
32 changes: 16 additions & 16 deletions test/test_md.expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ Test default:
> >
> >
> >
- header 1 |header 2 |[header 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]
> ----------|----------|----------
> cell 1.1 |[cell 1.2]|cell 1.3
> [cell 2.1]|cell 2.2 |**cell 2.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**
- > header 1 |header 2 |[header 3]
> ------------|------------|--------------
> cell 1.1 |[cell 1.2] |cell 1.3
> [cell 2.1] |cell 2.2 |**cell 2.3**
Test uniform unfolded:
Expand Down Expand Up @@ -262,14 +262,14 @@ Test foldable:
> >
> >
> >
- header 1 |header 2 |[header 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]
> ----------|----------|----------
> cell 1.1 |[cell 1.2]|cell 1.3
> [cell 2.1]|cell 2.2 |**cell 2.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**
- > header 1 |header 2 |[header 3]
> ------------|------------|--------------
> cell 1.1 |[cell 1.2] |cell 1.3
> [cell 2.1] |cell 2.2 |**cell 2.3**
</details>
Expand Down

0 comments on commit e1b96d3

Please sign in to comment.