Skip to content

Commit 9c785e5

Browse files
authored
Merge pull request #32 from lukstafi/master
Support frames in the HTML output
2 parents 8a23ed1 + 3a317c3 commit 9c785e5

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

src/printbox-html/PrintBox_html.ml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,22 @@ end
8787

8888
let to_html_rec ~config (b: B.t) =
8989
let open Config in
90-
let text_to_html ~l ~style =
90+
let text_to_html ?(border=false) ~l ~style () =
9191
let a, bold = attrs_of_style style in
9292
let l = List.map H.txt l in
9393
let l = if bold then List.map (fun x->H.b [x]) l else l in
94-
H.span ~a:(H.a_class config.cls_text :: (a @ config.a_text)) l in
94+
let a_border = if border then [H.a_style "border:thin solid"] else [] in
95+
H.span ~a:(H.a_class config.cls_text :: a_border @ (a @ config.a_text)) l in
9596
let loop :
9697
'tags. (B.t -> ([< Html_types.flow5 > `Pre `Span `Div `Ul `Table `P] as 'tags) html) -> B.t -> 'tags html =
9798
fun fix b ->
9899
match B.view b with
99100
| B.Empty -> (H.div [] :> [< Html_types.flow5 > `Pre `Span `Div `P `Table `Ul ] html)
100-
| B.Text {l; style} when style.B.Style.preformatted -> H.pre [text_to_html ~l ~style]
101-
| B.Text {l; style} -> text_to_html ~l ~style
102-
| B.Pad (_, b)
103-
| B.Frame b -> fix b
101+
| B.Text {l; style} when style.B.Style.preformatted -> H.pre [text_to_html ~l ~style ()]
102+
| B.Text {l; style} -> text_to_html ~l ~style ()
103+
| B.Pad (_, b) -> fix b
104+
| B.Frame b ->
105+
H.div ~a:[H.a_style "border:thin solid"] [ fix b ]
104106
| B.Align {h=`Right;inner=b;v=_} ->
105107
H.div ~a:[H.a_class ["align-right"]] [ fix b ]
106108
| B.Align {h=`Center;inner=b;v=_} ->
@@ -135,13 +137,23 @@ let to_html_rec ~config (b: B.t) =
135137
let l = Array.to_list l in
136138
(match B.view b with
137139
| B.Text {l=tl; style} ->
138-
H.details (H.summary [text_to_html ~l:tl ~style])
140+
H.details (H.summary [text_to_html ~l:tl ~style ()])
139141
[ H.ul (List.map (fun x -> H.li [to_html_rec x]) l) ]
140-
| _ ->
142+
| B.Frame b ->
143+
(match B.view b with
144+
| (B.Text {l=tl; style}) ->
145+
H.details (H.summary [text_to_html ~border:true ~l:tl ~style ()])
146+
[ H.ul (List.map (fun x -> H.li [to_html_rec x]) l) ]
147+
| _ ->
141148
H.div
142-
[ to_html_rec b
143-
; H.ul (List.map (fun x -> H.li [to_html_rec x]) l)
144-
])
149+
[ to_html_rec b
150+
; H.ul (List.map (fun x -> H.li [to_html_rec x]) l)
151+
])
152+
| _ ->
153+
H.div
154+
[ to_html_rec b
155+
; H.ul (List.map (fun x -> H.li [to_html_rec x]) l)
156+
])
145157
| B.Link {uri; inner} ->
146158
H.div [H.a ~a:[H.a_href uri] [to_html_nondet_rec inner]]
147159
| _ -> loop to_html_rec b

test/dune

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
(executables
3-
(names test1 test_ann_0_3 test_blending)
4-
(libraries printbox printbox-text))
3+
(names test1 test_ann_0_3 test_blending test_html)
4+
(libraries printbox printbox-text printbox-html))
55

66
(rule
77
(targets test1.output)
@@ -32,3 +32,14 @@
3232
(alias runtest)
3333
(package printbox-text)
3434
(action (diff test_blending.expected test_blending.output)))
35+
36+
37+
(rule
38+
(targets test_html.output)
39+
(package printbox-html)
40+
(action (with-stdout-to %{targets} (run ./test_html.exe))))
41+
42+
(rule
43+
(alias runtest)
44+
(package printbox-html)
45+
(action (diff test_html.expected test_html.output)))

test/test_html.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div><details><summary><span class="" style="border:thin solid">root</span></summary><ul><li><div style="border:thin solid"><span class="">child 1</span></div></li><li><span class="">child 2</span></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"><span class="">subchild 3</span></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><span class="">subchild 4</span></li></ul></details></li></ul></div></li><li><div style="border:thin solid"><details><summary><span class="">header 5</span></summary><ul><li><span class="">subchild 5</span></li></ul></details></div></li><li><div style="border:thin solid"><span class="">child 5</span></div></li></ul></details></div>
2+

test/test_html.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
let b =
2+
let open PrintBox in
3+
tree (frame @@ text "root") [
4+
frame @@ text "child 1";
5+
text "child 2";
6+
frame @@ tree empty [
7+
tree (frame @@ text "header 3") [frame @@ text "subchild 3"]
8+
];
9+
tree empty [
10+
tree (frame @@ text "header 4") [text "subchild 4"]
11+
];
12+
frame @@ tree (text "header 5") [text "subchild 5"];
13+
frame @@ text "child 5"
14+
]
15+
16+
let () = print_endline @@ PrintBox_html.(to_string ~config:Config.(tree_summary true default)) b

0 commit comments

Comments
 (0)