Skip to content

Commit

Permalink
feat(html): custom classes/attributes for html translation
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Feb 27, 2020
1 parent 37fa3d9 commit 5418717
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
61 changes: 49 additions & 12 deletions src/html/PrintBox_html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,51 @@ let attrs_of_style (s:B.Style.t) : _ list * _ =
| s -> [H.a_style @@ String.concat ";" @@ List.map (fun (k,v) -> k ^ ": " ^ v) s] in
a, bold

let rec to_html_rec (b: B.t) : [< Html_types.flow5 > `Div `Ul `Table `P] html =
module Config = struct
type t = {
cls_table: string list;
a_table: Html_types.table_attrib Html.attrib list;
cls_text: string list;
a_text: Html_types.div_attrib Html.attrib list;
cls_row: string list;
a_row: Html_types.div_attrib Html.attrib list;
cls_col: string list;
a_col: Html_types.div_attrib Html.attrib list;
}

let default : t = {
cls_table=[];
a_table=[];
cls_text=[];
a_text=[];
cls_row=[];
a_row=[];
cls_col=[];
a_col=[];
}

let cls_table x c = {c with cls_table=x}
let a_table x c = {c with a_table=x}
let cls_text x c = {c with cls_text=x}
let a_text x c = {c with a_text=x}
let cls_row x c = {c with cls_row=x}
let a_row x c = {c with a_row=x}
let cls_col x c = {c with cls_col=x}
let a_col x c = {c with a_col=x}
end

let rec to_html_rec ~config (b: B.t) : [< Html_types.flow5 > `Div `Ul `Table `P] html =
let open Config in
let to_html_rec = to_html_rec ~config in
match B.view b with
| B.Empty -> H.div []
| B.Text {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
H.div ~a l
H.div
~a:(H.a_class config.cls_text :: (a @ config.a_text))
l
| B.Pad (_, b)
| B.Frame b -> to_html_rec b
| B.Align {h=`Right;inner=b;v=_} ->
Expand All @@ -70,14 +107,14 @@ let rec to_html_rec (b: B.t) : [< Html_types.flow5 > `Div `Ul `Table `P] html =
in
let to_row a =
Array.to_list a
|> List.map (fun b -> H.td [to_html_rec b])
|> (fun x -> H.tr x)
|> List.map
(fun b -> H.td ~a:(H.a_class config.cls_col :: config.a_col) [to_html_rec b])
|> (fun x -> H.tr ~a:(H.a_class config.cls_row :: config.a_row) x)
in
let rows =
Array.to_list a
|> List.map to_row
Array.to_list a |> List.map to_row
in
H.table ~a:[H.a_class [class_]] rows
H.table ~a:(H.a_class (class_ :: config.cls_table)::config.a_table) rows
| B.Tree (_, b, l) ->
let l = Array.to_list l in
H.div
Expand All @@ -87,12 +124,12 @@ let rec to_html_rec (b: B.t) : [< Html_types.flow5 > `Div `Ul `Table `P] html =
| B.Link {uri; inner} ->
H.div [H.a ~a:[H.a_href uri] [to_html_rec inner]]

let to_html b = H.div [to_html_rec b]
let to_html ?(config=Config.default) b = H.div [to_html_rec ~config b]

let to_string b =
Format.asprintf "@[%a@]@." (H.pp_elt ()) (to_html b)
let to_string ?config b =
Format.asprintf "@[%a@]@." (H.pp_elt ()) (to_html ?config b)

let to_string_doc b =
let to_string_doc ?config b =
let meta_str = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" in
let footer_str =
"<script> \
Expand All @@ -104,4 +141,4 @@ let to_string_doc b =
</script>"
in
Format.asprintf "<head>%s%s</head><body>@[%a@]%s</body>@."
meta_str prelude_str (H.pp_elt ()) (to_html b) footer_str
meta_str prelude_str (H.pp_elt ()) (to_html ?config b) footer_str
27 changes: 22 additions & 5 deletions src/html/PrintBox_html.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,29 @@ val prelude : [> Html_types.style] html

val prelude_str : string

(* TODO: custom classes (e.g. for tables) *)

val to_html : PrintBox.t -> [`Div] html
(** {2 Classes and attributes}
Custom classes and attributes for tables, lists, etc.
@since 0.5 *)
module Config : sig
type t

val default : t
val cls_table : string list -> t -> t
val a_table : Html_types.table_attrib Html.attrib list -> t -> t
val cls_text : string list -> t -> t
val a_text : Html_types.div_attrib Html.attrib list -> t -> t
val cls_row : string list -> t -> t
val a_row : Html_types.div_attrib Html.attrib list -> t -> t
val cls_col : string list -> t -> t
val a_col : Html_types.div_attrib Html.attrib list -> t -> t
end

val to_html : ?config:Config.t -> PrintBox.t -> [`Div] html
(** HTML for one box *)

val to_string : PrintBox.t -> string
val to_string : ?config:Config.t -> PrintBox.t -> string

val to_string_doc : PrintBox.t -> string
val to_string_doc : ?config:Config.t -> PrintBox.t -> string
(** Same as {!to_string}, but adds the prelude and some footer *)

0 comments on commit 5418717

Please sign in to comment.