package janestreet_csv

  1. Overview
  2. Docs

Source file to_html_table.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
open Core
open Csv_common

let convert_attrs string_attrs =
  List.map string_attrs ~f:(fun (name, value) ->
    Tyxml.Html.Unsafe.string_attrib name (Option.value ~default:"" value))
;;

let run
  ?separator
  ~no_header
  ~suppress_header
  ~table_attrs
  ~th_attrs
  ~tr_attrs
  ~td_attrs
  ~border
  ~unescaped_html
  file
  =
  Or_file.with_all file ?separator ~f:(fun csv ->
    let table_attrs =
      convert_attrs (("border", Some (if border then "1" else "0")) :: table_attrs)
    in
    let tr_attrs = convert_attrs tr_attrs in
    let field_to_html =
      if unescaped_html then Tyxml.Html.Unsafe.data else Tyxml.Html.txt
    in
    let header =
      if no_header || suppress_header
      then None
      else (
        let th_attrs = convert_attrs th_attrs in
        Some
          (Tyxml.Html.tr
             ~a:tr_attrs
             (List.map csv.header ~f:(fun value ->
                Tyxml.Html.(th ~a:th_attrs [ field_to_html value ])))))
    in
    let rows =
      let td_attrs = convert_attrs td_attrs in
      (if no_header && not suppress_header then csv.header :: csv.lines else csv.lines)
      |> List.map ~f:(fun line ->
           Tyxml.Html.tr
             ~a:tr_attrs
             (List.map line ~f:(fun value ->
                Tyxml.Html.(td ~a:td_attrs [ field_to_html value ]))))
    in
    let document = Tyxml.Html.table ~a:table_attrs (Option.to_list header @ rows) in
    Format.printf "%a" (Tyxml.Html.pp_elt ()) document)
;;
OCaml

Innovation. Community. Security.