Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #39: more compact output for html and markdown #40

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ an alternative solution where trees are printed in HTML using the
# print_endline PrintBox_html.(to_string
~config:Config.(tree_summary true default)
B.(tree (text "0")[text "1"; tree (text "ω") [text "ω²"]]));;
<div><details><summary><span class="">0</span></summary><ul><li><div class="">1</div></li><li><details><summary><span class="">ω</span></summary><ul><li><div class="">ω²</div></li></ul></details></li></ul></details></div>
<div><details><summary><span>0</span></summary><ul><li><div>1</div></li><li><details><summary><span>ω</span></summary><ul><li><div>ω²</div></li></ul></details></li></ul></details></div>

- : unit = ()
```
2 changes: 1 addition & 1 deletion src/PrintBox.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let[@inline] view (t : t) : view = t
let[@inline] line_ s = Text { l = [ s ]; style = Style.default }

let line_with_style style s =
if String.contains s '\n' then invalid_arg "PrintBox.line";
if String.contains s '\n' then invalid_arg @@ "PrintBox.line: " ^ s;
Text { l = [ s ]; style }

let line s = line_with_style Style.default s
Expand Down
21 changes: 14 additions & 7 deletions src/printbox-html/PrintBox_html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ let attrs_of_style (s : B.Style.t) : _ list * _ =
in
a, bold

let a_class l =
if List.exists (fun s -> s <> "") l then
[ H.a_class l ]
else
[]

module Config = struct
type t = {
cls_table: string list;
Expand Down Expand Up @@ -116,7 +122,7 @@ let to_html_rec ~config (b : B.t) =
else
[]
in
H.span ~a:((H.a_class config.cls_text :: a_border) @ a @ config.a_text) l
H.span ~a:(a_class config.cls_text @ a_border @ a @ config.a_text) l
in
let v_text_to_html ?(border = false) ~l ~style () =
let a, bold = attrs_of_style style in
Expand All @@ -128,7 +134,7 @@ let to_html_rec ~config (b : B.t) =
in
if style.B.Style.preformatted then
H.pre
~a:((H.a_class config.cls_text :: a_border) @ a @ config.a_text)
~a:(a_class config.cls_text @ a_border @ a @ config.a_text)
[ H.txt @@ String.concat "\n" l ]
else (
(* TODO: remove possible trailing '\r' *)
Expand All @@ -151,7 +157,7 @@ let to_html_rec ~config (b : B.t) =
[]))
l
in
H.div ~a:((H.a_class config.cls_text :: a_border) @ a @ config.a_text) l
H.div ~a:(a_class config.cls_text @ a_border @ a @ config.a_text) l
)
in
let loop
Expand All @@ -164,7 +170,8 @@ let to_html_rec ~config (b : B.t) =
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 ()] *)
| B.Text { l; style } when style.B.Style.preformatted ->
H.pre [ h_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 All @@ -182,11 +189,11 @@ let to_html_rec ~config (b : B.t) =
let to_row a =
Array.to_list a
|> List.map (fun b ->
H.td ~a:(H.a_class config.cls_col :: config.a_col) [ fix b ])
|> fun x -> H.tr ~a:(H.a_class config.cls_row :: config.a_row) x
H.td ~a:(a_class config.cls_col @ config.a_col) [ fix b ])
|> fun x -> H.tr ~a:(a_class config.cls_row @ config.a_row) x
in
let rows = Array.to_list a |> List.map to_row in
H.table ~a:(H.a_class (class_ :: config.cls_table) :: config.a_table) rows
H.table ~a:(a_class (class_ :: config.cls_table) @ config.a_table) rows
| B.Tree (_, b, l) ->
let l = Array.to_list l in
H.div [ fix b; H.ul (List.map (fun x -> H.li [ fix x ]) l) ]
Expand Down
4 changes: 3 additions & 1 deletion src/printbox-md/PrintBox_md.ml
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,10 @@ let pp c out b =
(fun _out sub -> loop ~no_block ~no_md ~prefix:subprefix sub)
out
@@ Array.to_list body;
(* Note: vlist and tree element separators move to a new line.
A non-html hlist or table as a parent would not produce correct results. *)
if c.Config.foldable_trees then
fprintf out "@,%s</details>@,%s@,%s" prefix prefix prefix
fprintf out "@,%s</details>@,%s" prefix prefix
| B.Link { uri; inner } ->
pp_print_string out "[";
loop ~no_block:true ~no_md ~prefix:(prefix ^ " ") inner;
Expand Down
56 changes: 20 additions & 36 deletions src/printbox-md/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ or if \`Bars are set, | by the | vertical dash.

<div>
<table class="framed">
<tr class="">
<td class="">
<div class="">It only works when<br/>all the elements fit</div>
</td><td class=""><div class="">logically speaking,</div></td>
<td class=""><div class=""><b>on a single line.</b></div></td>
<tr><td><div>It only works when<br/>all the elements fit</div></td>
<td><div>logically speaking,</div></td>
<td><div><b>on a single line.</b></div></td>
</tr>
</table>
</div>
Expand All @@ -46,13 +44,9 @@ or if \`Bars are set, | by the | vertical dash.

<div>
<table class="non-framed">
<tr class="">
<td class=""><div class="">Otherwise, the fallback behavior is as if</div>
</td>
<td class=""><pre class="" style="font-family: monospace">`As_table</pre>
</td>
<td class=""><div class="">was used to configure horizontal boxes.</div>
</td>
<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><div>was used to configure horizontal boxes.</div></td>
</tr>
</table>
</div>
Expand Down Expand Up @@ -105,7 +99,7 @@ to separate the entries (here with style \`Line_break).

- And suprisingly it works even better
- - when tables are configured
- <div><div style="border:thin solid"><div class="">to fallback on</div></div></div>
- <div><div style="border:thin solid"><div>to fallback on</div></div></div>


- HTML -- but it doesn't work on GitHub Preview.
Expand All @@ -127,11 +121,9 @@ Trees are rendered as:
- **are the details.**
</details>


</details>



### Tables

There is a special case carved out for Markdown syntax tables.
Expand All @@ -143,29 +135,21 @@ Rows |[must be]|single |line.

<div>
<table class="framed">
<tr class=""><td class=""><div class=""><b>Tables</b></div></td>
<td class=""><div class=""><b>that meet</b></div></td>
<td class="">
<div style="border:thin solid"><div class=""><b>neither</b></div></div>
</td><td class=""><div class=""><b>of:</b></div></td>
<tr><td><div><b>Tables</b></div></td><td><div><b>that meet</b></div></td>
<td><div style="border:thin solid"><div><b>neither</b></div></div></td>
<td><div><b>of:</b></div></td>
</tr>
<tr class="">
<td class="">
<div style="border:thin solid">
<div class=""><b>Markdown's native</b></div>
</div>
</td><td class=""><div class="">restrictions,</div></td>
<td class=""><div class="">special cases:</div></td>
<td class=""><pre class="" style="font-family: monospace">hlist
vlist</pre>
</td>
<tr>
<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>
</tr>
<tr class=""><td class=""><div class="">End up</div></td>
<td class=""><div class="">as either</div></td>
<td class=""><div class="">of the fallbacks:</div></td>
<td class="">
<pre class="" style="font-family: monospace">printbox-text
printbox-html
<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>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion test/test_html.expected
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><details><summary><span class="" style="border:thin solid">root</span></summary><ul><li><div style="border:thin solid"><div class="">child 1</div></div></li><li><div class="">child 2</div></li><li><div style="border:thin solid"><div><div></div><ul><li><details><summary><span class="" style="border:thin solid">header 3</span></summary><ul><li><div style="border:thin solid"><div class="">subchild 3</div></div></li></ul></details></li></ul></div></div></li><li><div><div></div><ul><li><details><summary><span class="" style="border:thin solid">header 4</span></summary><ul><li><div class="">subchild 4</div></li></ul></details></li></ul></div></li><li><div style="border:thin solid"><details><summary><span class="">header 5</span></summary><ul><li><div class="">subchild 5</div></li></ul></details></div></li><li><div style="border:thin solid"><div class="">child 5</div></div></li></ul></details></div>
<div><details><summary><span style="border:thin solid">root</span></summary><ul><li><div style="border:thin solid"><div>child 1</div></div></li><li><div>child 2</div></li><li><div style="border:thin solid"><div><div></div><ul><li><details><summary><span style="border:thin solid">header 3</span></summary><ul><li><div style="border:thin solid"><div>subchild 3</div></div></li></ul></details></li></ul></div></div></li><li><div><div></div><ul><li><details><summary><span style="border:thin solid">header 4</span></summary><ul><li><div>subchild 4</div></li></ul></details></li></ul></div></li><li><div style="border:thin solid"><details><summary><span>header 5</span></summary><ul><li><div>subchild 5</div></li></ul></details></div></li><li><div style="border:thin solid"><div>child 5</div></div></li></ul></details></div>

Loading
Loading