Skip to content

Commit

Permalink
Bug fix: multiline text boxes are always vertical
Browse files Browse the repository at this point in the history
  • Loading branch information
lukstafi authored and c-cube committed Feb 25, 2024
1 parent 31d8351 commit 0cbd70f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
67 changes: 31 additions & 36 deletions src/printbox-html/PrintBox_html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,30 @@ module Config = struct
let tree_summary x c = { c with tree_summary = x }
end

let br_lines ~bold l =
let l =
List.map H.txt @@ List.concat @@ List.map (String.split_on_char '\n') l
in
let len = List.length l in
List.concat
@@ List.mapi
(fun i x ->
(if bold then
H.b [ x ]
else
x)
::
(if i < len - 1 then
[ H.br () ]
else
[]))
l

let to_html_rec ~config (b : B.t) =
let open Config in
let h_text_to_html ?(border = false) ~l ~style () =
let br_text_to_html ?(border = false) ~l ~style () =
let a, bold = attrs_of_style style in
let l = List.map H.txt l in
let l =
if bold then
List.map (fun x -> H.b [ x ]) l
else
l
in
let l = br_lines ~bold l in
let a_border =
if border then
[ H.a_style "border:thin solid" ]
Expand All @@ -138,40 +151,22 @@ let to_html_rec ~config (b : B.t) =
[ H.txt @@ String.concat "\n" l ]
else (
(* TODO: remove possible trailing '\r' *)
let l =
List.map H.txt @@ List.concat @@ List.map (String.split_on_char '\n') l
in
let len = List.length l in
let l =
List.concat
@@ List.mapi
(fun i x ->
(if bold then
H.b [ x ]
else
x)
::
(if i < len - 1 then
[ H.br () ]
else
[]))
l
in
let l = br_lines ~bold l in
H.div ~a:(a_class config.cls_text @ a_border @ a @ config.a_text) l
)
in
let loop
: 'tags.
(B.t ->
([< Html_types.flow5 > `Pre `Span `Div `Ul `Table `P ] as 'tags) html) ->
B.t ->
'tags html =
let loop :
'tags.
(B.t ->
([< Html_types.flow5 > `Pre `Span `Div `Ul `Table `P ] as 'tags) html) ->
B.t ->
'tags html =
fun fix b ->
match B.view b with
| B.Empty ->
(H.div [] :> [< Html_types.flow5 > `Pre `Span `Div `P `Table `Ul ] html)
| B.Text { l; style } when style.B.Style.preformatted ->
H.pre [ h_text_to_html ~l ~style () ]
v_text_to_html ~l ~style ()
| B.Text { l; style } -> v_text_to_html ~l ~style ()
| B.Pad (_, b) -> fix b
| B.Frame b -> H.div ~a:[ H.a_style "border:thin solid" ] [ fix b ]
Expand Down Expand Up @@ -206,13 +201,13 @@ let to_html_rec ~config (b : B.t) =
(match B.view b with
| B.Text { l = tl; style } ->
H.details
(H.summary [ h_text_to_html ~l:tl ~style () ])
(H.summary [ br_text_to_html ~l:tl ~style () ])
[ H.ul (List.map (fun x -> H.li [ to_html_rec x ]) l) ]
| B.Frame b ->
(match B.view b with
| B.Text { l = tl; style } ->
H.details
(H.summary [ h_text_to_html ~border:true ~l:tl ~style () ])
(H.summary [ br_text_to_html ~border:true ~l:tl ~style () ])
[ H.ul (List.map (fun x -> H.li [ to_html_rec x ]) l) ]
| _ ->
H.div
Expand Down
11 changes: 5 additions & 6 deletions src/printbox-md/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ or if \`Bars are set, | by the | vertical dash.
<div>
<table class="non-framed">
<tr><td><div>Otherwise, the fallback behavior is as if</div></td>
<td><pre><span style="font-family: monospace">`As_table</span></pre></td>
<td><pre style="font-family: monospace">`As_table</pre></td>
<td><div>was used to configure horizontal boxes.</div></td>
</tr>
</table>
Expand Down Expand Up @@ -143,14 +143,13 @@ Rows |[must be]|single |line.
<td>
<div style="border:thin solid"><div><b>Markdown's native</b></div></div>
</td><td><div>restrictions,</div></td><td><div>special cases:</div></td>
<td><pre><span style="font-family: monospace">hlistvlist</span></pre></td>
<td><pre style="font-family: monospace">hlist
vlist</pre></td>
</tr>
<tr><td><div>End up</div></td><td><div>as either</div></td>
<td><div>of the fallbacks:</div></td>
<td>
<pre>
<span style="font-family: monospace">printbox-textprintbox-html</span>
</pre>
<td><pre style="font-family: monospace">printbox-text
printbox-html</pre>
</td>
</tr>
</table>
Expand Down
6 changes: 5 additions & 1 deletion test/test_md.expected
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ Test uniform unfolded:
- \<returns\>
- `<nothing>`
- & \*\*subchild\*\* 4
- <div><div style="border:thin solid"><div><pre><span style="font-family: monospace">header 5</span></pre><ul><li><pre><span style="font-family: monospace">subchild 5 body 5 subbody 5 one tab end of sub 5end of 5</span></pre></li></ul></div></div></div>
- <div><div style="border:thin solid"><div><pre style="font-family: monospace">header 5</pre><ul><li><pre style="font-family: monospace">subchild 5
body 5
subbody 5
one tab end of sub 5
end of 5</pre></li></ul></div></div></div>


- <div><div style="border:thin solid"><div style="border:thin solid"><table class="framed"><tr><td><div>a</div></td><td><div>looooooooooooooooooooooooo<br/>oonng</div></td></tr><tr><td><div>bx</div></td><td><div class="center"><div style="border:thin solid"><table class="framed"><tr><td><div>x</div></td><td><div>y</div></td></tr><tr><td><div>1</div></td><td><div>2</div></td></tr></table></div></div></td></tr><tr><td><div>?</div></td><td><div class="center"><table class="framed"><tr><td><div>x</div></td><td><div>y</div></td></tr><tr><td><div>10</div></td><td><div>20</div></td></tr></table></div></td></tr></table></div></div></div>
Expand Down
6 changes: 5 additions & 1 deletion test/test_md.expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ Test uniform unfolded:
- \<returns\>
- `<nothing>`
- & \*\*subchild\*\* 4
- <div><div style="border:thin solid"><div><pre><span style="font-family: monospace">header 5</span></pre><ul><li><pre><span style="font-family: monospace">subchild 5 body 5 subbody 5 one tab end of sub 5end of 5</span></pre></li></ul></div></div></div>
- <div><div style="border:thin solid"><div><pre style="font-family: monospace">header 5</pre><ul><li><pre style="font-family: monospace">subchild 5
body 5
subbody 5
one tab end of sub 5
end of 5</pre></li></ul></div></div></div>


- <div><div style="border:thin solid"><div style="border:thin solid"><table class="framed"><tr><td><div>a</div></td><td><div>looooooooooooooooooooooooo<br/>oonng</div></td></tr><tr><td><div>bx</div></td><td><div class="center"><div style="border:thin solid"><table class="framed"><tr><td><div>x</div></td><td><div>y</div></td></tr><tr><td><div>1</div></td><td><div>2</div></td></tr></table></div></div></td></tr><tr><td><div>?</div></td><td><div class="center"><table class="framed"><tr><td><div>x</div></td><td><div>y</div></td></tr><tr><td><div>10</div></td><td><div>20</div></td></tr></table></div></td></tr></table></div></div></div>
Expand Down

0 comments on commit 0cbd70f

Please sign in to comment.